2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
19 #include <dali/internal/window-system/windows/platform-implement-win.h>
\r
21 // EXTERNAL INCLUDES
\r
23 #include <windows.h>
\r
25 // INTERNAL INCLUDES
\r
26 #include <dali/internal/window-system/windows/event-system-win.h>
\r
28 static constexpr float INCH = 25.4;
\r
30 using namespace std;
\r
41 namespace WindowsPlatformImplementation
\r
48 while( GetMessage( &nMsg, 0, NULL, NULL ) )
\r
50 if( WIN_CALLBACK_EVENT == nMsg.message )
\r
52 Dali::CallbackBase *callback = ( Dali::CallbackBase* )nMsg.wParam;
\r
53 Dali::CallbackBase::Execute( *callback );
\r
56 TranslateMessage( &nMsg );
\r
57 DispatchMessage( &nMsg );
\r
59 if( WM_CLOSE == nMsg.message )
\r
66 LRESULT CALLBACK WinProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
\r
68 WindowImpl::ProcWinMessge( reinterpret_cast<uint64_t>( hWnd ), uMsg, wParam, lParam );
\r
70 LRESULT ret = DefWindowProc( hWnd, uMsg, wParam, lParam );
\r
74 std::map<uint64_t, WindowImpl*> mHWndToListener;
\r
76 WindowImpl::WindowImpl()
\r
82 windowStyle = WS_OVERLAPPED;
\r
85 WindowImpl::~WindowImpl()
\r
87 mHWndToListener.erase( mHWnd );
\r
90 void WindowImpl::ProcWinMessge( uint64_t hWnd, uint32_t uMsg, uint64_t wParam, uint64_t lParam )
\r
92 std::map<uint64_t, WindowImpl*>::iterator x = mHWndToListener.find( hWnd );
\r
94 if( mHWndToListener.end() != x )
\r
96 CallbackBase* listener = x->second->listener;
\r
98 if( NULL != listener )
\r
100 TWinEventInfo eventInfo( hWnd, uMsg, wParam, lParam );
\r
101 CallbackBase::Execute( *listener, &eventInfo );
\r
106 void WindowImpl::GetDPI( float &xDpi, float &yDpi )
\r
108 HDC hdcScreen = GetDC( reinterpret_cast<HWND>( mHWnd ) );
\r
110 int32_t iX = GetDeviceCaps( hdcScreen, HORZRES ); // pixel
\r
111 int32_t iY = GetDeviceCaps( hdcScreen, VERTRES ); // pixel
\r
112 int32_t iPhsX = GetDeviceCaps( hdcScreen, HORZSIZE ); // mm
\r
113 int32_t iPhsY = GetDeviceCaps( hdcScreen, VERTSIZE ); // mm
\r
115 xDpi = static_cast<float>( iX ) / static_cast<float>( iPhsX ) * INCH;
\r
116 yDpi = static_cast<float>( iY ) / static_cast<float>( iPhsY ) * INCH;
\r
119 int WindowImpl::GetColorDepth()
\r
121 DALI_ASSERT_DEBUG( colorDepth >= 0 && "HWND hasn't been created, no color depth" );
\r
125 uint64_t WindowImpl::CreateHwnd(
\r
126 _In_opt_ const char *lpClassName,
\r
127 _In_opt_ const char *lpWindowName,
\r
132 _In_opt_ uint64_t parent )
\r
134 WNDCLASS cs = { 0 };
\r
137 cs.hbrBackground = (HBRUSH)( COLOR_WINDOW + 2 );
\r
140 cs.hInstance = GetModuleHandle( NULL );
\r
141 cs.lpfnWndProc = (WNDPROC)WinProc;
\r
142 cs.lpszClassName = lpClassName;
\r
143 cs.lpszMenuName = NULL;
\r
144 cs.style = CS_VREDRAW | CS_HREDRAW;
\r
145 RegisterClass( &cs );
\r
147 HWND hWnd = CreateWindow( lpClassName, lpWindowName, windowStyle, X, Y, nWidth + 2 * GetEdgeWidth(), nHeight + 2 * GetEdgeHeight(), NULL, NULL, cs.hInstance, NULL );
\r
148 ::ShowWindow( hWnd, SW_SHOW );
\r
150 SetHWND( reinterpret_cast<uint64_t>(hWnd) );
\r
155 void WindowImpl::SetListener( CallbackBase *callback )
\r
157 listener = callback;
\r
160 bool WindowImpl::PostWinMessage(
\r
162 _In_ uint32_t wParam,
\r
163 _In_ uint64_t lParam )
\r
165 return (bool)PostMessage( reinterpret_cast<HWND>( mHWnd ), Msg, wParam, lParam );
\r
168 int32_t WindowImpl::GetEdgeWidth()
\r
170 switch( windowStyle )
\r
172 case WS_OVERLAPPED:
\r
183 int32_t WindowImpl::GetEdgeHeight()
\r
185 switch( windowStyle )
\r
187 case WS_OVERLAPPED:
\r
198 void WindowImpl::SetHWND( uint64_t inHWnd )
\r
200 if( mHWnd != inHWnd )
\r
203 mHdc = reinterpret_cast<uint64_t>( GetDC( reinterpret_cast<HWND>( mHWnd ) ) );
\r
204 colorDepth = GetDeviceCaps( reinterpret_cast<HDC>( mHdc ), BITSPIXEL ) * GetDeviceCaps( reinterpret_cast<HDC>( mHdc ), PLANES );
\r
206 std::map<uint64_t, WindowImpl*>::iterator x = mHWndToListener.find( mHWnd );
\r
208 if( mHWndToListener.end() == x )
\r
210 mHWndToListener.insert( std::make_pair( mHWnd, this ) );
\r
219 bool PostWinThreadMessage(
\r
221 _In_ uint32_t wParam,
\r
222 _In_ uint64_t lParam,
\r
223 _In_ uint64_t threadID/* = -1*/ )
\r
225 if( -1 == threadID )
\r
227 threadID = GetCurrentThreadId();
\r
230 return (bool)PostThreadMessage( threadID, Msg, wParam, lParam );
\r
233 struct TTimerCallbackInfo
\r
236 timerCallback callback;
\r
240 void CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT_PTR nTimerid, DWORD dwTime)
\r
242 TTimerCallbackInfo *info = (TTimerCallbackInfo*)nTimerid;
\r
243 info->callback( info->data );
\r
246 int SetTimer(int interval, timerCallback callback, void *data)
\r
248 TTimerCallbackInfo *callbackInfo = new TTimerCallbackInfo;
\r
249 callbackInfo->data = data;
\r
250 callbackInfo->callback = callback;
\r
251 callbackInfo->hWnd = ::GetActiveWindow();
\r
253 UINT_PTR timerID = (UINT_PTR)callbackInfo;
\r
254 ::SetTimer( callbackInfo->hWnd, timerID, interval, TimerProc );
\r
259 void KillTimer(int id)
\r
261 TTimerCallbackInfo *info = (TTimerCallbackInfo*)id;
\r
262 ::KillTimer( info->hWnd, id );
\r
266 const char* GetKeyName( int keyCode )
\r
272 return "Backspace";
\r
355 static LARGE_INTEGER cpuFrequency;
\r
356 static LARGE_INTEGER *pCpuFrequency = NULL;
\r
358 uint64_t GetCurrentThreadId()
\r
360 return ::GetCurrentThreadId();
\r
363 void GetNanoseconds( uint64_t& timeInNanoseconds )
\r
365 if( NULL == pCpuFrequency )
\r
367 pCpuFrequency = &cpuFrequency;
\r
368 QueryPerformanceFrequency( pCpuFrequency );
\r
371 LARGE_INTEGER curTime;
\r
372 QueryPerformanceCounter( &curTime );
\r
374 timeInNanoseconds = static_cast<double>(curTime.QuadPart) / static_cast<double>(pCpuFrequency->QuadPart) * 1000000000;
\r
377 unsigned int GetCurrentMilliSeconds( void )
\r
379 if( NULL == pCpuFrequency )
\r
381 pCpuFrequency = &cpuFrequency;
\r
382 QueryPerformanceFrequency( pCpuFrequency );
\r
385 LARGE_INTEGER curTime;
\r
386 QueryPerformanceCounter( &curTime );
\r
388 return curTime.QuadPart * 1000 / pCpuFrequency->QuadPart;
\r
391 } // namespace WindowsPlatformImplement
393 } // namespace Adaptor
395 } // namespace internal
397 } // namespace Dali
\r