Revert "[Tizen] Add screen and client rotation itself function"
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / windows / platform-implement-win.cpp
index f122a43..1d79ce8 100755 (executable)
@@ -1,10 +1,30 @@
+/*\r
+* Copyright (c) 2018 Samsung Electronics Co., Ltd.\r
+*\r
+* Licensed under the Apache License, Version 2.0 (the "License");\r
+* you may not use this file except in compliance with the License.\r
+* You may obtain a copy of the License at\r
+*\r
+* http://www.apache.org/licenses/LICENSE-2.0\r
+*\r
+* Unless required by applicable law or agreed to in writing, software\r
+* distributed under the License is distributed on an "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+* See the License for the specific language governing permissions and\r
+* limitations under the License.\r
+*\r
+*/\r
+\r
 // CLASS HEADER\r
 #include <dali/internal/window-system/windows/platform-implement-win.h>\r
 \r
-// EXTERNAL_HEADERS\r
+// EXTERNAL INCLUDES\r
 #include <windows.h>\r
-#include <map>\r
-#include <set>\r
+\r
+// INTERNAL INCLUDES\r
+#include <dali/internal/window-system/windows/event-system-win.h>\r
+\r
+static constexpr float INCH = 25.4;\r
 \r
 using namespace std;\r
 \r
@@ -17,24 +37,24 @@ namespace Internal
 namespace Adaptor
 {
 \r
-namespace WindowsPlatformImplement\r
+namespace WindowsPlatformImplementation\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
+    if( WIN_CALLBACK_EVENT == nMsg.message )\r
+    {\r
+      Dali::CallbackBase *callback = ( Dali::CallbackBase* )nMsg.wParam;\r
+      Dali::CallbackBase::Execute( *callback );\r
+    }\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
@@ -42,52 +62,27 @@ void RunLoop()
   }\r
 }\r
 \r
-void SetParent(long child, long parent)\r
+void GetDPI( uint64_t hWnd, float &xDpi, float &yDpi )\r
 {\r
-    if (roothWnd == (HWND)child && NULL != (HWND)parent)\r
-    {\r
-        roothWnd = (HWND)parent;\r
-    }\r
+  HDC hdcScreen = GetDC( reinterpret_cast<HWND>( hWnd ) );\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
+  int32_t iX = GetDeviceCaps( hdcScreen, HORZRES );    // pixel\r
+  int32_t iY = GetDeviceCaps( hdcScreen, VERTRES );    // pixel\r
+  int32_t iPhsX = GetDeviceCaps( hdcScreen, HORZSIZE );    // mm\r
+  int32_t iPhsY = GetDeviceCaps( hdcScreen, VERTSIZE );    // mm\r
 \r
-  xDpi = (float)iX / (float)iPhsX * INCH;\r
-  yDpi = (float)iY / (float)iPhsY * INCH;\r
+  xDpi = static_cast<float>( iX ) / static_cast<float>( iPhsX ) * INCH;\r
+  yDpi = static_cast<float>( iY ) / static_cast<float>( iPhsY ) * INCH;\r
 }\r
 \r
-map<int, set<Dali::CallbackBase*>> callbackMap;\r
-set<winEventCallback> listenerSet;\r
+CallbackBase *listener = NULL;\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
+  if( NULL != listener )\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
+    TWinEventInfo eventInfo( reinterpret_cast<uint64_t>( hWnd ), uMsg, wParam, lParam);\r
+    CallbackBase::Execute( *listener, &eventInfo );\r
   }\r
 \r
   LRESULT ret = DefWindowProc( hWnd, uMsg, wParam, lParam );\r
@@ -96,42 +91,78 @@ LRESULT CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
 \r
 DWORD windowStyle = WS_OVERLAPPED;\r
 \r
-int GetEdgeWidth()\r
+int32_t GetEdgeWidth()\r
 {\r
   switch( windowStyle )\r
   {\r
-  case WS_OVERLAPPED:\r
-    return 8;\r
-    break;\r
-\r
-  default:\r
-    return 0;\r
-    break;\r
+    case WS_OVERLAPPED:\r
+    {\r
+      return 8;\r
+    }\r
+    default:\r
+    {\r
+      return 0;\r
+    }\r
   }\r
 }\r
 \r
-int GetEdgeHeight()\r
+int32_t GetEdgeHeight()\r
 {\r
   switch( windowStyle )\r
   {\r
-  case WS_OVERLAPPED:\r
-    return 18;\r
-    break;\r
+    case WS_OVERLAPPED:\r
+    {\r
+      return 18;\r
+    }\r
+    default:\r
+    {\r
+      return 0;\r
+    }\r
+  }\r
+}\r
+\r
+class WindowsDisplayInfo\r
+{\r
+public:\r
+  static int GetColorDepth()\r
+  {\r
+    DALI_ASSERT_DEBUG(colorDepth >= 0 && "HWND hasn't been created, no color depth");\r
+    return colorDepth;\r
+  }\r
 \r
-  default:\r
-    return 0;\r
-    break;\r
+  static void SetHWND( HWND inHWnd )\r
+  {\r
+    if( hWnd != inHWnd )\r
+    {\r
+      hWnd = inHWnd;\r
+      hdc = GetDC( hWnd );\r
+      colorDepth = GetDeviceCaps( WindowsDisplayInfo::hdc, BITSPIXEL ) * GetDeviceCaps( WindowsDisplayInfo::hdc, PLANES );\r
+    }\r
   }\r
+\r
+private:\r
+  static int colorDepth;\r
+  static HWND hWnd;\r
+  static HDC hdc;\r
+};\r
+\r
+int WindowsDisplayInfo::colorDepth = -1;\r
+HWND WindowsDisplayInfo::hWnd = NULL;\r
+HDC WindowsDisplayInfo::hdc = NULL;\r
+\r
+int GetColorDepth()\r
+{\r
+  return WindowsDisplayInfo::GetColorDepth();\r
 }\r
 \r
-long CreateHwnd(\r
+uint64_t 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
+    _In_opt_ uint64_t parent)\r
 {\r
   WNDCLASS cs = { 0 };\r
   cs.cbClsExtra = 0;\r
@@ -144,249 +175,210 @@ long CreateHwnd(
   cs.lpszClassName = lpClassName;\r
   cs.lpszMenuName = NULL;\r
   cs.style = CS_VREDRAW | CS_HREDRAW;\r
-  RegisterClass( &cs );//ʵÏÖ×¢²á´°¿Ú\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
+  WindowsDisplayInfo::SetHWND( hWnd );\r
 \r
-  return (long)hWnd;\r
+  return reinterpret_cast<uint64_t>( hWnd );\r
 }\r
 \r
-void AddListener(winEventCallback callback)\r
+void SetListener( CallbackBase *callback )\r
 {\r
-  listenerSet.insert(callback);\r
+  listener = 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
+bool PostWinMessage(\r
+    _In_ uint32_t Msg,\r
+    _In_ uint32_t wParam,\r
+    _In_ uint64_t lParam,\r
+    _In_ uint64_t hWnd)\r
 {\r
-  callbackMap.erase(msgType);\r
+  return (bool)PostMessage( reinterpret_cast<HWND>( hWnd ), Msg, wParam, lParam );\r
 }\r
 \r
-bool PostWinMessage(\r
-    _In_ unsigned int Msg,\r
-    _In_ long wParam,\r
-    _In_ long lParam,\r
-    _In_ long hWnd/* = 0*/)\r
+bool PostWinThreadMessage(\r
+    _In_ uint32_t Msg,\r
+    _In_ uint32_t wParam,\r
+    _In_ uint64_t lParam,\r
+    _In_ uint64_t threadID/* = -1*/ )\r
 {\r
-  if( 0 == hWnd )\r
+  if( -1 == threadID )\r
   {\r
-    return (bool)PostMessage( roothWnd, Msg, wParam, lParam );\r
+    threadID = GetCurrentThreadId();\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
+  return (bool)PostThreadMessage( threadID, Msg, wParam, lParam );\r
 }\r
 \r
-unsigned long WaitForSingleObject(_In_ long hHandle, _In_ unsigned long dwMilliseconds)\r
+void ShowWindow( uint64_t hWnd)\r
 {\r
-  return ::WaitForSingleObject((HWND)hHandle, dwMilliseconds);\r
+  ::ShowWindow( reinterpret_cast<HWND>( hWnd ), SW_SHOW);\r
 }\r
 \r
-bool ReleaseSemaphore(_In_ long hSemaphore, _In_ long lReleaseCount, _Out_opt_ long *lpPreviousCount)\r
+void HideWindow( uint64_t hWnd)\r
 {\r
-  return ::ReleaseSemaphore((HWND)hSemaphore, lReleaseCount, lpPreviousCount);\r
+  ::ShowWindow( reinterpret_cast<HWND>( hWnd ), SW_HIDE);\r
 }\r
 \r
 struct TTimerCallbackInfo\r
 {\r
   void *data;\r
   timerCallback callback;\r
+  HWND hWnd;\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
+  TTimerCallbackInfo *info = (TTimerCallbackInfo*)nTimerid;\r
+  info->callback( info->data );\r
 }\r
 \r
 int SetTimer(int interval, timerCallback callback, void *data)\r
 {\r
-  int timerID = 0;\r
+  TTimerCallbackInfo *callbackInfo = new TTimerCallbackInfo;\r
+  callbackInfo->data = data;\r
+  callbackInfo->callback = callback;\r
+  callbackInfo->hWnd = ::GetActiveWindow();\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
+  UINT_PTR timerID = (UINT_PTR)callbackInfo;\r
+  ::SetTimer( callbackInfo->hWnd, timerID, interval, TimerProc );\r
 \r
   return timerID;\r
 }\r
 \r
 void KillTimer(int id)\r
 {\r
-  mapTimerIDToData.erase(id);\r
-  ::KillTimer(roothWnd, id);\r
+  TTimerCallbackInfo *info = (TTimerCallbackInfo*)id;\r
+  ::KillTimer( info->hWnd, id );\r
+  delete info;\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
+    case VK_BACK:\r
+    {\r
+      return "Backspace";\r
+    }\r
+    case VK_TAB:\r
+    {\r
+      return "Tab";\r
+    }\r
+    case VK_RETURN:\r
+    {\r
+      return "Return";\r
+    }\r
+    case VK_ESCAPE:\r
+    {\r
+      return "Escape";\r
+    }\r
+    case VK_SPACE:\r
+    {\r
+      return "Space";\r
+    }\r
+    case VK_LEFT:\r
+    {\r
+      return "Left";\r
+    }\r
+    case VK_UP:\r
+    {\r
+      return "Up";\r
+    }\r
+    case VK_RIGHT:\r
+    {\r
+      return "Right";\r
+    }\r
+    case VK_DOWN:\r
+    {\r
+      return "Down";\r
+    }\r
+    case 48:\r
+    {\r
+      return "0";\r
+    }\r
+    case 49:\r
+    {\r
+      return "1";\r
+    }\r
+    case 50:\r
+    {\r
+      return "2";\r
+    }\r
+    case 51:\r
+    {\r
+      return "3";\r
+    }\r
+    case 52:\r
+    {\r
+      return "4";\r
+    }\r
+    case 53:\r
+    {\r
+      return "5";\r
+    }\r
+    case 54:\r
+    {\r
+      return "6";\r
+    }\r
+    case 55:\r
+    {\r
+      return "7";\r
+    }\r
+    case 56:\r
+    {\r
+      return "8";\r
+    }\r
+    case 57:\r
+    {\r
+      return "9";\r
+    }\r
+    default:\r
+    {\r
+      break;\r
+    }\r
   }\r
 \r
   return "";\r
 }\r
 
-static LARGE_INTEGER _cpuFrequency;\r
-static LARGE_INTEGER *_pCpuFrequency = NULL;\r
+static LARGE_INTEGER cpuFrequency;\r
+static LARGE_INTEGER *pCpuFrequency = NULL;\r
 \r
-long GetCurrentThreadId()\r
+uint64_t GetCurrentThreadId()\r
 {\r
   return ::GetCurrentThreadId();\r
 }\r
 \r
 void GetNanoseconds( uint64_t& timeInNanoseconds )\r
 {\r
-  if( NULL == _pCpuFrequency )\r
+  if( NULL == pCpuFrequency )\r
   {\r
-    _pCpuFrequency = &_cpuFrequency;\r
-    QueryPerformanceFrequency( _pCpuFrequency );\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
+  timeInNanoseconds = static_cast<double>(curTime.QuadPart) / static_cast<double>(pCpuFrequency->QuadPart) * 1000000000;\r
 }\r
 \r
 unsigned int GetCurrentMilliSeconds( void )\r
 {\r
-  if( NULL == _pCpuFrequency )\r
+  if( NULL == pCpuFrequency )\r
   {\r
-    _pCpuFrequency = &_cpuFrequency;\r
-    QueryPerformanceFrequency( _pCpuFrequency );\r
+    pCpuFrequency = &cpuFrequency;\r
+    QueryPerformanceFrequency( pCpuFrequency );\r
   }\r
 \r
   LARGE_INTEGER curTime;\r
   QueryPerformanceCounter( &curTime );\r
 \r
-  return curTime.QuadPart * 1000 / _pCpuFrequency->QuadPart;\r
+  return curTime.QuadPart * 1000 / pCpuFrequency->QuadPart;\r
 }\r
 
 } // namespace WindowsPlatformImplement