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
44 #if defined KEY_SEARCH
50 #if defined KEY_QUICKPANEL
53 #include <unique_ptr.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"
64 using namespace Tizen::Security;
65 using namespace Tizen::Graphics;
66 using namespace Tizen::Base;
68 namespace Tizen { namespace Ui
77 const ConvertKeys KeyCodeMap[] = {
78 { _KEY_ESC, "Escape" },
79 { _KEY_GRAVE, "grave" },
90 { _KEY_MINUS, "minus" },
91 { _KEY_EQUAL, "equal" },
92 { _KEY_BACKSLASH, "backslash" },
93 { _KEY_BACKSPACE, "BackSpace" },
105 { _KEY_LBRACKET, "bracketleft" },
106 { _KEY_RBRACKET, "bracketright" },
107 { _KEY_ENTER, "Return" },
108 { _KEY_CAPSLOCK, "Caps_Lock" },
118 { _KEY_SEMICOLON, "semicolon" },
119 { _KEY_APOSTROPHE, "apostrophe" },
120 { _KEY_SHIFT_L, "Shift_L" },
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" },
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" },
176 { _KEY_LEFT, "Left" },
177 { _KEY_DOWN, "Down" },
178 { _KEY_RIGHT, "Right" },
179 { _KEY_CONTEXT_MENU, "Menu" },
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" },
201 //{ _KEY_SIDE_UP, "XF86AudioRaiseVolume" },
202 //{ _KEY_SIDE_DOWN, "XF86AudioLowerVolume" },
203 //{ _KEY_POWER_HOLD, "XF86PowerOff" },
206 struct _DisplayDeleter
208 void operator()(Display* pDisplay)
210 XCloseDisplay(pDisplay);
214 _SystemUtilImpl::_SystemUtilImpl(void)
218 _SystemUtilImpl::~_SystemUtilImpl(void)
223 _SystemUtilImpl::GenerateKeyEvent(Tizen::Ui::KeyEventType keyEvent, Tizen::Ui::_KeyCode keyCode)
225 result r = E_SUCCESS;
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!");
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!");
235 for (unsigned int idx = 0; idx < sizeof(KeyCodeMap) / sizeof(ConvertKeys); ++idx)
237 if (KeyCodeMap[idx].code == keyCode)
239 KeySym keySym = XStringToKeysym(KeyCodeMap[idx].pName);
240 key = (int) XKeysymToKeycode(pDpy.get(), keySym);
247 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Unsupported KeyCode argument.");
248 return E_INVALID_ARG;
253 case KEY_EVENT_TYPE_PRESSED:
254 XTestFakeKeyEvent(pDisplay, key, true, CurrentTime);
257 case KEY_EVENT_TYPE_RELEASED:
258 XTestFakeKeyEvent(pDisplay, key, false, CurrentTime);
262 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Invalid KeyEventType argument.");
263 return E_INVALID_ARG;
266 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
271 _SystemUtilImpl::GenerateTouchEvent(TouchEventType touchEvent, const Tizen::Graphics::Point& point)
273 return GenerateTouchEvent(touchEvent, _CoordinateSystemUtils::ConvertToFloat(point));
277 _SystemUtilImpl::GenerateTouchEvent(TouchEventType touchEvent, const Tizen::Graphics::FloatPoint& point)
279 result r = E_SUCCESS;
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!");
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!");
287 int width = DisplayWidth(pDpy.get(), DefaultScreen(pDpy.get()));
288 int height = DisplayHeight(pDpy.get(), DefaultScreen(pDpy.get()));
290 if (( point.x<0 || point.x>width) || (point.y<0 || point.y>height))
292 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Out of bounds x, y argument.");
293 return E_INVALID_ARG;
296 int screenNumber = 0; //main screen
297 int button = 1; //left button
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);
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);
311 case TOUCH_EVENT_TYPE_MOVED:
312 XTestFakeMotionEvent(pDisplay, screenNumber, point.x, point.y, CurrentTime);
316 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Invalid TouchEventType argument.");
317 return E_INVALID_ARG;
320 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
324 Tizen::Graphics::Bitmap*
325 _SystemUtilImpl::CaptureScreenN(void)
327 result r = E_SUCCESS;
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!");
332 int width = DisplayWidth(pDpy.get(), DefaultScreen(pDpy.get()));
333 int height = DisplayHeight(pDpy.get(), DefaultScreen(pDpy.get()));
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!");
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.");
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));
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.");
348 r = pBitmap->Construct(*pBuffer, dim, BITMAP_PIXEL_FORMAT_ARGB8888);
349 SysTryReturn(NID_UI, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
351 utilx_release_screen_shot();
353 return pBitmap.release();
357 _SystemUtilImpl::SendKeyStopMessage(void)
361 Ecore_X_Window *_keygrab_win = null;
362 char msg_data[] = "PxXF86Stop";
364 Ecore_X_Atom type = ecore_x_atom_get("_HWKEY_EMULATION");
366 int ret = ecore_x_window_prop_property_get(null, type, ECORE_X_ATOM_WINDOW, 32, (unsigned char **)&_keygrab_win, &num);
369 Ecore_X_Window xWindow = *_keygrab_win;
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.");