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" },
153 //{ _KEY_SIDE_UP, "XF86AudioRaiseVolume" },
154 //{ _KEY_SIDE_DOWN, "XF86AudioLowerVolume" },
155 //{ _KEY_POWER_HOLD, "XF86PowerOff" },
156 { _KEY_STOP, "XF86Stop" }
159 struct _DisplayDeleter
161 void operator()(Display* pDisplay)
163 XCloseDisplay(pDisplay);
167 _SystemUtilImpl::_SystemUtilImpl(void)
171 _SystemUtilImpl::~_SystemUtilImpl(void)
176 _SystemUtilImpl::GenerateKeyEvent(Tizen::Ui::KeyEventType keyEvent, Tizen::Ui::_KeyCode keyCode)
178 result r = E_SUCCESS;
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!");
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!");
188 for (unsigned int idx = 0; idx < sizeof(KeyCodeMap) / sizeof(ConvertKeys); ++idx)
190 if (KeyCodeMap[idx].code == keyCode)
192 KeySym keySym = XStringToKeysym(KeyCodeMap[idx].pName);
193 key = (int) XKeysymToKeycode(pDpy.get(), keySym);
200 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Unsupported KeyCode argument.");
201 return E_INVALID_ARG;
206 case KEY_EVENT_TYPE_PRESSED:
207 XTestFakeKeyEvent(pDisplay, key, true, CurrentTime);
210 case KEY_EVENT_TYPE_RELEASED:
211 XTestFakeKeyEvent(pDisplay, key, false, CurrentTime);
215 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Invalid KeyEventType argument.");
216 return E_INVALID_ARG;
219 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
224 _SystemUtilImpl::GenerateTouchEvent(TouchEventType touchEvent, const Tizen::Graphics::Point& point)
226 return GenerateTouchEvent(touchEvent, _CoordinateSystemUtils::ConvertToFloat(point));
230 _SystemUtilImpl::GenerateTouchEvent(TouchEventType touchEvent, const Tizen::Graphics::FloatPoint& point)
232 result r = E_SUCCESS;
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!");
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!");
240 int width = DisplayWidth(pDpy.get(), DefaultScreen(pDpy.get()));
241 int height = DisplayHeight(pDpy.get(), DefaultScreen(pDpy.get()));
243 if (( point.x<0 || point.x>width) || (point.y<0 || point.y>height))
245 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Out of bounds x, y argument.");
246 return E_INVALID_ARG;
249 int screenNumber = 0; //main screen
250 int button = 1; //left button
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);
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);
264 case TOUCH_EVENT_TYPE_MOVED:
265 XTestFakeMotionEvent(pDisplay, screenNumber, point.x, point.y, CurrentTime);
269 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Invalid TouchEventType argument.");
270 return E_INVALID_ARG;
273 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
277 Tizen::Graphics::Bitmap*
278 _SystemUtilImpl::CaptureScreenN(void)
280 result r = E_SUCCESS;
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!");
285 int width = DisplayWidth(pDpy.get(), DefaultScreen(pDpy.get()));
286 int height = DisplayHeight(pDpy.get(), DefaultScreen(pDpy.get()));
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!");
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.");
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));
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.");
301 r = pBitmap->Construct(*pBuffer, dim, BITMAP_PIXEL_FORMAT_ARGB8888);
302 SysTryReturn(NID_UI, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
304 utilx_release_screen_shot();
306 return pBitmap.release();
310 _SystemUtilImpl::SendKeyStopMessage(void)
314 Ecore_X_Window *_keygrab_win = NULL;
315 char msg_data[] = "PxXF86Stop";
317 Ecore_X_Atom type = ecore_x_atom_get("_HWKEY_EMULATION");
319 int ret = ecore_x_window_prop_property_get(NULL, type, ECORE_X_ATOM_WINDOW, 32, (unsigned char **)&_keygrab_win, &num);
322 Ecore_X_Window xWindow = *_keygrab_win;
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.");