[Tizen] Add a flag to check whether it is forced to enable
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / windows / platform-implement-win.cpp
index f122a43..3e51425 100755 (executable)
@@ -1,13 +1,35 @@
-// CLASS HEADER\r
-#include <dali/internal/window-system/windows/platform-implement-win.h>\r
-\r
-// EXTERNAL_HEADERS\r
-#include <windows.h>\r
-#include <map>\r
-#include <set>\r
-\r
-using namespace std;\r
-\r
+/*
+* Copyright (c) 2018 Samsung Electronics Co., Ltd.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*
+*/
+
+// CLASS HEADER
+#include <dali/internal/window-system/windows/platform-implement-win.h>
+
+// EXTERNAL INCLUDES
+#include <map>
+#include <windows.h>
+
+// INTERNAL INCLUDES
+#include <dali/internal/window-system/windows/event-system-win.h>
+
+namespace
+{
+constexpr float INCH = 25.4;
+}
+
 namespace Dali
 {
 
@@ -16,378 +38,371 @@ namespace Internal
 
 namespace Adaptor
 {
-\r
-namespace WindowsPlatformImplement\r
-{\r
-\r
-HWND roothWnd = NULL;\r
-\r
-void RunLoop()\r
-{\r
-  MSG nMsg = { 0 };\r
-\r
-  while( GetMessage( &nMsg, 0, NULL, NULL ) )\r
-  {\r
-    TranslateMessage( &nMsg );\r
-    DispatchMessage( &nMsg );\r
-\r
-#ifdef _WIN32\r
-    //::Sleep( 20 );\r
-#endif\r
-\r
-    if( WM_CLOSE == nMsg.message )\r
-    {\r
-      break;\r
-    }\r
-  }\r
-}\r
-\r
-void SetParent(long child, long parent)\r
-{\r
-    if (roothWnd == (HWND)child && NULL != (HWND)parent)\r
-    {\r
-        roothWnd = (HWND)parent;\r
-    }\r
-\r
-    ::SetParent((HWND)child, (HWND)parent);\r
-}\r
-\r
-#define INCH 25.4\r
-\r
-void GetDPI(float &xDpi, float &yDpi)\r
-{\r
-  HDC hdcScreen = GetDC(roothWnd);\r
-\r
-  int iX = GetDeviceCaps( hdcScreen, HORZRES );    // pixel\r
-  int iY = GetDeviceCaps( hdcScreen, VERTRES );    // pixel\r
-  int iPhsX = GetDeviceCaps( hdcScreen, HORZSIZE );    // mm\r
-  int iPhsY = GetDeviceCaps( hdcScreen, VERTSIZE );    // mm\r
-\r
-  xDpi = (float)iX / (float)iPhsX * INCH;\r
-  yDpi = (float)iY / (float)iPhsY * INCH;\r
-}\r
-\r
-map<int, set<Dali::CallbackBase*>> callbackMap;\r
-set<winEventCallback> listenerSet;\r
-\r
-LRESULT CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)\r
-{\r
-  for( set<winEventCallback>::iterator x = listenerSet.begin(); x != listenerSet.end(); ++x )\r
-  {\r
-    ( *x )( (long)hWnd, (long)uMsg, (long)wParam, (long)lParam );\r
-  }\r
-\r
-  {\r
-  map<int, set<Dali::CallbackBase*>>::iterator x = callbackMap.find( uMsg );\r
-\r
-  if( callbackMap.end() != x )\r
-  {\r
-    set<Dali::CallbackBase*> &cbSet = x->second;\r
-    for( set<Dali::CallbackBase*>::iterator y = cbSet.begin(); cbSet.end() != y; ++y )\r
-    {\r
-      Dali::CallbackBase::Execute(**y);\r
-    }\r
-  }\r
-  }\r
-\r
-  LRESULT ret = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
-  return ret;\r
-}\r
-\r
-DWORD windowStyle = WS_OVERLAPPED;\r
-\r
-int GetEdgeWidth()\r
-{\r
-  switch( windowStyle )\r
-  {\r
-  case WS_OVERLAPPED:\r
-    return 8;\r
-    break;\r
-\r
-  default:\r
-    return 0;\r
-    break;\r
-  }\r
-}\r
-\r
-int GetEdgeHeight()\r
-{\r
-  switch( windowStyle )\r
-  {\r
-  case WS_OVERLAPPED:\r
-    return 18;\r
-    break;\r
-\r
-  default:\r
-    return 0;\r
-    break;\r
-  }\r
-}\r
-\r
-long CreateHwnd(\r
-    _In_opt_ const char *lpClassName,\r
-    _In_opt_ const char *lpWindowName,\r
-    _In_ int X,\r
-    _In_ int Y,\r
-    _In_ int nWidth,\r
-    _In_ int nHeight,\r
-    _In_opt_ long parent)\r
-{\r
-  WNDCLASS cs = { 0 };\r
-  cs.cbClsExtra = 0;\r
-  cs.cbWndExtra = 0;\r
-  cs.hbrBackground = (HBRUSH)( COLOR_WINDOW + 2 );\r
-  cs.hCursor = NULL;\r
-  cs.hIcon = NULL;\r
-  cs.hInstance = GetModuleHandle( NULL );\r
-  cs.lpfnWndProc = (WNDPROC)WinProc;\r
-  cs.lpszClassName = lpClassName;\r
-  cs.lpszMenuName = NULL;\r
-  cs.style = CS_VREDRAW | CS_HREDRAW;\r
-  RegisterClass( &cs );//ʵÏÖ×¢²á´°¿Ú\r
-\r
-  HWND hWnd = CreateWindow( lpClassName, lpWindowName, windowStyle, X, Y, nWidth + 2 * GetEdgeWidth(), nHeight + 2 * GetEdgeHeight(), NULL, NULL, cs.hInstance, NULL );\r
-  ShowWindow( hWnd, SW_SHOW );\r
-\r
-  if( hWnd != roothWnd )\r
-  {\r
-    roothWnd = hWnd;\r
-  }\r
-\r
-  return (long)hWnd;\r
-}\r
-\r
-void AddListener(winEventCallback callback)\r
-{\r
-  listenerSet.insert(callback);\r
-}\r
-\r
-void AddListener( int msgType, Dali::CallbackBase *callback )\r
-{\r
-  map<int, set<Dali::CallbackBase*>>::iterator x = callbackMap.find( msgType );\r
-  if( callbackMap.end() == x )\r
-  {\r
-    set<Dali::CallbackBase*> callbackSet;\r
-    callbackSet.insert( callback );\r
-\r
-    callbackMap.insert( make_pair( msgType, callbackSet ) );\r
-  }\r
-  else\r
-  {\r
-    set<Dali::CallbackBase*> &callbackSet = x->second;\r
-    set<Dali::CallbackBase*>::iterator y = callbackSet.find( callback );\r
-    if( callbackSet.end() == y )\r
-    {\r
-      callbackSet.insert( callback );\r
-    }\r
-  }\r
-}\r
-\r
-void RemoveListener(int msgType)\r
-{\r
-  callbackMap.erase(msgType);\r
-}\r
-\r
-bool PostWinMessage(\r
-    _In_ unsigned int Msg,\r
-    _In_ long wParam,\r
-    _In_ long lParam,\r
-    _In_ long hWnd/* = 0*/)\r
-{\r
-  if( 0 == hWnd )\r
-  {\r
-    return (bool)PostMessage( roothWnd, Msg, wParam, lParam );\r
-  }\r
-  else\r
-  {\r
-    return (bool)PostMessage( (HWND)hWnd, Msg, wParam, lParam );\r
-  }\r
-}\r
-\r
-void ShowWindow(long hWnd)\r
-{\r
-  ::ShowWindow((HWND)hWnd, SW_SHOW);\r
-}\r
-\r
-void HideWindow(long hWnd)\r
-{\r
-  ::ShowWindow((HWND)hWnd, SW_HIDE);\r
-}\r
-\r
-long CreateWinSemaphore(_In_ long lInitialCount, _In_ long lMaximumCount)\r
-{\r
-  return (long)::CreateSemaphore(NULL, lInitialCount, lMaximumCount, NULL);\r
-}\r
-\r
-unsigned long WaitForSingleObject(_In_ long hHandle, _In_ unsigned long dwMilliseconds)\r
-{\r
-  return ::WaitForSingleObject((HWND)hHandle, dwMilliseconds);\r
-}\r
-\r
-bool ReleaseSemaphore(_In_ long hSemaphore, _In_ long lReleaseCount, _Out_opt_ long *lpPreviousCount)\r
-{\r
-  return ::ReleaseSemaphore((HWND)hSemaphore, lReleaseCount, lpPreviousCount);\r
-}\r
-\r
-struct TTimerCallbackInfo\r
-{\r
-  void *data;\r
-  timerCallback callback;\r
-};\r
-\r
-map<int, TTimerCallbackInfo> mapTimerIDToData;\r
-\r
-void CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT_PTR nTimerid, DWORD dwTime)\r
-{\r
-  map<int, TTimerCallbackInfo>::iterator x = mapTimerIDToData.find( nTimerid );\r
-\r
-  if( mapTimerIDToData.end() == x )\r
-  {\r
-    return;\r
-  }\r
-\r
-  TTimerCallbackInfo &info = x->second;\r
-\r
-  info.callback( info.data );\r
-}\r
-\r
-int SetTimer(int interval, timerCallback callback, void *data)\r
-{\r
-  int timerID = 0;\r
-\r
-  for( map<int, TTimerCallbackInfo>::iterator x = mapTimerIDToData.begin(); mapTimerIDToData.end() != x; ++x )\r
-  {\r
-    int id = x->first;\r
-\r
-    if( timerID < id )\r
-    {\r
-      break;\r
-    }\r
-    else\r
-    {\r
-      timerID++;\r
-    }\r
-  }\r
-\r
-  TTimerCallbackInfo callbackInfo;\r
-  callbackInfo.data = data;\r
-  callbackInfo.callback = callback;\r
-\r
-  mapTimerIDToData.insert( make_pair( timerID, callbackInfo ) );\r
-\r
-  ::SetTimer( roothWnd, timerID, interval, TimerProc );\r
-\r
-  return timerID;\r
-}\r
-\r
-void KillTimer(int id)\r
-{\r
-  mapTimerIDToData.erase(id);\r
-  ::KillTimer(roothWnd, id);\r
-}\r
-\r
-const char* GetKeyName( int keyCode )\r
-{\r
-  switch( keyCode )\r
-  {\r
-  case VK_BACK:\r
-    return "Backspace";\r
-\r
-  case VK_TAB:\r
-    return "Tab";\r
-\r
-  case VK_RETURN:\r
-    return "Return";\r
-\r
-  case VK_ESCAPE:\r
-    return "Escape";\r
-\r
-  case VK_SPACE:\r
-    return "space";\r
-\r
-  case VK_LEFT:\r
-    return "Left";\r
-\r
-  case VK_UP:\r
-    return "Up";\r
-\r
-  case VK_RIGHT:\r
-    return "Right";\r
-\r
-  case VK_DOWN:\r
-    return "Down";\r
-\r
-  case 48:\r
-    return "0";\r
-\r
-  case 49:\r
-    return "1";\r
-\r
-  case 50:\r
-    return "2";\r
-\r
-  case 51:\r
-    return "3";\r
-\r
-  case 52:\r
-    return "4";\r
-\r
-  case 53:\r
-    return "5";\r
-\r
-  case 54:\r
-    return "6";\r
-\r
-  case 55:\r
-    return "7";\r
-\r
-  case 56:\r
-    return "8";\r
-\r
-  case 57:\r
-    return "9";\r
-\r
-    break;\r
-  }\r
-\r
-  return "";\r
-}\r
-
-static LARGE_INTEGER _cpuFrequency;\r
-static LARGE_INTEGER *_pCpuFrequency = NULL;\r
-\r
-long GetCurrentThreadId()\r
-{\r
-  return ::GetCurrentThreadId();\r
-}\r
-\r
-void GetNanoseconds( uint64_t& timeInNanoseconds )\r
-{\r
-  if( NULL == _pCpuFrequency )\r
-  {\r
-    _pCpuFrequency = &_cpuFrequency;\r
-    QueryPerformanceFrequency( _pCpuFrequency );\r
-  }\r
-\r
-  LARGE_INTEGER curTime;\r
-  QueryPerformanceCounter( &curTime );\r
-\r
-  timeInNanoseconds = (double)curTime.QuadPart / (double)_pCpuFrequency->QuadPart * 1000000000;\r
-}\r
-\r
-unsigned int GetCurrentMilliSeconds( void )\r
-{\r
-  if( NULL == _pCpuFrequency )\r
-  {\r
-    _pCpuFrequency = &_cpuFrequency;\r
-    QueryPerformanceFrequency( _pCpuFrequency );\r
-  }\r
-\r
-  LARGE_INTEGER curTime;\r
-  QueryPerformanceCounter( &curTime );\r
-\r
-  return curTime.QuadPart * 1000 / _pCpuFrequency->QuadPart;\r
-}\r
+
+namespace WindowsPlatform
+{
+
+LRESULT CALLBACK WinProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
+{
+  WindowImpl::ProcWinMessage( reinterpret_cast<uint64_t>( hWnd ), uMsg, wParam, lParam );
+
+  LRESULT ret = DefWindowProc( hWnd, uMsg, wParam, lParam );
+  return ret;
+}
+
+namespace
+{
+
+const std::string DALI_WINDOW_CLASS_NAME = "DaliWindow";
+
+uint32_t sNumWindows = 0;
+
+void EnsureWindowClassRegistered()
+{
+  if (sNumWindows == 0)
+  {
+    WNDCLASS cs = { 0 };
+    cs.cbClsExtra = 0;
+    cs.cbWndExtra = 0;
+    cs.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);
+    cs.hCursor = NULL;
+    cs.hIcon = NULL;
+    cs.hInstance = GetModuleHandle(NULL);
+    cs.lpfnWndProc = (WNDPROC)WinProc;
+    cs.lpszClassName = DALI_WINDOW_CLASS_NAME.c_str();
+    cs.lpszMenuName = NULL;
+    cs.style = CS_VREDRAW | CS_HREDRAW;
+    RegisterClass(&cs);
+  }
+}
+
+void EnsureWindowClassUnregistered()
+{
+  if (sNumWindows == 0)
+  {
+    UnregisterClass(DALI_WINDOW_CLASS_NAME.c_str(), GetModuleHandle(NULL));
+  }
+}
+
+std::map<uint64_t, WindowImpl*> sHWndToListener;
+
+void RemoveListener(uint64_t hWnd)
+{
+  std::map<uint64_t, WindowImpl*>::iterator x = sHWndToListener.find(hWnd);
+  if (sHWndToListener.end() != x)
+  {
+    sHWndToListener.erase(x);
+  }
+}
+
+}
+
+const uint32_t WindowImpl::STYLE = WS_OVERLAPPED;
+const int32_t WindowImpl::EDGE_WIDTH = 8;
+const int32_t WindowImpl::EDGE_HEIGHT = 18;
+
+WindowImpl::WindowImpl()
+{
+  colorDepth = -1;
+  mHWnd = 0;
+  mHdc = 0;
+  listener = NULL;
+}
+
+WindowImpl::~WindowImpl()
+{
+  RemoveListener(mHWnd);
+}
+
+void WindowImpl::ProcWinMessage( uint64_t hWnd, uint32_t uMsg, uint64_t wParam, uint64_t lParam )
+{
+  std::map<uint64_t, WindowImpl*>::iterator x = sHWndToListener.find( hWnd );
+
+  if( sHWndToListener.end() != x )
+  {
+    CallbackBase* listener = x->second->listener;
+
+    if( NULL != listener )
+    {
+      TWinEventInfo eventInfo( hWnd, uMsg, wParam, lParam );
+      CallbackBase::Execute( *listener, &eventInfo );
+    }
+  }
+}
+
+void WindowImpl::GetDPI( float &xDpi, float &yDpi )
+{
+  HDC hdcScreen = GetDC( reinterpret_cast<HWND>( mHWnd ) );
+
+  int32_t iX = GetDeviceCaps( hdcScreen, HORZRES );    // pixel
+  int32_t iY = GetDeviceCaps( hdcScreen, VERTRES );    // pixel
+  int32_t iPhsX = GetDeviceCaps( hdcScreen, HORZSIZE );    // mm
+  int32_t iPhsY = GetDeviceCaps( hdcScreen, VERTSIZE );    // mm
+
+  xDpi = static_cast<float>( iX ) / static_cast<float>( iPhsX ) * INCH;
+  yDpi = static_cast<float>( iY ) / static_cast<float>( iPhsY ) * INCH;
+}
+
+int WindowImpl::GetColorDepth()
+{
+  DALI_ASSERT_DEBUG( colorDepth >= 0 && "HWND hasn't been created, no color depth" );
+  return colorDepth;
+}
+
+uint64_t WindowImpl::CreateHwnd(
+  _In_opt_ const char *lpWindowName,
+  _In_ int X,
+  _In_ int Y,
+  _In_ int nWidth,
+  _In_ int nHeight,
+  _In_opt_ uint64_t parent )
+{
+  EnsureWindowClassRegistered();
+  ++sNumWindows;
+
+  HWND hWnd = CreateWindow( DALI_WINDOW_CLASS_NAME.c_str(), lpWindowName, STYLE, X, Y,
+    nWidth + 2 * EDGE_WIDTH, nHeight + 2 * EDGE_HEIGHT, NULL, NULL, GetModuleHandle(NULL), NULL );
+  ::ShowWindow( hWnd, SW_SHOW );
+
+  return reinterpret_cast<uint64_t>(hWnd);
+}
+
+void WindowImpl::DestroyHWnd(uint64_t hWnd)
+{
+  if (hWnd != 0)
+  {
+    ::DestroyWindow(reinterpret_cast<HWND>(hWnd));
+
+    --sNumWindows;
+    EnsureWindowClassUnregistered();
+  }
+}
+
+void WindowImpl::SetListener( CallbackBase *callback )
+{
+  listener = callback;
+}
+
+bool WindowImpl::PostWinMessage(
+  _In_ uint32_t Msg,
+  _In_ uint64_t wParam,
+  _In_ uint64_t lParam )
+{
+  return (bool)PostMessage( reinterpret_cast<HWND>( mHWnd ), Msg, wParam, lParam );
+}
+
+void WindowImpl::SetHWND( uint64_t inHWnd )
+{
+  if (mHWnd != inHWnd)
+  {
+    RemoveListener(mHWnd);
+
+    mHWnd = inHWnd;
+    mHdc = reinterpret_cast<uint64_t>(GetDC(reinterpret_cast<HWND>(mHWnd)));
+    colorDepth = GetDeviceCaps(reinterpret_cast<HDC>(mHdc), BITSPIXEL) * GetDeviceCaps(reinterpret_cast<HDC>(mHdc), PLANES);
+
+    std::map<uint64_t, WindowImpl*>::iterator x = sHWndToListener.find(mHWnd);
+    if (sHWndToListener.end() == x)
+    {
+      sHWndToListener.insert(std::make_pair(mHWnd, this));
+    }
+    else
+    {
+      x->second = this;
+    }
+  }
+}
+
+void WindowImpl::SetWinProc()
+{
+  // Sets the WinProc function.
+  LONG_PTR ret = SetWindowLongPtr((HWND)mHWnd,
+                                  GWLP_WNDPROC,
+                                  reinterpret_cast<LONG_PTR>(&WinProc));
+
+  if (0 == ret)
+  {
+    DWORD error = GetLastError();
+    return;
+  }
+
+  HMODULE module = GetModuleHandle(nullptr);
+  ret = SetWindowLongPtr((HWND)mHWnd,
+                         GWLP_HINSTANCE,
+                         reinterpret_cast<LONG_PTR>(&module));
+}
+
+bool PostWinThreadMessage(
+  _In_ uint32_t Msg,
+  _In_ uint64_t wParam,
+  _In_ uint64_t lParam,
+  _In_ uint64_t threadID/* = -1*/ )
+{
+  if( -1 == threadID )
+  {
+    threadID = GetCurrentThreadId();
+  }
+
+  return (bool)PostThreadMessage( threadID, Msg, wParam, lParam );
+}
+
+struct TTimerCallbackInfo
+{
+  void *data;
+  timerCallback callback;
+  HWND hWnd;
+};
+
+void CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT_PTR nTimerid, DWORD dwTime)
+{
+  TTimerCallbackInfo *info = (TTimerCallbackInfo*)nTimerid;
+  info->callback( info->data );
+}
+
+intptr_t SetTimer(int interval, timerCallback callback, void *data)
+{
+  TTimerCallbackInfo *callbackInfo = new TTimerCallbackInfo;
+  callbackInfo->data = data;
+  callbackInfo->callback = callback;
+  callbackInfo->hWnd = ::GetActiveWindow();
+
+  INT_PTR timerID = (INT_PTR)callbackInfo;
+  ::SetTimer( callbackInfo->hWnd, timerID, interval, TimerProc );
+
+  return timerID;
+}
+
+void KillTimer(intptr_t id)
+{
+  TTimerCallbackInfo *info = (TTimerCallbackInfo*)id;
+  ::KillTimer( info->hWnd, id );
+  delete info;
+}
+
+const char* GetKeyName( int keyCode )
+{
+  switch( keyCode )
+  {
+    case VK_BACK:
+    {
+      return "Backspace";
+    }
+    case VK_TAB:
+    {
+      return "Tab";
+    }
+    case VK_RETURN:
+    {
+      return "Return";
+    }
+    case VK_ESCAPE:
+    {
+      return "Escape";
+    }
+    case VK_SPACE:
+    {
+      return "Space";
+    }
+    case VK_LEFT:
+    {
+      return "Left";
+    }
+    case VK_UP:
+    {
+      return "Up";
+    }
+    case VK_RIGHT:
+    {
+      return "Right";
+    }
+    case VK_DOWN:
+    {
+      return "Down";
+    }
+    case 48:
+    {
+      return "0";
+    }
+    case 49:
+    {
+      return "1";
+    }
+    case 50:
+    {
+      return "2";
+    }
+    case 51:
+    {
+      return "3";
+    }
+    case 52:
+    {
+      return "4";
+    }
+    case 53:
+    {
+      return "5";
+    }
+    case 54:
+    {
+      return "6";
+    }
+    case 55:
+    {
+      return "7";
+    }
+    case 56:
+    {
+      return "8";
+    }
+    case 57:
+    {
+      return "9";
+    }
+    default:
+    {
+      break;
+    }
+  }
+
+  return "";
+}
+
+static LARGE_INTEGER cpuFrequency;
+static LARGE_INTEGER *pCpuFrequency = NULL;
+
+uint64_t GetCurrentThreadId()
+{
+  return ::GetCurrentThreadId();
+}
+
+void GetNanoseconds( uint64_t& timeInNanoseconds )
+{
+  if( NULL == pCpuFrequency )
+  {
+    pCpuFrequency = &cpuFrequency;
+    QueryPerformanceFrequency( pCpuFrequency );
+  }
+
+  LARGE_INTEGER curTime;
+  QueryPerformanceCounter( &curTime );
+
+  timeInNanoseconds = static_cast<double>(curTime.QuadPart) / static_cast<double>(pCpuFrequency->QuadPart) * 1000000000;
+}
+
+unsigned int GetCurrentMilliSeconds( void )
+{
+  if( NULL == pCpuFrequency )
+  {
+    pCpuFrequency = &cpuFrequency;
+    QueryPerformanceFrequency( pCpuFrequency );
+  }
+
+  LARGE_INTEGER curTime;
+  QueryPerformanceCounter( &curTime );
+
+  return curTime.QuadPart * 1000 / pCpuFrequency->QuadPart;
+}
 
 } // namespace WindowsPlatformImplement
 
@@ -395,4 +410,4 @@ unsigned int GetCurrentMilliSeconds( void )
 
 } // namespace internal
 
-} // namespace Dali\r
+} // namespace Dali