Merge "Fix svace issue for image-operator" into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / windows / platform-implement-win.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 9322e26..2716f90
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2018 Samsung Electronics Co., Ltd.
+* Copyright (c) 2023 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.
 #include <dali/internal/window-system/windows/platform-implement-win.h>
 
 // EXTERNAL INCLUDES
-#include <map>
 #include <windows.h>
+#include <map>
 
 // INTERNAL INCLUDES
 #include <dali/internal/window-system/windows/event-system-win.h>
 
 namespace
 {
-static constexpr float INCH = 25.4;
+constexpr float INCH = 25.4;
 }
 
 namespace Dali
 {
-
 namespace Internal
 {
-
 namespace Adaptor
 {
+namespace WindowsPlatform
+{
+LRESULT CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
+{
+  WindowImpl::ProcWinMessage(reinterpret_cast<uint64_t>(hWnd), uMsg, wParam, lParam);
 
-namespace WindowsPlatformImplementation
+  LRESULT ret = DefWindowProc(hWnd, uMsg, wParam, lParam);
+  return ret;
+}
+
+namespace
 {
+const char* DALI_WINDOW_CLASS_NAME = "DaliWindow";
 
-LRESULT CALLBACK WinProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
+uint32_t sNumWindows = 0;
+
+void EnsureWindowClassRegistered()
 {
-  WindowImpl::ProcWinMessage( reinterpret_cast<uint64_t>( hWnd ), uMsg, wParam, lParam );
+  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);
+  }
+}
 
-  LRESULT ret = DefWindowProc( hWnd, uMsg, wParam, lParam );
-  return ret;
+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);
+  }
 }
 
-std::map<uint64_t, WindowImpl*> mHWndToListener;
+} // namespace
+
+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;
-  windowStyle = WS_OVERLAPPED;
+  mHWnd      = 0;
+  mHdc       = 0;
+  listener   = NULL;
 }
 
 WindowImpl::~WindowImpl()
 {
-  mHWndToListener.erase( mHWnd );
+  RemoveListener(mHWnd);
 }
 
-void WindowImpl::ProcWinMessage( uint64_t hWnd, uint32_t uMsg, uint64_t wParam, uint64_t lParam )
+void WindowImpl::ProcWinMessage(uint64_t hWnd, uint32_t uMsg, uint64_t wParam, uint64_t lParam)
 {
-  std::map<uint64_t, WindowImpl*>::iterator x = mHWndToListener.find( hWnd );
+  std::map<uint64_t, WindowImpl*>::iterator x = sHWndToListener.find(hWnd);
 
-  if( mHWndToListener.end() != x )
+  if(sHWndToListener.end() != x)
   {
     CallbackBase* listener = x->second->listener;
 
-    if( NULL != listener )
+    if(NULL != listener)
     {
-      TWinEventInfo eventInfo( hWnd, uMsg, wParam, lParam );
-      CallbackBase::Execute( *listener, &eventInfo );
+      TWinEventInfo eventInfo(hWnd, uMsg, wParam, lParam);
+      CallbackBase::Execute(*listener, &eventInfo);
     }
   }
 }
 
-void WindowImpl::GetDPI( float &xDpi, float &yDpi )
+void WindowImpl::GetDPI(float& xDpi, float& yDpi)
 {
-  HDC hdcScreen = GetDC( reinterpret_cast<HWND>( mHWnd ) );
+  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
+  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;
+  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" );
+  DALI_ASSERT_DEBUG(colorDepth >= 0 && "HWND hasn't been created, no color depth");
   return colorDepth;
 }
 
 uint64_t WindowImpl::CreateHwnd(
-  _In_opt_ const char *lpClassName,
-  _In_opt_ const char *lpWindowName,
-  _In_ int X,
-  _In_ int Y,
-  _In_ int nWidth,
-  _In_ int nHeight,
-  _In_opt_ uint64_t parent )
+  _In_opt_ const char* lpWindowName,
+  _In_ int             X,
+  _In_ int             Y,
+  _In_ int             nWidth,
+  _In_ int             nHeight,
+  _In_opt_ uint64_t    parent)
 {
-  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 = lpClassName;
-  cs.lpszMenuName = NULL;
-  cs.style = CS_VREDRAW | CS_HREDRAW;
-  RegisterClass( &cs );
-
-  HWND hWnd = CreateWindow( lpClassName, lpWindowName, windowStyle, X, Y, nWidth + 2 * GetEdgeWidth(), nHeight + 2 * GetEdgeHeight(), NULL, NULL, cs.hInstance, NULL );
-  ::ShowWindow( hWnd, SW_SHOW );
-
-  SetHWND( reinterpret_cast<uint64_t>(hWnd) );
-
-  return mHWnd;
+  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::SetListener( CallbackBase *callback )
+void WindowImpl::DestroyHWnd(uint64_t hWnd)
+{
+  if(hWnd != 0)
+  {
+    ::DestroyWindow(reinterpret_cast<HWND>(hWnd));
+
+    --sNumWindows;
+    EnsureWindowClassUnregistered();
+  }
+}
+
+void WindowImpl::SetListener(CallbackBase* callback)
 {
   listener = callback;
 }
@@ -139,54 +180,25 @@ void WindowImpl::SetListener( CallbackBase *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 );
-}
-
-int32_t WindowImpl::GetEdgeWidth()
+  _In_ uint64_t lParam)
 {
-  switch( windowStyle )
-  {
-  case WS_OVERLAPPED:
-  {
-    return 8;
-  }
-  default:
-  {
-    return 0;
-  }
-  }
+  return (bool)PostMessage(reinterpret_cast<HWND>(mHWnd), Msg, wParam, lParam);
 }
 
-int32_t WindowImpl::GetEdgeHeight()
+void WindowImpl::SetHWND(uint64_t inHWnd)
 {
-  switch( windowStyle )
-  {
-  case WS_OVERLAPPED:
+  if(mHWnd != inHWnd)
   {
-    return 18;
-  }
-  default:
-  {
-    return 0;
-  }
-  }
-}
+    RemoveListener(mHWnd);
 
-void WindowImpl::SetHWND( uint64_t inHWnd )
-{
-  if (mHWnd != inHWnd)
-  {
-    mHWnd = inHWnd;
-    mHdc = reinterpret_cast<uint64_t>(GetDC(reinterpret_cast<HWND>(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 = mHWndToListener.find(mHWnd);
-
-    if (mHWndToListener.end() == x)
+    std::map<uint64_t, WindowImpl*>::iterator x = sHWndToListener.find(mHWnd);
+    if(sHWndToListener.end() == x)
     {
-      mHWndToListener.insert(std::make_pair(mHWnd, this));
+      sHWndToListener.insert(std::make_pair(mHWnd, this));
     }
     else
     {
@@ -202,14 +214,14 @@ void WindowImpl::SetWinProc()
                                   GWLP_WNDPROC,
                                   reinterpret_cast<LONG_PTR>(&WinProc));
 
-  if (0 == ret)
+  if(0 == ret)
   {
     DWORD error = GetLastError();
     return;
   }
 
   HMODULE module = GetModuleHandle(nullptr);
-  ret = SetWindowLongPtr((HWND)mHWnd,
+  ret            = SetWindowLongPtr((HWND)mHWnd,
                          GWLP_HINSTANCE,
                          reinterpret_cast<LONG_PTR>(&module));
 }
@@ -218,52 +230,63 @@ bool PostWinThreadMessage(
   _In_ uint32_t Msg,
   _In_ uint64_t wParam,
   _In_ uint64_t lParam,
-  _In_ uint64_t threadID/* = -1*/ )
+  _In_ uint64_t threadID /* = -1*/)
 {
-  if( -1 == threadID )
+  if(-1 == threadID)
   {
     threadID = GetCurrentThreadId();
   }
 
-  return (bool)PostThreadMessage( threadID, Msg, wParam, lParam );
+  return (bool)PostThreadMessage(threadID, Msg, wParam, lParam);
 }
 
 struct TTimerCallbackInfo
 {
-  void *data;
+  void*         data;
   timerCallback callback;
-  HWND hWnd;
+  HWND          hWnd;
 };
 
 void CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT_PTR nTimerid, DWORD dwTime)
 {
-  TTimerCallbackInfo *info = (TTimerCallbackInfo*)nTimerid;
-  info->callback( info->data );
+  TTimerCallbackInfoinfo = (TTimerCallbackInfo*)nTimerid;
+  info->callback(info->data);
 }
 
-intptr_t SetTimer(int interval, timerCallback callback, void *data)
+intptr_t SetTimer(int interval, timerCallback callback, voiddata)
 {
-  TTimerCallbackInfo *callbackInfo = new TTimerCallbackInfo;
-  callbackInfo->data = data;
-  callbackInfo->callback = callback;
-  callbackInfo->hWnd = ::GetActiveWindow();
+  HWND hwnd = GetActiveWindow();
+  if(!hwnd)
+  {
+    hwnd = FindWindow(DALI_WINDOW_CLASS_NAME.c_str(), nullptr);
+  }
+
+  if(!hwnd)
+  {
+    return -1;
+  }
+
+  TTimerCallbackInfo* callbackInfo = new TTimerCallbackInfo;
+  callbackInfo->data               = data;
+  callbackInfo->callback           = callback;
+  callbackInfo->hWnd               = hwnd;
 
   INT_PTR timerID = (INT_PTR)callbackInfo;
-  ::SetTimer( callbackInfo->hWnd, timerID, interval, TimerProc );
+  ::SetTimer(hwnd, timerID, interval, TimerProc);
 
   return timerID;
 }
 
 void KillTimer(intptr_t id)
 {
-  TTimerCallbackInfo *info = (TTimerCallbackInfo*)id;
-  ::KillTimer( info->hWnd, id );
+  TTimerCallbackInfoinfo = (TTimerCallbackInfo*)id;
+  ::KillTimer(info->hWnd, id);
   delete info;
 }
 
-const char* GetKeyName( int keyCode )
+std::string GetKeyName(int keyCode)
 {
-  switch( keyCode )
+  switch(keyCode)
   {
     case VK_BACK:
     {
@@ -343,6 +366,10 @@ const char* GetKeyName( int keyCode )
     }
     default:
     {
+      if(keyCode > 0 && keyCode < 128)
+      {
+        return std::string(1u, static_cast<char>(keyCode));
+      }
       break;
     }
   }
@@ -350,46 +377,46 @@ const char* GetKeyName( int keyCode )
   return "";
 }
 
-static LARGE_INTEGER cpuFrequency;
-static LARGE_INTEGER *pCpuFrequency = NULL;
+static LARGE_INTEGER  cpuFrequency;
+static LARGE_INTEGERpCpuFrequency = NULL;
 
 uint64_t GetCurrentThreadId()
 {
   return ::GetCurrentThreadId();
 }
 
-void GetNanoseconds( uint64_t& timeInNanoseconds )
+void GetNanoseconds(uint64_t& timeInNanoseconds)
 {
-  if( NULL == pCpuFrequency )
+  if(NULL == pCpuFrequency)
   {
     pCpuFrequency = &cpuFrequency;
-    QueryPerformanceFrequency( pCpuFrequency );
+    QueryPerformanceFrequency(pCpuFrequency);
   }
 
   LARGE_INTEGER curTime;
-  QueryPerformanceCounter( &curTime );
+  QueryPerformanceCounter(&curTime);
 
   timeInNanoseconds = static_cast<double>(curTime.QuadPart) / static_cast<double>(pCpuFrequency->QuadPart) * 1000000000;
 }
 
-unsigned int GetCurrentMilliSeconds( void )
+unsigned int GetCurrentMilliSeconds(void)
 {
-  if( NULL == pCpuFrequency )
+  if(NULL == pCpuFrequency)
   {
     pCpuFrequency = &cpuFrequency;
-    QueryPerformanceFrequency( pCpuFrequency );
+    QueryPerformanceFrequency(pCpuFrequency);
   }
 
   LARGE_INTEGER curTime;
-  QueryPerformanceCounter( &curTime );
+  QueryPerformanceCounter(&curTime);
 
   return curTime.QuadPart * 1000 / pCpuFrequency->QuadPart;
 }
 
-} // namespace WindowsPlatformImplement
+} // namespace WindowsPlatform
 
 } // namespace Adaptor
 
-} // namespace internal
+} // namespace Internal
 
 } // namespace Dali