Revert "[Tizen] Add screen and client rotation itself function"
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / windows / platform-implement-win.cpp
1 /*\r
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.\r
3 *\r
4 * Licensed under the Apache License, Version 2.0 (the "License");\r
5 * you may not use this file except in compliance with the License.\r
6 * You may obtain a copy of the License at\r
7 *\r
8 * http://www.apache.org/licenses/LICENSE-2.0\r
9 *\r
10 * Unless required by applicable law or agreed to in writing, software\r
11 * distributed under the License is distributed on an "AS IS" BASIS,\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13 * See the License for the specific language governing permissions and\r
14 * limitations under the License.\r
15 *\r
16 */\r
17 \r
18 // CLASS HEADER\r
19 #include <dali/internal/window-system/windows/platform-implement-win.h>\r
20 \r
21 // EXTERNAL INCLUDES\r
22 #include <windows.h>\r
23 \r
24 // INTERNAL INCLUDES\r
25 #include <dali/internal/window-system/windows/event-system-win.h>\r
26 \r
27 static constexpr float INCH = 25.4;\r
28 \r
29 using namespace std;\r
30 \r
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace Adaptor
38 {
39 \r
40 namespace WindowsPlatformImplementation\r
41 {\r
42 \r
43 void RunLoop()\r
44 {\r
45   MSG nMsg = { 0 };\r
46 \r
47   while( GetMessage( &nMsg, 0, NULL, NULL ) )\r
48   {\r
49     if( WIN_CALLBACK_EVENT == nMsg.message )\r
50     {\r
51       Dali::CallbackBase *callback = ( Dali::CallbackBase* )nMsg.wParam;\r
52       Dali::CallbackBase::Execute( *callback );\r
53     }\r
54 \r
55     TranslateMessage( &nMsg );\r
56     DispatchMessage( &nMsg );\r
57 \r
58     if( WM_CLOSE == nMsg.message )\r
59     {\r
60       break;\r
61     }\r
62   }\r
63 }\r
64 \r
65 void GetDPI( uint64_t hWnd, float &xDpi, float &yDpi )\r
66 {\r
67   HDC hdcScreen = GetDC( reinterpret_cast<HWND>( hWnd ) );\r
68 \r
69   int32_t iX = GetDeviceCaps( hdcScreen, HORZRES );    // pixel\r
70   int32_t iY = GetDeviceCaps( hdcScreen, VERTRES );    // pixel\r
71   int32_t iPhsX = GetDeviceCaps( hdcScreen, HORZSIZE );    // mm\r
72   int32_t iPhsY = GetDeviceCaps( hdcScreen, VERTSIZE );    // mm\r
73 \r
74   xDpi = static_cast<float>( iX ) / static_cast<float>( iPhsX ) * INCH;\r
75   yDpi = static_cast<float>( iY ) / static_cast<float>( iPhsY ) * INCH;\r
76 }\r
77 \r
78 CallbackBase *listener = NULL;\r
79 \r
80 LRESULT CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)\r
81 {\r
82   if( NULL != listener )\r
83   {\r
84     TWinEventInfo eventInfo( reinterpret_cast<uint64_t>( hWnd ), uMsg, wParam, lParam);\r
85     CallbackBase::Execute( *listener, &eventInfo );\r
86   }\r
87 \r
88   LRESULT ret = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
89   return ret;\r
90 }\r
91 \r
92 DWORD windowStyle = WS_OVERLAPPED;\r
93 \r
94 int32_t GetEdgeWidth()\r
95 {\r
96   switch( windowStyle )\r
97   {\r
98     case WS_OVERLAPPED:\r
99     {\r
100       return 8;\r
101     }\r
102     default:\r
103     {\r
104       return 0;\r
105     }\r
106   }\r
107 }\r
108 \r
109 int32_t GetEdgeHeight()\r
110 {\r
111   switch( windowStyle )\r
112   {\r
113     case WS_OVERLAPPED:\r
114     {\r
115       return 18;\r
116     }\r
117     default:\r
118     {\r
119       return 0;\r
120     }\r
121   }\r
122 }\r
123 \r
124 class WindowsDisplayInfo\r
125 {\r
126 public:\r
127   static int GetColorDepth()\r
128   {\r
129     DALI_ASSERT_DEBUG(colorDepth >= 0 && "HWND hasn't been created, no color depth");\r
130     return colorDepth;\r
131   }\r
132 \r
133   static void SetHWND( HWND inHWnd )\r
134   {\r
135     if( hWnd != inHWnd )\r
136     {\r
137       hWnd = inHWnd;\r
138       hdc = GetDC( hWnd );\r
139       colorDepth = GetDeviceCaps( WindowsDisplayInfo::hdc, BITSPIXEL ) * GetDeviceCaps( WindowsDisplayInfo::hdc, PLANES );\r
140     }\r
141   }\r
142 \r
143 private:\r
144   static int colorDepth;\r
145   static HWND hWnd;\r
146   static HDC hdc;\r
147 };\r
148 \r
149 int WindowsDisplayInfo::colorDepth = -1;\r
150 HWND WindowsDisplayInfo::hWnd = NULL;\r
151 HDC WindowsDisplayInfo::hdc = NULL;\r
152 \r
153 int GetColorDepth()\r
154 {\r
155   return WindowsDisplayInfo::GetColorDepth();\r
156 }\r
157 \r
158 uint64_t CreateHwnd(\r
159     _In_opt_ const char *lpClassName,\r
160     _In_opt_ const char *lpWindowName,\r
161     _In_ int X,\r
162     _In_ int Y,\r
163     _In_ int nWidth,\r
164     _In_ int nHeight,\r
165     _In_opt_ uint64_t parent)\r
166 {\r
167   WNDCLASS cs = { 0 };\r
168   cs.cbClsExtra = 0;\r
169   cs.cbWndExtra = 0;\r
170   cs.hbrBackground = (HBRUSH)( COLOR_WINDOW + 2 );\r
171   cs.hCursor = NULL;\r
172   cs.hIcon = NULL;\r
173   cs.hInstance = GetModuleHandle( NULL );\r
174   cs.lpfnWndProc = (WNDPROC)WinProc;\r
175   cs.lpszClassName = lpClassName;\r
176   cs.lpszMenuName = NULL;\r
177   cs.style = CS_VREDRAW | CS_HREDRAW;\r
178   RegisterClass( &cs );\r
179 \r
180   HWND hWnd = CreateWindow( lpClassName, lpWindowName, windowStyle, X, Y, nWidth + 2 * GetEdgeWidth(), nHeight + 2 * GetEdgeHeight(), NULL, NULL, cs.hInstance, NULL );\r
181   ShowWindow( hWnd, SW_SHOW );\r
182 \r
183   WindowsDisplayInfo::SetHWND( hWnd );\r
184 \r
185   return reinterpret_cast<uint64_t>( hWnd );\r
186 }\r
187 \r
188 void SetListener( CallbackBase *callback )\r
189 {\r
190   listener = callback;\r
191 }\r
192 \r
193 bool PostWinMessage(\r
194     _In_ uint32_t Msg,\r
195     _In_ uint32_t wParam,\r
196     _In_ uint64_t lParam,\r
197     _In_ uint64_t hWnd)\r
198 {\r
199   return (bool)PostMessage( reinterpret_cast<HWND>( hWnd ), Msg, wParam, lParam );\r
200 }\r
201 \r
202 bool PostWinThreadMessage(\r
203     _In_ uint32_t Msg,\r
204     _In_ uint32_t wParam,\r
205     _In_ uint64_t lParam,\r
206     _In_ uint64_t threadID/* = -1*/ )\r
207 {\r
208   if( -1 == threadID )\r
209   {\r
210     threadID = GetCurrentThreadId();\r
211   }\r
212 \r
213   return (bool)PostThreadMessage( threadID, Msg, wParam, lParam );\r
214 }\r
215 \r
216 void ShowWindow( uint64_t hWnd)\r
217 {\r
218   ::ShowWindow( reinterpret_cast<HWND>( hWnd ), SW_SHOW);\r
219 }\r
220 \r
221 void HideWindow( uint64_t hWnd)\r
222 {\r
223   ::ShowWindow( reinterpret_cast<HWND>( hWnd ), SW_HIDE);\r
224 }\r
225 \r
226 struct TTimerCallbackInfo\r
227 {\r
228   void *data;\r
229   timerCallback callback;\r
230   HWND hWnd;\r
231 };\r
232 \r
233 void CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT_PTR nTimerid, DWORD dwTime)\r
234 {\r
235   TTimerCallbackInfo *info = (TTimerCallbackInfo*)nTimerid;\r
236   info->callback( info->data );\r
237 }\r
238 \r
239 int SetTimer(int interval, timerCallback callback, void *data)\r
240 {\r
241   TTimerCallbackInfo *callbackInfo = new TTimerCallbackInfo;\r
242   callbackInfo->data = data;\r
243   callbackInfo->callback = callback;\r
244   callbackInfo->hWnd = ::GetActiveWindow();\r
245 \r
246   UINT_PTR timerID = (UINT_PTR)callbackInfo;\r
247   ::SetTimer( callbackInfo->hWnd, timerID, interval, TimerProc );\r
248 \r
249   return timerID;\r
250 }\r
251 \r
252 void KillTimer(int id)\r
253 {\r
254   TTimerCallbackInfo *info = (TTimerCallbackInfo*)id;\r
255   ::KillTimer( info->hWnd, id );\r
256   delete info;\r
257 }\r
258 \r
259 const char* GetKeyName( int keyCode )\r
260 {\r
261   switch( keyCode )\r
262   {\r
263     case VK_BACK:\r
264     {\r
265       return "Backspace";\r
266     }\r
267     case VK_TAB:\r
268     {\r
269       return "Tab";\r
270     }\r
271     case VK_RETURN:\r
272     {\r
273       return "Return";\r
274     }\r
275     case VK_ESCAPE:\r
276     {\r
277       return "Escape";\r
278     }\r
279     case VK_SPACE:\r
280     {\r
281       return "Space";\r
282     }\r
283     case VK_LEFT:\r
284     {\r
285       return "Left";\r
286     }\r
287     case VK_UP:\r
288     {\r
289       return "Up";\r
290     }\r
291     case VK_RIGHT:\r
292     {\r
293       return "Right";\r
294     }\r
295     case VK_DOWN:\r
296     {\r
297       return "Down";\r
298     }\r
299     case 48:\r
300     {\r
301       return "0";\r
302     }\r
303     case 49:\r
304     {\r
305       return "1";\r
306     }\r
307     case 50:\r
308     {\r
309       return "2";\r
310     }\r
311     case 51:\r
312     {\r
313       return "3";\r
314     }\r
315     case 52:\r
316     {\r
317       return "4";\r
318     }\r
319     case 53:\r
320     {\r
321       return "5";\r
322     }\r
323     case 54:\r
324     {\r
325       return "6";\r
326     }\r
327     case 55:\r
328     {\r
329       return "7";\r
330     }\r
331     case 56:\r
332     {\r
333       return "8";\r
334     }\r
335     case 57:\r
336     {\r
337       return "9";\r
338     }\r
339     default:\r
340     {\r
341       break;\r
342     }\r
343   }\r
344 \r
345   return "";\r
346 }\r
347
348 static LARGE_INTEGER cpuFrequency;\r
349 static LARGE_INTEGER *pCpuFrequency = NULL;\r
350 \r
351 uint64_t GetCurrentThreadId()\r
352 {\r
353   return ::GetCurrentThreadId();\r
354 }\r
355 \r
356 void GetNanoseconds( uint64_t& timeInNanoseconds )\r
357 {\r
358   if( NULL == pCpuFrequency )\r
359   {\r
360     pCpuFrequency = &cpuFrequency;\r
361     QueryPerformanceFrequency( pCpuFrequency );\r
362   }\r
363 \r
364   LARGE_INTEGER curTime;\r
365   QueryPerformanceCounter( &curTime );\r
366 \r
367   timeInNanoseconds = static_cast<double>(curTime.QuadPart) / static_cast<double>(pCpuFrequency->QuadPart) * 1000000000;\r
368 }\r
369 \r
370 unsigned int GetCurrentMilliSeconds( void )\r
371 {\r
372   if( NULL == pCpuFrequency )\r
373   {\r
374     pCpuFrequency = &cpuFrequency;\r
375     QueryPerformanceFrequency( pCpuFrequency );\r
376   }\r
377 \r
378   LARGE_INTEGER curTime;\r
379   QueryPerformanceCounter( &curTime );\r
380 \r
381   return curTime.QuadPart * 1000 / pCpuFrequency->QuadPart;\r
382 }\r
383
384 } // namespace WindowsPlatformImplement
385
386 } // namespace Adaptor
387
388 } // namespace internal
389
390 } // namespace Dali\r