2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 <FUiSystemUtil.h>
30 #include <FGrpPoint.h>
31 #include <FGrpDimension.h>
32 #include <FGrpBitmap.h>
33 #include <FAppAppManager.h>
34 #include "FSec_AccessController.h"
35 #include "FApp_AppInfo.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[] = {
52 { KEY_SIDE_UP, "XF86AudioRaiseVolume" },
53 { KEY_SIDE_DOWN, "XF86AudioLowerVolume" },
54 { KEY_OK, "XF86Phone" },
70 { KEY_LEFT, "KP_Left" },
72 { KEY_DOWN, "KP_Down" },
73 { KEY_RIGHT, "KP_Right" },
101 { KEY_BACKSPACE, "BackSpace" },
102 { KEY_COMMA, "comma" },
104 { KEY_CAPSLOCK, "Caps_Lock" },
105 { KEY_QUESTION, "" },
106 { KEY_ALT, "Alt_L" },
109 { KEY_SPACE, "space" },
112 { KEY_CAMERA_HALF_SHUTTER, "" },
113 { KEY_CHAR_SYM_1, "" },
114 { KEY_CHAR_SYM_2, "" },
115 { KEY_CHAR_SYM_3, "" },
116 { KEY_CHAR_SYM_4, "" },
117 { KEY_CHAR_SYM_5, "" },
118 { KEY_CHAR_SYM_6, "" },
119 { KEY_CHAR_SYM_7, "" },
120 { KEY_CHAR_SYM_8, "" },
121 { KEY_CHAR_SYM_9, "" },
122 { KEY_CHAR_SYM_A, "" },
123 { KEY_CHAR_SYM_B, "" },
124 { KEY_CHAR_SYM_C, "" },
125 { KEY_CHAR_SYM_D, "" },
126 { KEY_CHAR_SYM_E, "" },
127 { KEY_CHAR_SYM_F, "" },
133 { KEY_POWER_HOLD, "XF86PowerOff" },
135 { KEY_HARDWARE_MAX, "" },
138 struct _DisplayDeleter
140 void operator()(Display* pDisplay)
142 XCloseDisplay(pDisplay);
146 SystemUtil::SystemUtil(void)
150 SystemUtil::~SystemUtil(void)
155 SystemUtil::GenerateKeyEvent(KeyEventType KeyEvent, KeyCode keyCode)
157 result r = E_SUCCESS;
159 const String& appId = Tizen::App::_AppInfo::GetAppId();
161 r = _AccessController::CheckSystemPrivilege(appId, _PRV_INPUTMANAGER);
163 SysTryReturn(NID_UI, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED, ("[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method."));
165 Display* pDisplay = (Display*)ecore_x_display_get();
166 SysTryReturnResult(NID_UI, pDisplay != null, E_SYSTEM, "[E_SYSTEM] pDisplay is invalid. something wrong!");
168 std::unique_ptr<Display, _DisplayDeleter> pDpy(XOpenDisplay(NULL));
169 SysTryReturnResult(NID_UI, pDpy.get() != null, E_SYSTEM, "[E_SYSTEM] pDpy is invalid. something wrong!");
173 for (unsigned int idx = 0; idx < sizeof(KeyCodeMap) / sizeof(ConvertKeys); ++idx)
175 if (KeyCodeMap[idx].code == keyCode)
177 KeySym keySym = XStringToKeysym(KeyCodeMap[idx].pName);
178 key = (int) XKeysymToKeycode(pDpy.get(), keySym);
185 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Unsupported KeyCode argument.");
186 return E_INVALID_ARG;
191 case KEY_EVENT_TYPE_PRESSED:
192 XTestFakeKeyEvent(pDisplay, key, true, CurrentTime);
195 case KEY_EVENT_TYPE_RELEASED:
196 XTestFakeKeyEvent(pDisplay, key, false, CurrentTime);
200 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Invalid KeyEventType argument.");
201 return E_INVALID_ARG;
204 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
209 SystemUtil::GenerateTouchEvent(TouchEventType touchEvent, const Tizen::Graphics::Point& point)
211 result r = E_SUCCESS;
212 const String& appId = Tizen::App::_AppInfo::GetAppId();
214 r = _AccessController::CheckSystemPrivilege(appId, _PRV_INPUTMANAGER);
216 SysTryReturn(NID_UI, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED, ("[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method."));
218 Display* pDisplay = (Display*)ecore_x_display_get();
219 SysTryReturnResult(NID_UI, pDisplay != null, E_SYSTEM, "[E_SYSTEM] pDisplay is invalid. something wrong!");
221 std::unique_ptr<Display, _DisplayDeleter> pDpy(XOpenDisplay(NULL));
222 SysTryReturnResult(NID_UI, pDpy.get() != null, E_SYSTEM, "[E_SYSTEM] pDpy is invalid. something wrong!");
224 int width = DisplayWidth(pDpy.get(), DefaultScreen(pDpy.get()));
225 int height = DisplayHeight(pDpy.get(), DefaultScreen(pDpy.get()));
227 if (( point.x<0 || point.x>width) || (point.y<0 || point.y>height))
229 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Out of bounds x, y argument.");
230 return E_INVALID_ARG;
233 int screenNumber = 0; //main screen
234 int button = 1; //left button
238 case TOUCH_EVENT_TYPE_PRESSED:
239 XWarpPointer(pDisplay, None, XRootWindow(pDisplay, 0), 0, 0, 0, 0, point.x, point.y);
240 XTestFakeButtonEvent(pDisplay, button, true, CurrentTime);
243 case TOUCH_EVENT_TYPE_RELEASED:
244 XWarpPointer(pDisplay, None, XRootWindow(pDisplay, 0), 0, 0, 0, 0, point.x, point.y);
245 XTestFakeButtonEvent(pDisplay, button, false, CurrentTime);
248 case TOUCH_EVENT_TYPE_MOVED:
249 XTestFakeMotionEvent(pDisplay, screenNumber, point.x, point.y, CurrentTime);
253 SysLogException(NID_UI, E_INVALID_ARG, "[E_INVALID_ARG] Invalid TouchEventType argument.");
254 return E_INVALID_ARG;
257 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r)); //fix me
261 Tizen::Graphics::Bitmap*
262 SystemUtil::CaptureScreenN(void)
264 result r = E_SUCCESS;
265 const String& appId = Tizen::App::_AppInfo::GetAppId();
267 r = _AccessController::CheckSystemPrivilege(appId, _PRV_INPUTMANAGER);
269 SysTryReturn(NID_UI, r == E_SUCCESS, null, E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
270 std::unique_ptr<Display, _DisplayDeleter> pDpy(XOpenDisplay(NULL));
271 SysTryReturn(NID_UI, pDpy, null, E_SYSTEM, "[E_SYSTEM] pDpy is invalid. something wrong!");
273 int width = DisplayWidth(pDpy.get(), DefaultScreen(pDpy.get()));
274 int height = DisplayHeight(pDpy.get(), DefaultScreen(pDpy.get()));
276 void* pDump = utilx_create_screen_shot(pDpy.get(), width, height);
277 SysTryReturn(NID_UI, pDump, null, E_SYSTEM, "[E_SYSTEM] pDump is invalid. something wrong!");
279 std::unique_ptr<ByteBuffer> pBuffer(new (std::nothrow) ByteBuffer());
280 SysTryReturn(NID_UI, pBuffer != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failure.");
282 r = pBuffer->Construct(static_cast<byte*>(pDump), 0, width*height*4, width*height*4);
283 SysTryReturn(NID_UI, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
285 Tizen::Graphics::Dimension dim(width, height);
286 std::unique_ptr<Tizen::Graphics::Bitmap> pBitmap(new (std::nothrow) Bitmap());
287 SysTryReturn(NID_UI, pBitmap != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failure.");
289 r = pBitmap->Construct(*pBuffer, dim, BITMAP_PIXEL_FORMAT_ARGB8888);
290 SysTryReturn(NID_UI, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
292 utilx_release_screen_shot();
294 return pBitmap.release();