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>
30 static constexpr float INCH = 25.4;
42 namespace WindowsPlatformImplementation
45 LRESULT CALLBACK WinProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
47 WindowImpl::ProcWinMessage( reinterpret_cast<uint64_t>( hWnd ), uMsg, wParam, lParam );
49 LRESULT ret = DefWindowProc( hWnd, uMsg, wParam, lParam );
53 std::map<uint64_t, WindowImpl*> mHWndToListener;
55 WindowImpl::WindowImpl()
61 windowStyle = WS_OVERLAPPED;
64 WindowImpl::~WindowImpl()
66 mHWndToListener.erase( mHWnd );
69 void WindowImpl::ProcWinMessage( uint64_t hWnd, uint32_t uMsg, uint64_t wParam, uint64_t lParam )
71 std::map<uint64_t, WindowImpl*>::iterator x = mHWndToListener.find( hWnd );
73 if( mHWndToListener.end() != x )
75 CallbackBase* listener = x->second->listener;
77 if( NULL != listener )
79 TWinEventInfo eventInfo( hWnd, uMsg, wParam, lParam );
80 CallbackBase::Execute( *listener, &eventInfo );
85 void WindowImpl::GetDPI( float &xDpi, float &yDpi )
87 HDC hdcScreen = GetDC( reinterpret_cast<HWND>( mHWnd ) );
89 int32_t iX = GetDeviceCaps( hdcScreen, HORZRES ); // pixel
90 int32_t iY = GetDeviceCaps( hdcScreen, VERTRES ); // pixel
91 int32_t iPhsX = GetDeviceCaps( hdcScreen, HORZSIZE ); // mm
92 int32_t iPhsY = GetDeviceCaps( hdcScreen, VERTSIZE ); // mm
94 xDpi = static_cast<float>( iX ) / static_cast<float>( iPhsX ) * INCH;
95 yDpi = static_cast<float>( iY ) / static_cast<float>( iPhsY ) * INCH;
98 int WindowImpl::GetColorDepth()
100 DALI_ASSERT_DEBUG( colorDepth >= 0 && "HWND hasn't been created, no color depth" );
104 uint64_t WindowImpl::CreateHwnd(
105 _In_opt_ const char *lpClassName,
106 _In_opt_ const char *lpWindowName,
111 _In_opt_ uint64_t parent )
116 cs.hbrBackground = (HBRUSH)( COLOR_WINDOW + 2 );
119 cs.hInstance = GetModuleHandle( NULL );
120 cs.lpfnWndProc = (WNDPROC)WinProc;
121 cs.lpszClassName = lpClassName;
122 cs.lpszMenuName = NULL;
123 cs.style = CS_VREDRAW | CS_HREDRAW;
124 RegisterClass( &cs );
126 HWND hWnd = CreateWindow( lpClassName, lpWindowName, windowStyle, X, Y, nWidth + 2 * GetEdgeWidth(), nHeight + 2 * GetEdgeHeight(), NULL, NULL, cs.hInstance, NULL );
127 ::ShowWindow( hWnd, SW_SHOW );
129 SetHWND( reinterpret_cast<uint64_t>(hWnd) );
134 void WindowImpl::SetListener( CallbackBase *callback )
139 bool WindowImpl::PostWinMessage(
141 _In_ uint64_t wParam,
142 _In_ uint64_t lParam )
144 return (bool)PostMessage( reinterpret_cast<HWND>( mHWnd ), Msg, wParam, lParam );
147 int32_t WindowImpl::GetEdgeWidth()
149 switch( windowStyle )
162 int32_t WindowImpl::GetEdgeHeight()
164 switch( windowStyle )
177 void WindowImpl::SetHWND( uint64_t inHWnd )
182 mHdc = reinterpret_cast<uint64_t>(GetDC(reinterpret_cast<HWND>(mHWnd)));
183 colorDepth = GetDeviceCaps(reinterpret_cast<HDC>(mHdc), BITSPIXEL) * GetDeviceCaps(reinterpret_cast<HDC>(mHdc), PLANES);
185 std::map<uint64_t, WindowImpl*>::iterator x = mHWndToListener.find(mHWnd);
187 if (mHWndToListener.end() == x)
189 mHWndToListener.insert(std::make_pair(mHWnd, this));
198 void WindowImpl::SetWinProc()
200 // Sets the WinProc function.
201 LONG_PTR ret = SetWindowLongPtr((HWND)mHWnd,
203 reinterpret_cast<LONG_PTR>(&WinProc));
207 DWORD error = GetLastError();
211 HMODULE module = GetModuleHandle(nullptr);
212 ret = SetWindowLongPtr((HWND)mHWnd,
214 reinterpret_cast<LONG_PTR>(&module));
217 bool PostWinThreadMessage(
219 _In_ uint64_t wParam,
220 _In_ uint64_t lParam,
221 _In_ uint64_t threadID/* = -1*/ )
225 threadID = GetCurrentThreadId();
228 return (bool)PostThreadMessage( threadID, Msg, wParam, lParam );
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 intptr_t 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 INT_PTR timerID = (INT_PTR)callbackInfo;
252 ::SetTimer( callbackInfo->hWnd, timerID, interval, TimerProc );
257 void KillTimer(intptr_t 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