2013-10-22 17:27:35 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "nsWindowBase.h"
|
|
|
|
|
|
|
|
#include "mozilla/MiscEvents.h"
|
2019-04-13 04:05:21 +03:00
|
|
|
#include "mozilla/PresShell.h"
|
2019-07-26 04:10:23 +03:00
|
|
|
#include "mozilla/StaticPrefs_apz.h"
|
2014-12-30 07:57:40 +03:00
|
|
|
#include "KeyboardLayout.h"
|
2013-12-15 00:40:56 +04:00
|
|
|
#include "WinUtils.h"
|
2013-10-22 17:27:35 +04:00
|
|
|
#include "npapi.h"
|
2016-04-19 23:51:25 +03:00
|
|
|
#include "nsAutoPtr.h"
|
2013-10-22 17:27:35 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2013-12-15 00:40:56 +04:00
|
|
|
using namespace mozilla::widget;
|
|
|
|
|
|
|
|
static const wchar_t kUser32LibName[] = L"user32.dll";
|
|
|
|
bool nsWindowBase::sTouchInjectInitialized = false;
|
|
|
|
InjectTouchInputPtr nsWindowBase::sInjectTouchFuncPtr;
|
2013-10-22 17:27:35 +04:00
|
|
|
|
|
|
|
bool nsWindowBase::DispatchPluginEvent(const MSG& aMsg) {
|
2016-09-14 18:16:18 +03:00
|
|
|
if (!ShouldDispatchPluginEvent()) {
|
2013-10-22 17:27:35 +04:00
|
|
|
return false;
|
|
|
|
}
|
2015-09-08 17:33:38 +03:00
|
|
|
WidgetPluginEvent pluginEvent(true, ePluginInputEvent, this);
|
2015-11-10 08:37:32 +03:00
|
|
|
LayoutDeviceIntPoint point(0, 0);
|
2013-10-22 17:27:35 +04:00
|
|
|
InitEvent(pluginEvent, &point);
|
|
|
|
NPEvent npEvent;
|
|
|
|
npEvent.event = aMsg.message;
|
|
|
|
npEvent.wParam = aMsg.wParam;
|
|
|
|
npEvent.lParam = aMsg.lParam;
|
2014-07-23 23:55:51 +04:00
|
|
|
pluginEvent.mPluginEvent.Copy(npEvent);
|
2016-03-30 12:07:50 +03:00
|
|
|
pluginEvent.mRetargetToFocusedDocument = true;
|
2013-10-22 17:27:35 +04:00
|
|
|
return DispatchWindowEvent(&pluginEvent);
|
|
|
|
}
|
2013-12-15 00:40:56 +04:00
|
|
|
|
2016-09-14 18:16:18 +03:00
|
|
|
bool nsWindowBase::ShouldDispatchPluginEvent() { return PluginHasFocus(); }
|
|
|
|
|
2013-12-15 00:40:56 +04:00
|
|
|
// static
|
|
|
|
bool nsWindowBase::InitTouchInjection() {
|
|
|
|
if (!sTouchInjectInitialized) {
|
|
|
|
// Initialize touch injection on the first call
|
|
|
|
HMODULE hMod = LoadLibraryW(kUser32LibName);
|
|
|
|
if (!hMod) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
InitializeTouchInjectionPtr func =
|
|
|
|
(InitializeTouchInjectionPtr)GetProcAddress(hMod,
|
|
|
|
"InitializeTouchInjection");
|
|
|
|
if (!func) {
|
|
|
|
WinUtils::Log("InitializeTouchInjection not available.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!func(TOUCH_INJECT_MAX_POINTS, TOUCH_FEEDBACK_DEFAULT)) {
|
|
|
|
WinUtils::Log("InitializeTouchInjection failure. GetLastError=%d",
|
|
|
|
GetLastError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sInjectTouchFuncPtr =
|
|
|
|
(InjectTouchInputPtr)GetProcAddress(hMod, "InjectTouchInput");
|
|
|
|
if (!sInjectTouchFuncPtr) {
|
|
|
|
WinUtils::Log("InjectTouchInput not available.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sTouchInjectInitialized = true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-15 13:39:36 +03:00
|
|
|
bool nsWindowBase::InjectTouchPoint(uint32_t aId, LayoutDeviceIntPoint& aPoint,
|
2013-12-15 00:40:56 +04:00
|
|
|
POINTER_FLAGS aFlags, uint32_t aPressure,
|
|
|
|
uint32_t aOrientation) {
|
|
|
|
if (aId > TOUCH_INJECT_MAX_POINTS) {
|
|
|
|
WinUtils::Log("Pointer ID exceeds maximum. See TOUCH_INJECT_MAX_POINTS.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
POINTER_TOUCH_INFO info;
|
|
|
|
memset(&info, 0, sizeof(POINTER_TOUCH_INFO));
|
|
|
|
|
|
|
|
info.touchFlags = TOUCH_FLAG_NONE;
|
|
|
|
info.touchMask =
|
|
|
|
TOUCH_MASK_CONTACTAREA | TOUCH_MASK_ORIENTATION | TOUCH_MASK_PRESSURE;
|
|
|
|
info.pressure = aPressure;
|
|
|
|
info.orientation = aOrientation;
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2013-12-15 00:40:56 +04:00
|
|
|
info.pointerInfo.pointerFlags = aFlags;
|
|
|
|
info.pointerInfo.pointerType = PT_TOUCH;
|
|
|
|
info.pointerInfo.pointerId = aId;
|
2016-04-15 13:39:36 +03:00
|
|
|
info.pointerInfo.ptPixelLocation.x = aPoint.x;
|
|
|
|
info.pointerInfo.ptPixelLocation.y = aPoint.y;
|
2013-12-15 00:40:56 +04:00
|
|
|
|
|
|
|
info.rcContact.top = info.pointerInfo.ptPixelLocation.y - 2;
|
|
|
|
info.rcContact.bottom = info.pointerInfo.ptPixelLocation.y + 2;
|
|
|
|
info.rcContact.left = info.pointerInfo.ptPixelLocation.x - 2;
|
|
|
|
info.rcContact.right = info.pointerInfo.ptPixelLocation.x + 2;
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2019-04-17 19:53:29 +03:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
if (sInjectTouchFuncPtr(1, &info)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
DWORD error = GetLastError();
|
|
|
|
if (error == ERROR_NOT_READY && i < 2) {
|
|
|
|
// We sent it too quickly after the previous injection (see bug 1535140
|
|
|
|
// comment 10). On the first loop iteration we just yield (via Sleep(0))
|
|
|
|
// and try again. If it happens again on the second loop iteration we
|
|
|
|
// explicitly Sleep(1) and try again. If that doesn't work either we just
|
|
|
|
// error out.
|
|
|
|
::Sleep(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
WinUtils::Log("InjectTouchInput failure. GetLastError=%d", error);
|
2013-12-15 00:40:56 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-04 19:59:14 +03:00
|
|
|
void nsWindowBase::ChangedDPI() {
|
|
|
|
if (mWidgetListener) {
|
2019-04-13 04:05:21 +03:00
|
|
|
if (PresShell* presShell = mWidgetListener->GetPresShell()) {
|
2015-12-04 19:59:14 +03:00
|
|
|
presShell->BackingScaleFactorChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-15 00:40:56 +04:00
|
|
|
nsresult nsWindowBase::SynthesizeNativeTouchPoint(
|
|
|
|
uint32_t aPointerId, nsIWidget::TouchPointerState aPointerState,
|
2016-04-15 13:39:36 +03:00
|
|
|
LayoutDeviceIntPoint aPoint, double aPointerPressure,
|
2015-04-14 18:36:36 +03:00
|
|
|
uint32_t aPointerOrientation, nsIObserver* aObserver) {
|
|
|
|
AutoObserverNotifier notifier(aObserver, "touchpoint");
|
|
|
|
|
2019-06-24 06:57:06 +03:00
|
|
|
if (StaticPrefs::apz_test_fails_with_native_injection() ||
|
|
|
|
!InitTouchInjection()) {
|
2016-10-31 17:05:15 +03:00
|
|
|
// If we don't have touch injection from the OS, or if we are running a test
|
|
|
|
// that cannot properly inject events to satisfy the OS requirements (see
|
|
|
|
// bug 1313170) we can just fake it and synthesize the events from here.
|
2016-08-03 19:06:59 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (aPointerState == TOUCH_HOVER) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mSynthesizedTouchInput) {
|
|
|
|
mSynthesizedTouchInput = MakeUnique<MultiTouchInput>();
|
|
|
|
}
|
|
|
|
|
|
|
|
WidgetEventTime time = CurrentMessageWidgetEventTime();
|
|
|
|
LayoutDeviceIntPoint pointInWindow = aPoint - WidgetToScreenOffset();
|
|
|
|
MultiTouchInput inputToDispatch = UpdateSynthesizedTouchState(
|
|
|
|
mSynthesizedTouchInput.get(), time.mTime, time.mTimeStamp, aPointerId,
|
|
|
|
aPointerState, pointInWindow, aPointerPressure, aPointerOrientation);
|
|
|
|
DispatchTouchInput(inputToDispatch);
|
|
|
|
return NS_OK;
|
2013-12-15 00:40:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool hover = aPointerState & TOUCH_HOVER;
|
|
|
|
bool contact = aPointerState & TOUCH_CONTACT;
|
|
|
|
bool remove = aPointerState & TOUCH_REMOVE;
|
|
|
|
bool cancel = aPointerState & TOUCH_CANCEL;
|
|
|
|
|
|
|
|
// win api expects a value from 0 to 1024. aPointerPressure is a value
|
|
|
|
// from 0.0 to 1.0.
|
|
|
|
uint32_t pressure = (uint32_t)ceil(aPointerPressure * 1024);
|
|
|
|
|
|
|
|
// If we already know about this pointer id get it's record
|
|
|
|
PointerInfo* info = mActivePointers.Get(aPointerId);
|
|
|
|
|
|
|
|
// We know about this pointer, send an update
|
|
|
|
if (info) {
|
|
|
|
POINTER_FLAGS flags = POINTER_FLAG_UPDATE;
|
|
|
|
if (hover) {
|
|
|
|
flags |= POINTER_FLAG_INRANGE;
|
|
|
|
} else if (contact) {
|
|
|
|
flags |= POINTER_FLAG_INCONTACT | POINTER_FLAG_INRANGE;
|
|
|
|
} else if (remove) {
|
|
|
|
flags = POINTER_FLAG_UP;
|
|
|
|
// Remove the pointer from our tracking list. This is nsAutPtr wrapped,
|
|
|
|
// so shouldn't leak.
|
|
|
|
mActivePointers.Remove(aPointerId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cancel) {
|
|
|
|
flags |= POINTER_FLAG_CANCELED;
|
|
|
|
}
|
|
|
|
|
2016-04-15 13:39:36 +03:00
|
|
|
return !InjectTouchPoint(aPointerId, aPoint, flags, pressure,
|
2013-12-15 00:40:56 +04:00
|
|
|
aPointerOrientation)
|
|
|
|
? NS_ERROR_UNEXPECTED
|
|
|
|
: NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Missing init state, error out
|
|
|
|
if (remove || cancel) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new pointer
|
2016-04-15 13:39:36 +03:00
|
|
|
info = new PointerInfo(aPointerId, aPoint);
|
2013-12-15 00:40:56 +04:00
|
|
|
|
|
|
|
POINTER_FLAGS flags = POINTER_FLAG_INRANGE;
|
|
|
|
if (contact) {
|
|
|
|
flags |= POINTER_FLAG_INCONTACT | POINTER_FLAG_DOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
mActivePointers.Put(aPointerId, info);
|
2016-04-15 13:39:36 +03:00
|
|
|
return !InjectTouchPoint(aPointerId, aPoint, flags, pressure,
|
2013-12-15 00:40:56 +04:00
|
|
|
aPointerOrientation)
|
|
|
|
? NS_ERROR_UNEXPECTED
|
|
|
|
: NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-04-14 18:36:36 +03:00
|
|
|
nsresult nsWindowBase::ClearNativeTouchSequence(nsIObserver* aObserver) {
|
|
|
|
AutoObserverNotifier notifier(aObserver, "cleartouch");
|
2013-12-15 00:40:56 +04:00
|
|
|
if (!sTouchInjectInitialized) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cancel all input points
|
2015-11-23 05:52:40 +03:00
|
|
|
for (auto iter = mActivePointers.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
nsAutoPtr<PointerInfo>& info = iter.Data();
|
|
|
|
InjectTouchPoint(info.get()->mPointerId, info.get()->mPosition,
|
|
|
|
POINTER_FLAG_CANCELED);
|
|
|
|
iter.Remove();
|
|
|
|
}
|
2013-12-15 00:40:56 +04:00
|
|
|
|
2015-04-14 18:36:36 +03:00
|
|
|
nsBaseWidget::ClearNativeTouchSequence(nullptr);
|
2013-12-15 00:40:56 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-12-30 04:47:56 +03:00
|
|
|
bool nsWindowBase::HandleAppCommandMsg(const MSG& aAppCommandMsg,
|
2014-02-05 19:36:59 +04:00
|
|
|
LRESULT* aRetValue) {
|
2014-12-30 04:47:56 +03:00
|
|
|
ModifierKeyState modKeyState;
|
|
|
|
NativeKey nativeKey(this, aAppCommandMsg, modKeyState);
|
|
|
|
bool consumed = nativeKey.HandleAppCommandMessage();
|
|
|
|
*aRetValue = consumed ? 1 : 0;
|
|
|
|
return consumed;
|
2014-02-05 19:36:59 +04:00
|
|
|
}
|