Blender V5.3
GHOST_TrackpadWin32.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cmath>
10
11#include "GHOST_Debug.hh"
13
14GHOST_DirectManipulationHelper::GHOST_DirectManipulationHelper(
15 HWND hWnd,
16 Microsoft::WRL::ComPtr<IDirectManipulationManager> directManipulationManager,
17 Microsoft::WRL::ComPtr<IDirectManipulationUpdateManager> directManipulationUpdateManager,
18 Microsoft::WRL::ComPtr<IDirectManipulationViewport> directManipulationViewport,
19 Microsoft::WRL::ComPtr<GHOST_DirectManipulationViewportEventHandler>
20 directManipulationEventHandler,
21 DWORD directManipulationViewportHandlerCookie,
22 bool isScrollDirectionInverted)
23 : h_wnd_(hWnd),
24 scroll_direction_reg_key_(nullptr),
25 scroll_direction_change_event_(nullptr),
26 direct_manipulation_manager_(directManipulationManager),
27 direct_manipulation_update_manager_(directManipulationUpdateManager),
28 direct_manipulation_viewport_(directManipulationViewport),
29 direct_manipulation_event_handler_(directManipulationEventHandler),
30 direct_manipulation_viewport_handler_cookie_(directManipulationViewportHandlerCookie),
31 is_scroll_direction_inverted_(isScrollDirectionInverted)
32{
33}
34
35GHOST_DirectManipulationHelper *GHOST_DirectManipulationHelper::create(HWND hWnd, uint16_t dpi)
36{
37#define DM_CHECK_RESULT_AND_EXIT_EARLY(hr, failMessage) \
38 { \
39 if (!SUCCEEDED(hr)) { \
40 GHOST_PRINT(failMessage); \
41 return nullptr; \
42 } \
43 }
44
45 Microsoft::WRL::ComPtr<IDirectManipulationManager> directManipulationManager;
46 HRESULT hr = ::CoCreateInstance(CLSID_DirectManipulationManager,
47 nullptr,
48 CLSCTX_INPROC_SERVER,
49 IID_PPV_ARGS(&directManipulationManager));
50 DM_CHECK_RESULT_AND_EXIT_EARLY(hr, "DirectManipulationManager create failed\n");
51
52 /* Since we want to use fake viewport, we need to send fake updates to UpdateManager. */
53 Microsoft::WRL::ComPtr<IDirectManipulationUpdateManager> directManipulationUpdateManager;
54 hr = directManipulationManager->GetUpdateManager(IID_PPV_ARGS(&directManipulationUpdateManager));
55 DM_CHECK_RESULT_AND_EXIT_EARLY(hr, "Get UpdateManager failed\n");
56
57 Microsoft::WRL::ComPtr<IDirectManipulationViewport> directManipulationViewport;
58 hr = directManipulationManager->CreateViewport(
59 nullptr, hWnd, IID_PPV_ARGS(&directManipulationViewport));
60 DM_CHECK_RESULT_AND_EXIT_EARLY(hr, "Viewport create failed\n");
61
62 DIRECTMANIPULATION_CONFIGURATION configuration =
63 DIRECTMANIPULATION_CONFIGURATION_INTERACTION |
64 DIRECTMANIPULATION_CONFIGURATION_TRANSLATION_X |
65 DIRECTMANIPULATION_CONFIGURATION_TRANSLATION_Y |
66 DIRECTMANIPULATION_CONFIGURATION_TRANSLATION_INERTIA |
67 DIRECTMANIPULATION_CONFIGURATION_SCALING;
68
69 hr = directManipulationViewport->ActivateConfiguration(configuration);
70 DM_CHECK_RESULT_AND_EXIT_EARLY(hr, "Viewport set ActivateConfiguration failed\n");
71
72 /* Since we are using fake viewport and only want to use Direct Manipulation for touchpad, we
73 * need to use MANUALUPDATE option. */
74 hr = directManipulationViewport->SetViewportOptions(
75 DIRECTMANIPULATION_VIEWPORT_OPTIONS_MANUALUPDATE);
76 DM_CHECK_RESULT_AND_EXIT_EARLY(hr, "Viewport set ViewportOptions failed\n");
77
78 /* We receive Direct Manipulation transform updates in IDirectManipulationViewportEventHandler
79 * callbacks. */
80 Microsoft::WRL::ComPtr<GHOST_DirectManipulationViewportEventHandler>
81 directManipulationEventHandler =
82 Microsoft::WRL::Make<GHOST_DirectManipulationViewportEventHandler>(dpi);
83 DWORD directManipulationViewportHandlerCookie;
84 directManipulationViewport->AddEventHandler(
85 hWnd, directManipulationEventHandler.Get(), &directManipulationViewportHandlerCookie);
86 DM_CHECK_RESULT_AND_EXIT_EARLY(hr, "Viewport add EventHandler failed\n");
87
88 /* Set default rect for viewport before activating. */
89 RECT rect = {0, 0, 10000, 10000};
90 hr = directManipulationViewport->SetViewportRect(&rect);
91 DM_CHECK_RESULT_AND_EXIT_EARLY(hr, "Viewport set rect failed\n");
92
93 hr = directManipulationManager->Activate(hWnd);
94 DM_CHECK_RESULT_AND_EXIT_EARLY(hr, "DirectManipulationManager activate failed\n");
95
96 hr = directManipulationViewport->Enable();
97 DM_CHECK_RESULT_AND_EXIT_EARLY(hr, "Viewport enable failed\n");
98
99 directManipulationEventHandler->resetViewport(directManipulationViewport.Get());
100
101 bool isScrollDirectionInverted = getScrollDirectionFromReg();
102
103 auto instance = new GHOST_DirectManipulationHelper(hWnd,
104 directManipulationManager,
105 directManipulationUpdateManager,
106 directManipulationViewport,
107 directManipulationEventHandler,
108 directManipulationViewportHandlerCookie,
109 isScrollDirectionInverted);
110
111 instance->registerScrollDirectionChangeListener();
112
113 return instance;
114
115#undef DM_CHECK_RESULT_AND_EXIT_EARLY
116}
117
118bool GHOST_DirectManipulationHelper::getScrollDirectionFromReg()
119{
120 DWORD scrollDirectionRegValue = 0;
121 DWORD pcbData = sizeof(scrollDirectionRegValue);
122 HRESULT hr = HRESULT_FROM_WIN32(
123 RegGetValueW(HKEY_CURRENT_USER,
124 L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\PrecisionTouchPad\\",
125 L"ScrollDirection",
126 RRF_RT_REG_DWORD,
127 nullptr,
128 &scrollDirectionRegValue,
129 &pcbData));
130 if (!SUCCEEDED(hr)) {
131 GHOST_PRINT("Failed to get scroll direction from registry\n");
132 /* This is the default value for scroll direction, not a failure/success indicator. */
133 return true;
134 }
135
136 /* Return false for natural scrolling direction (upward motion results in content scrolling
137 * downward), and true for reversed direction. */
138 return scrollDirectionRegValue == 0;
139}
140
141void GHOST_DirectManipulationHelper::registerScrollDirectionChangeListener()
142{
143
144 if (!scroll_direction_reg_key_) {
145 HRESULT hr = HRESULT_FROM_WIN32(
146 RegOpenKeyExW(HKEY_CURRENT_USER,
147 L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\PrecisionTouchPad\\",
148 0,
149 KEY_NOTIFY,
150 &scroll_direction_reg_key_));
151 if (!SUCCEEDED(hr)) {
152 GHOST_PRINT("Failed to open scroll direction registry key\n");
153 return;
154 }
155 }
156
157 if (!scroll_direction_change_event_) {
158 scroll_direction_change_event_ = CreateEventW(nullptr, true, false, nullptr);
159 }
160 else {
161 ResetEvent(scroll_direction_change_event_);
162 }
163 HRESULT hr = HRESULT_FROM_WIN32(RegNotifyChangeKeyValue(scroll_direction_reg_key_,
164 true,
165 REG_NOTIFY_CHANGE_LAST_SET,
166 scroll_direction_change_event_,
167 true));
168 if (!SUCCEEDED(hr)) {
169 GHOST_PRINT("Failed to register scroll direction change listener\n");
170 return;
171 }
172}
173
175{
176 [[maybe_unused]] HRESULT hr = direct_manipulation_viewport_->SetContact(pointerId);
177 GHOST_ASSERT(SUCCEEDED(hr), "Viewport set contact failed\n");
178
179 if (WaitForSingleObject(scroll_direction_change_event_, 0) == WAIT_OBJECT_0) {
180 is_scroll_direction_inverted_ = getScrollDirectionFromReg();
181 registerScrollDirectionChangeListener();
182 }
183}
184
186{
187 if (direct_manipulation_event_handler_->dm_status == DIRECTMANIPULATION_RUNNING ||
188 direct_manipulation_event_handler_->dm_status == DIRECTMANIPULATION_INERTIA)
189 {
190 [[maybe_unused]] HRESULT hr = direct_manipulation_update_manager_->Update(nullptr);
191 GHOST_ASSERT(SUCCEEDED(hr), "DirectManipulationUpdateManager update failed\n");
192 }
193}
194
196{
197 direct_manipulation_event_handler_->dpi = dpi;
198}
199
201{
202 GHOST_TTrackpadInfo result = direct_manipulation_event_handler_->accumulated_values;
203 result.isScrollDirectionInverted = is_scroll_direction_inverted_;
204
205 direct_manipulation_event_handler_->accumulated_values = {0, 0, 0};
206 return result;
207}
208
210{
211 [[maybe_unused]] HRESULT hr;
212 hr = direct_manipulation_viewport_->Stop();
213 GHOST_ASSERT(SUCCEEDED(hr), "Viewport stop failed\n");
214
215 hr = direct_manipulation_viewport_->RemoveEventHandler(
216 direct_manipulation_viewport_handler_cookie_);
217 GHOST_ASSERT(SUCCEEDED(hr), "Viewport remove event handler failed\n");
218
219 hr = direct_manipulation_viewport_->Abandon();
220 GHOST_ASSERT(SUCCEEDED(hr), "Viewport abandon failed\n");
221
222 hr = direct_manipulation_manager_->Deactivate(h_wnd_);
223 GHOST_ASSERT(SUCCEEDED(hr), "DirectManipulationManager deactivate failed\n");
224
225 if (scroll_direction_change_event_) {
226 CloseHandle(scroll_direction_change_event_);
227 scroll_direction_change_event_ = nullptr;
228 }
229 if (scroll_direction_reg_key_) {
230 RegCloseKey(scroll_direction_reg_key_);
231 scroll_direction_reg_key_ = nullptr;
232 }
233}
234
236 uint16_t dpi)
237 : accumulated_values({0, 0, 0}), dpi(dpi), dm_status(DIRECTMANIPULATION_BUILDING)
238{
239}
240
242 IDirectManipulationViewport *viewport)
243{
244 if (gesture_state != GESTURE_NONE) {
245 [[maybe_unused]] HRESULT hr = viewport->ZoomToRect(0.0f, 0.0f, 10000.0f, 10000.0f, FALSE);
246 GHOST_ASSERT(SUCCEEDED(hr), "Viewport reset failed\n");
247 }
248
249 gesture_state = GESTURE_NONE;
250
251 last_scale = PINCH_SCALE_FACTOR;
252 last_x = 0.0f;
253 last_y = 0.0f;
254}
255
257 IDirectManipulationViewport *viewport,
258 DIRECTMANIPULATION_STATUS current,
259 DIRECTMANIPULATION_STATUS previous)
260{
261 dm_status = current;
262
263 if (current == previous) {
264 return S_OK;
265 }
266
267 if (previous == DIRECTMANIPULATION_ENABLED || current == DIRECTMANIPULATION_READY ||
268 (previous == DIRECTMANIPULATION_INERTIA && current != DIRECTMANIPULATION_INERTIA))
269 {
270 resetViewport(viewport);
271 }
272
273 return S_OK;
274}
275
277 IDirectManipulationViewport * /*viewport*/)
278{
279 /* Nothing to do here. */
280 return S_OK;
281}
282
284 IDirectManipulationViewport * /*viewport*/, IDirectManipulationContent *content)
285{
286 float transform[6];
287 HRESULT hr = content->GetContentTransform(transform, ARRAYSIZE(transform));
288 GHOST_ASSERT(SUCCEEDED(hr), "DirectManipulationContent get transform failed\n");
289
290 const float device_scale_factor = dpi / 96.0f;
291
292 const float scale = transform[0] * PINCH_SCALE_FACTOR;
293 const float x = transform[4] / device_scale_factor;
294 const float y = transform[5] / device_scale_factor;
295
296 const float EPS = 3e-5;
297
298 /* Ignore repeating or incorrect input. */
299 if ((fabs(scale - last_scale) <= EPS && fabs(x - last_x) <= EPS && fabs(y - last_y) <= EPS) ||
300 scale == 0.0f)
301 {
302 GHOST_PRINT("Ignoring touchpad input\n");
303 return hr;
304 }
305
306 /* Assume that every gesture is a pan in the beginning.
307 * If it's a pinch, the gesture will be changed below. */
308 if (gesture_state == GESTURE_NONE) {
309 gesture_state = GESTURE_PAN;
310 }
311
312 /* DM doesn't always immediately recognize pinch gestures,
313 * so allow transition from pan to pinch. */
314 if (gesture_state == GESTURE_PAN) {
315 if (fabs(scale - PINCH_SCALE_FACTOR) > EPS) {
316 gesture_state = GESTURE_PINCH;
317 }
318 }
319
320 /* This state machine is used here because:
321 * 1. Pinch and pan gestures must be differentiated and cannot be processed at the same time
322 * because XY transform values become nonsensical during pinch gesture.
323 * 2. GHOST requires delta values for events while DM provides transformation matrix of the
324 * current gesture.
325 * 3. GHOST events accept integer values while DM values are non-integer.
326 * Truncated fractional parts are accumulated and accounted for in following updates.
327 */
328 switch (gesture_state) {
329 case GESTURE_PINCH: {
330 int32_t dscale = roundf(scale - last_scale);
331
332 last_scale += dscale;
333
334 accumulated_values.scale += dscale;
335 break;
336 }
337 case GESTURE_PAN: {
338 int32_t dx = roundf(x - last_x);
339 int32_t dy = roundf(y - last_y);
340
341 last_x += dx;
342 last_y += dy;
343
344 accumulated_values.x += dx;
345 accumulated_values.y += dy;
346 break;
347 }
348 case GESTURE_NONE:
349 break;
350 }
351
352 return hr;
353}
#define GHOST_ASSERT(x, info)
#define GHOST_PRINT(x)
#define DM_CHECK_RESULT_AND_EXIT_EARLY(hr, failMessage)
#define PINCH_SCALE_FACTOR
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
void onPointerHitTest(UINT32 pointerId)
static GHOST_DirectManipulationHelper * create(HWND hWnd, uint16_t dpi)
void resetViewport(IDirectManipulationViewport *viewport)
HRESULT STDMETHODCALLTYPE OnContentUpdated(IDirectManipulationViewport *viewport, IDirectManipulationContent *content) override
HRESULT STDMETHODCALLTYPE OnViewportUpdated(IDirectManipulationViewport *viewport) override
HRESULT STDMETHODCALLTYPE OnViewportStatusChanged(IDirectManipulationViewport *viewport, DIRECTMANIPULATION_STATUS current, DIRECTMANIPULATION_STATUS previous) override
#define roundf(x)
float3x3 scale(float3x3 mat, float2 scale)
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
#define L
#define EPS
Definition unit.cc:44