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>
26 #include <dali/internal/window-system/windows/event-system-win.h>
28 static constexpr float INCH = 25.4;
41 namespace WindowsPlatformImplementation
48 while( GetMessage( &nMsg, 0, NULL, NULL ) )
50 if( WIN_CALLBACK_EVENT == nMsg.message )
52 Dali::CallbackBase *callback = ( Dali::CallbackBase* )nMsg.wParam;
53 Dali::CallbackBase::Execute( *callback );
56 TranslateMessage( &nMsg );
57 DispatchMessage( &nMsg );
59 if( WM_CLOSE == nMsg.message )
66 LRESULT CALLBACK WinProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
68 WindowImpl::ProcWinMessge( reinterpret_cast<uint64_t>( hWnd ), uMsg, wParam, lParam );
70 LRESULT ret = DefWindowProc( hWnd, uMsg, wParam, lParam );
74 std::map<uint64_t, WindowImpl*> mHWndToListener;
76 WindowImpl::WindowImpl()
82 windowStyle = WS_OVERLAPPED;
85 WindowImpl::~WindowImpl()
87 mHWndToListener.erase( mHWnd );
90 void WindowImpl::ProcWinMessge( uint64_t hWnd, uint32_t uMsg, uint64_t wParam, uint64_t lParam )
92 std::map<uint64_t, WindowImpl*>::iterator x = mHWndToListener.find( hWnd );
94 if( mHWndToListener.end() != x )
96 CallbackBase* listener = x->second->listener;
98 if( NULL != listener )
100 TWinEventInfo eventInfo( hWnd, uMsg, wParam, lParam );
101 CallbackBase::Execute( *listener, &eventInfo );
106 void WindowImpl::GetDPI( float &xDpi, float &yDpi )
108 HDC hdcScreen = GetDC( reinterpret_cast<HWND>( mHWnd ) );
110 int32_t iX = GetDeviceCaps( hdcScreen, HORZRES ); // pixel
111 int32_t iY = GetDeviceCaps( hdcScreen, VERTRES ); // pixel
112 int32_t iPhsX = GetDeviceCaps( hdcScreen, HORZSIZE ); // mm
113 int32_t iPhsY = GetDeviceCaps( hdcScreen, VERTSIZE ); // mm
115 xDpi = static_cast<float>( iX ) / static_cast<float>( iPhsX ) * INCH;
116 yDpi = static_cast<float>( iY ) / static_cast<float>( iPhsY ) * INCH;
119 int WindowImpl::GetColorDepth()
121 DALI_ASSERT_DEBUG( colorDepth >= 0 && "HWND hasn't been created, no color depth" );
125 uint64_t WindowImpl::CreateHwnd(
126 _In_opt_ const char *lpClassName,
127 _In_opt_ const char *lpWindowName,
132 _In_opt_ uint64_t parent )
137 cs.hbrBackground = (HBRUSH)( COLOR_WINDOW + 2 );
140 cs.hInstance = GetModuleHandle( NULL );
141 cs.lpfnWndProc = (WNDPROC)WinProc;
142 cs.lpszClassName = lpClassName;
143 cs.lpszMenuName = NULL;
144 cs.style = CS_VREDRAW | CS_HREDRAW;
145 RegisterClass( &cs );
147 HWND hWnd = CreateWindow( lpClassName, lpWindowName, windowStyle, X, Y, nWidth + 2 * GetEdgeWidth(), nHeight + 2 * GetEdgeHeight(), NULL, NULL, cs.hInstance, NULL );
148 ::ShowWindow( hWnd, SW_SHOW );
150 SetHWND( reinterpret_cast<uint64_t>(hWnd) );
155 void WindowImpl::SetListener( CallbackBase *callback )
160 bool WindowImpl::PostWinMessage(
162 _In_ uint32_t wParam,
163 _In_ uint64_t lParam )
165 return (bool)PostMessage( reinterpret_cast<HWND>( mHWnd ), Msg, wParam, lParam );
168 int32_t WindowImpl::GetEdgeWidth()
170 switch( windowStyle )
183 int32_t WindowImpl::GetEdgeHeight()
185 switch( windowStyle )
198 void WindowImpl::SetHWND( uint64_t inHWnd )
203 mHdc = reinterpret_cast<uint64_t>(GetDC(reinterpret_cast<HWND>(mHWnd)));
204 colorDepth = GetDeviceCaps(reinterpret_cast<HDC>(mHdc), BITSPIXEL) * GetDeviceCaps(reinterpret_cast<HDC>(mHdc), PLANES);
206 std::map<uint64_t, WindowImpl*>::iterator x = mHWndToListener.find(mHWnd);
208 if (mHWndToListener.end() == x)
210 mHWndToListener.insert(std::make_pair(mHWnd, this));
219 void WindowImpl::SetWinProc()
221 // Sets the WinProc function.
222 LONG_PTR ret = SetWindowLongPtr((HWND)mHWnd,
224 reinterpret_cast<LONG_PTR>(&WinProc));
228 DWORD error = GetLastError();
232 HMODULE module = GetModuleHandle(nullptr);
233 ret = SetWindowLongPtr((HWND)mHWnd,
235 reinterpret_cast<LONG_PTR>(&module));
238 bool PostWinThreadMessage(
240 _In_ uint32_t wParam,
241 _In_ uint64_t lParam,
242 _In_ uint64_t threadID/* = -1*/ )
246 threadID = GetCurrentThreadId();
249 return (bool)PostThreadMessage( threadID, Msg, wParam, lParam );
252 struct TTimerCallbackInfo
255 timerCallback callback;
259 void CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT_PTR nTimerid, DWORD dwTime)
261 TTimerCallbackInfo *info = (TTimerCallbackInfo*)nTimerid;
262 info->callback( info->data );
265 int SetTimer(int interval, timerCallback callback, void *data)
267 TTimerCallbackInfo *callbackInfo = new TTimerCallbackInfo;
268 callbackInfo->data = data;
269 callbackInfo->callback = callback;
270 callbackInfo->hWnd = ::GetActiveWindow();
272 UINT_PTR timerID = (UINT_PTR)callbackInfo;
273 ::SetTimer( callbackInfo->hWnd, timerID, interval, TimerProc );
278 void KillTimer(int id)
280 TTimerCallbackInfo *info = (TTimerCallbackInfo*)id;
281 ::KillTimer( info->hWnd, id );
285 const char* GetKeyName( int keyCode )
374 static LARGE_INTEGER cpuFrequency;
375 static LARGE_INTEGER *pCpuFrequency = NULL;
377 uint64_t GetCurrentThreadId()
379 return ::GetCurrentThreadId();
382 void GetNanoseconds( uint64_t& timeInNanoseconds )
384 if( NULL == pCpuFrequency )
386 pCpuFrequency = &cpuFrequency;
387 QueryPerformanceFrequency( pCpuFrequency );
390 LARGE_INTEGER curTime;
391 QueryPerformanceCounter( &curTime );
393 timeInNanoseconds = static_cast<double>(curTime.QuadPart) / static_cast<double>(pCpuFrequency->QuadPart) * 1000000000;
396 unsigned int GetCurrentMilliSeconds( void )
398 if( NULL == pCpuFrequency )
400 pCpuFrequency = &cpuFrequency;
401 QueryPerformanceFrequency( pCpuFrequency );
404 LARGE_INTEGER curTime;
405 QueryPerformanceCounter( &curTime );
407 return curTime.QuadPart * 1000 / pCpuFrequency->QuadPart;
410 } // namespace WindowsPlatformImplement
412 } // namespace Adaptor
414 } // namespace internal