2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/window-system/windows/platform-implement-win.h>
25 #include <dali/internal/window-system/windows/event-system-win.h>
27 static constexpr float INCH = 25.4;
40 namespace WindowsPlatformImplementation
47 while( GetMessage( &nMsg, 0, NULL, NULL ) )
49 if( WIN_CALLBACK_EVENT == nMsg.message )
51 Dali::CallbackBase *callback = ( Dali::CallbackBase* )nMsg.wParam;
52 Dali::CallbackBase::Execute( *callback );
55 TranslateMessage( &nMsg );
56 DispatchMessage( &nMsg );
58 if( WM_CLOSE == nMsg.message )
65 void GetDPI( uint64_t hWnd, float &xDpi, float &yDpi )
67 HDC hdcScreen = GetDC( reinterpret_cast<HWND>( hWnd ) );
69 int32_t iX = GetDeviceCaps( hdcScreen, HORZRES ); // pixel
70 int32_t iY = GetDeviceCaps( hdcScreen, VERTRES ); // pixel
71 int32_t iPhsX = GetDeviceCaps( hdcScreen, HORZSIZE ); // mm
72 int32_t iPhsY = GetDeviceCaps( hdcScreen, VERTSIZE ); // mm
74 xDpi = static_cast<float>( iX ) / static_cast<float>( iPhsX ) * INCH;
75 yDpi = static_cast<float>( iY ) / static_cast<float>( iPhsY ) * INCH;
83 CallbackBase *listener = NULL;
85 LRESULT CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
87 if( NULL != listener )
89 TWinEventInfo eventInfo( reinterpret_cast<uint64_t>( hWnd ), uMsg, wParam, lParam);
90 CallbackBase::Execute( *listener, &eventInfo );
93 LRESULT ret = DefWindowProc( hWnd, uMsg, wParam, lParam );
97 DWORD windowStyle = WS_OVERLAPPED;
99 int32_t GetEdgeWidth()
101 switch( windowStyle )
114 int32_t GetEdgeHeight()
116 switch( windowStyle )
129 class WindowsDisplayInfo
132 static int GetColorDepth()
134 DALI_ASSERT_DEBUG(colorDepth >= 0 && "HWND hasn't been created, no color depth");
138 static void SetHWND( HWND inHWnd )
144 colorDepth = GetDeviceCaps( WindowsDisplayInfo::hdc, BITSPIXEL ) * GetDeviceCaps( WindowsDisplayInfo::hdc, PLANES );
149 static int colorDepth;
154 int WindowsDisplayInfo::colorDepth = -1;
155 HWND WindowsDisplayInfo::hWnd = NULL;
156 HDC WindowsDisplayInfo::hdc = NULL;
160 return WindowsDisplayInfo::GetColorDepth();
164 _In_opt_ const char *lpClassName,
165 _In_opt_ const char *lpWindowName,
170 _In_opt_ uint64_t parent)
175 cs.hbrBackground = (HBRUSH)( COLOR_WINDOW + 2 );
178 cs.hInstance = GetModuleHandle( NULL );
179 cs.lpfnWndProc = (WNDPROC)WinProc;
180 cs.lpszClassName = lpClassName;
181 cs.lpszMenuName = NULL;
182 cs.style = CS_VREDRAW | CS_HREDRAW;
183 RegisterClass( &cs );
185 HWND hWnd = CreateWindow( lpClassName, lpWindowName, windowStyle, X, Y, nWidth + 2 * GetEdgeWidth(), nHeight + 2 * GetEdgeHeight(), NULL, NULL, cs.hInstance, NULL );
186 ShowWindow( hWnd, SW_SHOW );
188 WindowsDisplayInfo::SetHWND( hWnd );
190 return reinterpret_cast<uint64_t>( hWnd );
193 void SetListener( CallbackBase *callback )
200 _In_ uint32_t wParam,
201 _In_ uint64_t lParam,
204 return (bool)PostMessage( reinterpret_cast<HWND>( hWnd ), Msg, wParam, lParam );
207 bool PostWinThreadMessage(
209 _In_ uint32_t wParam,
210 _In_ uint64_t lParam,
211 _In_ uint64_t threadID/* = -1*/ )
215 threadID = GetCurrentThreadId();
218 return (bool)PostThreadMessage( threadID, Msg, wParam, lParam );
221 void ShowWindow( uint64_t hWnd)
223 ::ShowWindow( reinterpret_cast<HWND>( hWnd ), SW_SHOW);
226 void HideWindow( uint64_t hWnd)
228 ::ShowWindow( reinterpret_cast<HWND>( hWnd ), SW_HIDE);
231 struct TTimerCallbackInfo
234 timerCallback callback;
238 void CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT_PTR nTimerid, DWORD dwTime)
240 TTimerCallbackInfo *info = (TTimerCallbackInfo*)nTimerid;
241 info->callback( info->data );
244 int SetTimer(int interval, timerCallback callback, void *data)
246 TTimerCallbackInfo *callbackInfo = new TTimerCallbackInfo;
247 callbackInfo->data = data;
248 callbackInfo->callback = callback;
249 callbackInfo->hWnd = ::GetActiveWindow();
251 UINT_PTR timerID = (UINT_PTR)callbackInfo;
252 ::SetTimer( callbackInfo->hWnd, timerID, interval, TimerProc );
257 void KillTimer(int id)
259 TTimerCallbackInfo *info = (TTimerCallbackInfo*)id;
260 ::KillTimer( info->hWnd, id );
264 const char* GetKeyName( int keyCode )
353 static LARGE_INTEGER cpuFrequency;
354 static LARGE_INTEGER *pCpuFrequency = NULL;
356 uint64_t GetCurrentThreadId()
358 return ::GetCurrentThreadId();
361 void GetNanoseconds( uint64_t& timeInNanoseconds )
363 if( NULL == pCpuFrequency )
365 pCpuFrequency = &cpuFrequency;
366 QueryPerformanceFrequency( pCpuFrequency );
369 LARGE_INTEGER curTime;
370 QueryPerformanceCounter( &curTime );
372 timeInNanoseconds = static_cast<double>(curTime.QuadPart) / static_cast<double>(pCpuFrequency->QuadPart) * 1000000000;
375 unsigned int GetCurrentMilliSeconds( void )
377 if( NULL == pCpuFrequency )
379 pCpuFrequency = &cpuFrequency;
380 QueryPerformanceFrequency( pCpuFrequency );
383 LARGE_INTEGER curTime;
384 QueryPerformanceCounter( &curTime );
386 return curTime.QuadPart * 1000 / pCpuFrequency->QuadPart;
389 } // namespace WindowsPlatformImplement
391 } // namespace Adaptor
393 } // namespace internal