summaryrefslogtreecommitdiffstats
path: root/widget/windows/OSKInputPaneManager.cpp
blob: 293a84cd28c8008898978ba6de719c863f5c48c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sts=2 sw=2 et cin: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#define NTDDI_VERSION NTDDI_WIN10_RS1

#include "OSKInputPaneManager.h"
#include "nsDebug.h"

#ifndef __MINGW32__
#  include <inputpaneinterop.h>
#  include <windows.ui.viewmanagement.h>
#  include <wrl.h>

using namespace ABI::Windows::UI::ViewManagement;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
#endif

namespace mozilla {
namespace widget {

#ifndef __MINGW32__
static ComPtr<IInputPane2> GetInputPane(HWND aHwnd) {
  ComPtr<IInputPaneInterop> inputPaneInterop;
  HRESULT hr = RoGetActivationFactory(
      HStringReference(RuntimeClass_Windows_UI_ViewManagement_InputPane).Get(),
      IID_PPV_ARGS(&inputPaneInterop));
  if (NS_WARN_IF(FAILED(hr))) {
    return nullptr;
  }

  ComPtr<IInputPane> inputPane;
  hr = inputPaneInterop->GetForWindow(aHwnd, IID_PPV_ARGS(&inputPane));
  if (NS_WARN_IF(FAILED(hr))) {
    return nullptr;
  }

  ComPtr<IInputPane2> inputPane2;
  inputPane.As(&inputPane2);
  return inputPane2;
}

#  ifdef DEBUG
static bool IsInputPaneVisible(ComPtr<IInputPane2>& aInputPane2) {
  ComPtr<IInputPaneControl> inputPaneControl;
  aInputPane2.As(&inputPaneControl);
  if (NS_WARN_IF(!inputPaneControl)) {
    return false;
  }
  boolean visible = false;
  inputPaneControl->get_Visible(&visible);
  return visible;
}

static bool IsForegroundApp() {
  HWND foregroundWnd = ::GetForegroundWindow();
  if (!foregroundWnd) {
    return false;
  }
  DWORD pid;
  ::GetWindowThreadProcessId(foregroundWnd, &pid);
  return pid == ::GetCurrentProcessId();
}
#  endif
#endif

// static
void OSKInputPaneManager::ShowOnScreenKeyboard(HWND aWnd) {
#ifndef __MINGW32__
  ComPtr<IInputPane2> inputPane2 = GetInputPane(aWnd);
  if (!inputPane2) {
    return;
  }
  boolean result;
  inputPane2->TryShow(&result);
  NS_WARNING_ASSERTION(
      result || !IsForegroundApp() || IsInputPaneVisible(inputPane2),
      "IInputPane2::TryShow is failure");
#endif
}

// static
void OSKInputPaneManager::DismissOnScreenKeyboard(HWND aWnd) {
#ifndef __MINGW32__
  ComPtr<IInputPane2> inputPane2 = GetInputPane(aWnd);
  if (!inputPane2) {
    return;
  }
  boolean result;
  inputPane2->TryHide(&result);
  NS_WARNING_ASSERTION(
      result || !IsForegroundApp() || !IsInputPaneVisible(inputPane2),
      "IInputPane2::TryHide is failure");
#endif
}

}  // namespace widget
}  // namespace mozilla