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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
|
//
// Definition of Windows Pen/Tablet event driver.
//
// Copyright 2025 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
// https://www.fltk.org/COPYING.php
//
// Please see the following page on how to report bugs and issues:
//
// https://www.fltk.org/bugs.php
//
#include "src/drivers/Base/Fl_Base_Pen_Events.H"
#include <FL/platform.H>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include "../../Fl_Screen_Driver.H"
#include <windows.h>
#include <ole2.h>
#include <shellapi.h>
// Some versions of MinGW now require us to explicitly include winerror to get S_OK defined
#include <winerror.h>
#if 0 // Base the Windows driver on the existing Cocoa driver for now.
#import <Cocoa/Cocoa.h>
extern Fl_Window *fl_xmousewin;
static NSPointingDeviceType device_type_ { NSPointingDeviceTypePen };
// The trait list keeps track of traits for every pen ID that appears while
// handling events.
// AppKit does not tell us what traits are available per pen or tablet, so
// we use the first 5 motion events to discover event values that are not
// the default value, and enter that knowledge into the traits database.
typedef std::map<int, Fl::Pen::Trait> TraitList;
static TraitList trait_list_;
static int trait_countdown_ { 5 };
static int current_pen_id_ { -1 };
static Fl::Pen::Trait current_pen_trait_ { Fl::Pen::Trait::DRIVER_AVAILABLE };
static Fl::Pen::Trait driver_traits_ {
Fl::Pen::Trait::DRIVER_AVAILABLE | Fl::Pen::Trait::PEN_ID |
Fl::Pen::Trait::ERASER | Fl::Pen::Trait::PRESSURE |
Fl::Pen::Trait::BARREL_PRESSURE | Fl::Pen::Trait::TILT_X |
Fl::Pen::Trait::TILT_Y | Fl::Pen::Trait::TWIST
// Notably missing: PROXIMITY
};
#endif
// Temporary storage of event data for the driver;
static Fl::Pen::EventData ev;
namespace Fl {
// namespace Private {
// // Global mouse position at mouse down event
// extern int e_x_down;
// extern int e_y_down;
// }; // namespace Private
namespace Pen {
class Windows_Driver : public Driver {
public:
Windows_Driver() = default;
//virtual void subscribe(Fl_Widget* widget) override;
//virtual void unsubscribe(Fl_Widget* widget) override;
//virtual void release() override;
// virtual Trait traits() override { return driver_traits_; }
// virtual Trait pen_traits(int pen_id) override {
// auto it = trait_list_.find(pen_id);
// if (pen_id == 0)
// return current_pen_trait_;
// if (it == trait_list_.end()) {
// return Trait::DRIVER_AVAILABLE;
// } else {
// return it->second;
// }
// }
};
Windows_Driver windows_driver;
Driver& driver { windows_driver };
} // namespace Pen
} // namespace Fl
using namespace Fl::Pen;
#if 0
/*
Copy the event state.
*/
static void copy_state() {
Fl::Pen::State tr = (Fl::Pen::State)((uint32_t)Fl::Pen::e.state ^ (uint32_t)ev.state);
Fl::Pen::e = ev;
Fl::Pen::e.trigger = tr;
Fl::e_x = (int)ev.x;
Fl::e_y = (int)ev.y;
Fl::e_x_root = (int)ev.rx;
Fl::e_y_root = (int)ev.ry;
}
/*
Offset coordinates for subwindows and subsubwindows.
*/
static void offset_subwindow_event(Fl_Widget *w, double &x, double &y) {
Fl_Widget *p = w, *q;
while (p) {
q = p->parent();
if (p->as_window() && q) {
x -= p->x();
y -= p->y();
}
p = q;
};
}
/*
Check if coordinates are within the widget box.
Coordinates are in top_window space. We iterate up the hierarchy to ensure
that we handle subwindows correctly.
*/
static bool event_inside(Fl_Widget *w, double x, double y) {
offset_subwindow_event(w, x, y);
if (w->as_window()) {
return ((x >= 0) && (y >= 0) && (x < w->w()) && (y < w->h()));
} else {
return ((x >= w->x()) && (y >= w->y()) && (x < w->x() + w->w()) && (y < w->y() + w->h()));
}
}
/*
Find the widget under the pen event.
Search the subscriber list for widgets that are inside the same top window,
are visible, and are within the give coordinates. Subwindow aware.
*/
static Fl_Widget *find_below_pen(Fl_Window *win, double x, double y) {
for (auto &sub: subscriber_list_) {
Fl_Widget *candidate = sub.second->widget();
if (candidate && (candidate->top_window() == win)) {
if (candidate->visible() && event_inside(candidate, x, y)) {
return candidate;
}
}
}
return nullptr;
}
/*
Send the current event and event data to a widget.
Note: we will get the wrong coordinates if the widget is not a child of
the current event window (LEAVE events between windows).
*/
static int pen_send(Fl_Widget *w, int event, State trigger, bool &copied) {
// Copy most event data only once
if (!copied) {
copy_state();
copied = true;
}
// Copy the top_window coordinates again as they may change when w changes
e.x = ev.x;
e.y = ev.y;
offset_subwindow_event(w, e.x, e.y);
Fl::e_x = e.x;
Fl::e_y = e.y;
// Send the event.
e.trigger = trigger;
return w->handle(event);
}
/*
Send an event to all subscribers.
*/
static int pen_send_all(int event, State trigger) {
bool copied = false;
// use local value because handler may still change ev values
for (auto &it: subscriber_list_) {
auto w = it.second->widget();
if (w)
pen_send(w, event, trigger, copied);
}
return 1;
}
/*
Convert the NSEvent button number to Fl::Pen::State,
*/
static State button_to_trigger(NSInteger button, bool down) {
switch (button) {
case 0:
if ( (ev.state & (State::ERASER_DOWN | State::ERASER_HOVERS)) != State::NONE ) {
return down ? State::ERASER_DOWN : State::ERASER_HOVERS;
} else {
return down ? State::TIP_DOWN : State::TIP_HOVERS;
}
case 1: return State::BUTTON0;
case 2: return State::BUTTON1;
case 3: return State::BUTTON2;
case 4: return State::BUTTON3;
default: return State::NONE;
}
}
#endif
/*
Handle events coming from the Win32 API.
WM_TABLET (Windows 2000 and up)
WM_POINTER (Windows 8 and up)
https://learn.microsoft.com/en-us/windows/win32/inputmsg/messages-and-notifications-portal
#if(WINVER >= 0x0602) ... #endif
EnableMouseInPointer(FALSE);
GET_X_LPARAM(lParam): the x (horizontal point) coordinate.
GET_Y_LPARAM(lParam): the y (vertical point) coordinate.
\return -1 if we did not handle the event and want the main event handler to call DefWindowProc()
\return any other value that will then be return from WndProc() directly.
*/
LRESULT fl_win32_tablet_handler(MSG& msg) {
auto message = msg.message;
if (message < WM_NCPOINTERUPDATE || message > WM_POINTERROUTEDRELEASED) {
return -1;
}
Fl_Window *window = fl_find(msg.hwnd); // can be nullptr
switch (msg.message) {
case WM_NCPOINTERDOWN: // pen pushed over window decoration, don't care
case WM_NCPOINTERUP: // pen released over window decoration, don't care
case WM_NCPOINTERUPDATE: // pen moved over decoration, don't care
case WM_POINTERACTIVATE: // shall the pointer activate an inactive window?
return -1; // let the system handle this forwarding this to DefWindowProc
case WM_POINTERENTER: // pointer moved into window area from top or sides
// position
// GET_POINTERID_WPARAM(wParam): the pointer identifier.
// IS_POINTER_NEW_WPARAM(wParam): indicates whether this message is the first message generated by a new pointer entering detection range (hover).
// IS_POINTER_INRANGE_WPARAM(wParam): indicates whether this message was generated by a pointer that has not left detection range. This flag is always set for WM_POINTERENTER messages.
// IS_POINTER_INCONTACT_WPARAM(wParam): a flag that indicates whether this message was generated by a pointer that is in contact. This flag is not set for a pointer in detection range (hover).
puts("Windows message: WM_POINTERENTER"); break;
case WM_POINTERLEAVE: // left window area to top or sides
// position
// GET_POINTERID_WPARAM(wParam): the pointer identifier.
// IS_POINTER_INRANGE_WPARAM(wParam): indicates whether this message was generated by a pointer that has not left detection range. This flag is not set when the pointer leaves the detection range of the window.
// IS_POINTER_INCONTACT_WPARAM(wParam): a flag that indicates whether this message was generated by a pointer that is in contact. This flag is not set for a pointer in detection range (hover).
puts("Windows message: WM_POINTERLEAVE"); break;
case WM_POINTERDOWN:
puts("Windows message: WM_POINTERDOWN"); break;
// location
// GET_POINTERID_WPARAM(wParam): the pointer identifier.
// IS_POINTER_NEW_WPARAM(wParam): a flag that indicates whether this message represents the first input generated by a new pointer.
// IS_POINTER_INRANGE_WPARAM(wParam): a flag that indicates whether this message was generated by a pointer during its lifetime. This flag is not set on messages that indicate that the pointer has left detection range
// IS_POINTER_INCONTACT_WPARAM(wParam): a flag that indicates whether this message was generated by a pointer that is in contact with the window surface. This flag is not set on messages that indicate a hovering pointer.
// IS_POINTER_PRIMARY_WPARAM(wParam): indicates that this pointer has been designated as primary.
// IS_POINTER_FIRSTBUTTON_WPARAM(wParam): a flag that indicates whether there is a primary action.
// This is analogous to a mouse left button down.
// A touch pointer will have this set when it is in contact with the digitizer surface.
// A pen pointer will have this set when it is in contact with the digitizer surface with no buttons pressed.
// IS_POINTER_SECONDBUTTON_WPARAM(wParam): a flag that indicates whether there is a secondary action.
// This is analogous to a mouse right button down.
// A pen pointer will have this set when it is in contact with the digitizer surface with the pen barrel button pressed.
// IS_POINTER_THIRDBUTTON_WPARAM(wParam): a flag that indicates whether there are one or more tertiary actions based on the pointer type; applications that wish to respond to tertiary actions must retrieve information specific to the pointer type to determine which tertiary buttons are pressed. For example, an application can determine the buttons states of a pen by calling GetPointerPenInfo and examining the flags that specify button states.
// IS_POINTER_FOURTHBUTTON_WPARAM(wParam): a flag that indicates whether the specified pointer took fourth action. Applications that wish to respond to fourth actions must retrieve information specific to the pointer type to determine if the first extended mouse (XButton1) button is pressed.
// IS_POINTER_FIFTHBUTTON_WPARAM(wParam): a flag that indicates whether the specified pointer took fifth action. Applications that wish to respond to fifth actions must retrieve information specific to the pointer type to determine if the second extended mouse (XButton2) button is pressed.
// GetPointerInfo: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getpointerinfo
// GetKeyState: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getkeystate
case WM_POINTERUP:
puts("Windows message: WM_POINTERUP"); break;
case WM_POINTERUPDATE: return false;
puts("Windows message: WM_POINTERUPDATE"); break;
case WM_POINTERCAPTURECHANGED:
puts("Windows message: WM_POINTERCAPTURECHANGED"); break;
case WM_TOUCHHITTESTING:
puts("Windows message: WM_TOUCHHITTESTING"); break;
case WM_POINTERWHEEL:
puts("Windows message: WM_POINTERWHEEL"); break;
case WM_POINTERHWHEEL:
puts("Windows message: WM_POINTERHWHEEL"); break;
case DM_POINTERHITTEST:
puts("Windows message: DM_POINTERHITTEST"); break;
case WM_POINTERROUTEDTO:
puts("Windows message: WM_POINTERROUTEDTO"); break;
case WM_POINTERROUTEDAWAY:
puts("Windows message: WM_POINTERROUTEDAWAY"); break;
case WM_POINTERROUTEDRELEASED:
puts("Windows message: WM_POINTERROUTEDRELEASED"); break;
default:
printf("Windows message: msg=0x%04X wParam=0x%08X lParam=0x%08X\n",
msg.message, (unsigned)msg.wParam, (unsigned)msg.lParam);
return -1;
}
printf(" msg=0x%04X wParam=0x%08X lParam=0x%08X\n",
msg.message, (unsigned)msg.wParam, (unsigned)msg.lParam);
return -1;
}
#if 0
// Quick access to the main type.
auto type = [event type];
// There seems nothing useful here. Ignore for now.
if ((type == NSEventTypeMouseEntered) || (type == NSEventTypeMouseExited)) {
return false;
}
// Sort out tablet-only events and mouse plus tablet events.
bool is_mouse = ((type != NSEventTypeTabletPoint) && (type != NSEventTypeTabletProximity));
// Set the subtype if one is available. Only NSEventSubtypeTabletPoint and
// NSEventSubtypeTabletProximity matter in this context
NSEventSubtype subtype = is_mouse ? [event subtype] : NSEventSubtypeMouseEvent;
// Is this a change in proximity event?
bool is_proximity = ((type == NSEventTypeTabletProximity) || (subtype == NSEventSubtypeTabletProximity));
// Is this a pen pointer event?
bool is_point = ((type == NSEventTypeTabletPoint) || (subtype == NSEventSubtypeTabletPoint));
// Check if any of the pen down, move, drag, or up events was triggered.
bool is_down = ((type == NSEventTypeLeftMouseDown) || (type == NSEventTypeRightMouseDown) || (type == NSEventTypeOtherMouseDown));
bool is_up = ((type == NSEventTypeLeftMouseUp) || (type == NSEventTypeRightMouseUp) || (type == NSEventTypeOtherMouseUp));
bool is_drag = ((type == NSEventTypeLeftMouseDragged) || (type == NSEventTypeRightMouseDragged) || (type == NSEventTypeOtherMouseDragged));
bool is_motion = is_drag || (type == NSEventTypeMouseMoved);
// Find out if we can get the pen position
bool has_position = (eventWindow != nullptr) && (is_up || is_down || is_motion || is_proximity || is_point);
// Event has extended pen data set:
if (has_position) {
// Get the position data.
auto pt = [event locationInWindow];
double s = Fl::screen_driver()->scale(0);
ev.x = pt.x/s;
ev.y = eventWindow->h() - pt.y/s;
ev.rx = ev.x + eventWindow->x();
ev.ry = ev.y + eventWindow->y();
if (!is_proximity) {
// Get the pressure data.
ev.pressure = [event pressure];
ev.barrel_pressure = [event tangentialPressure];
// Get the tilt
auto tilt = [event tilt];
ev.tilt_x = -tilt.x;
ev.tilt_y = tilt.y;
// Other stuff
ev.twist = [event rotation]; // TODO: untested
// ev.proximity = [event proximity]; // not supported in AppKit
}
if (device_type_ == NSPointingDeviceTypeEraser) {
if ([event buttonMask] & 1)
ev.state = State::ERASER_DOWN;
else
ev.state = State::ERASER_HOVERS;
} else {
if ([event buttonMask] & 1)
ev.state = State::TIP_DOWN;
else
ev.state = State::TIP_HOVERS;
}
if ([event buttonMask] & 0x0002) ev.state |= State::BUTTON0;
if ([event buttonMask] & 0x0004) ev.state |= State::BUTTON1;
if ([event buttonMask] & 0x0008) ev.state |= State::BUTTON2;
if ([event buttonMask] & 0x0010) ev.state |= State::BUTTON3;
// printf("0x%08x\n", [event buttonMask]);
}
if (is_proximity) {
ev.pen_id = (int)[event vendorID];
device_type_ = [event pointingDeviceType];
}
if (type == NSEventTypeTabletProximity) {
if ([event isEnteringProximity]) {
// Check if this is the first time we see this pen, or if the pen changed
if (current_pen_id_ != ev.pen_id) {
current_pen_id_ = ev.pen_id;
auto it = trait_list_.find(current_pen_id_);
if (it == trait_list_.end()) { // not found, create a new entry
trait_list_[current_pen_id_] = Trait::DRIVER_AVAILABLE;
trait_countdown_ = 5;
pen_send_all(Fl::Pen::DETECTED, State::NONE);
// printf("IN RANGE, NEW PEN\n");
} else {
pen_send_all(Fl::Pen::CHANGED, State::NONE);
// printf("IN RANGE, CHANGED PEN\n");
}
trait_list_[0] = trait_list_[current_pen_id_]; // set current pen traits
} else {
pen_send_all(Fl::Pen::IN_RANGE, State::NONE);
// printf("IN RANGE\n");
}
} else {
pen_send_all(Fl::Pen::OUT_OF_RANGE, State::NONE);
// printf("OUT OF RANGE\n");
}
}
Fl_Widget *receiver = nullptr;
bool pushed = false;
bool event_data_copied = false;
if (has_position) {
if (trait_countdown_) {
trait_countdown_--;
if (ev.tilt_x != 0.0) current_pen_trait_ |= Trait::TILT_X;
if (ev.tilt_y != 0.0) current_pen_trait_ |= Trait::TILT_Y;
if (ev.pressure != 1.0) current_pen_trait_ |= Trait::PRESSURE;
if (ev.barrel_pressure != 0.0) current_pen_trait_ |= Trait::BARREL_PRESSURE;
if (ev.pen_id != 0) current_pen_trait_ |= Trait::PEN_ID;
if (ev.twist != 0.0) current_pen_trait_ |= Trait::TWIST;
//if (ev.proximity != 0) current_pen_trait_ |= Trait::PROXIMITY;
trait_list_[current_pen_id_] = current_pen_trait_;
}
fl_xmousewin = eventWindow;
if (pushed_ && pushed_->widget() && (Fl::pushed() == pushed_->widget())) {
receiver = pushed_->widget();
if (Fl::grab() && (Fl::grab() != receiver->top_window()))
return 0;
if (Fl::modal() && (Fl::modal() != receiver->top_window()))
return 0;
pushed = true;
} else {
if (Fl::grab() && (Fl::grab() != eventWindow))
return 0;
if (Fl::modal() && (Fl::modal() != eventWindow))
return 0;
auto bpen = below_pen_ ? below_pen_->widget() : nullptr;
auto bmouse = Fl::belowmouse();
auto bpen_old = bmouse && (bmouse == bpen) ? bpen : nullptr;
auto bpen_now = find_below_pen(eventWindow, ev.x, ev.y);
if (bpen_now != bpen_old) {
if (bpen_old) {
pen_send(bpen_old, Fl::Pen::LEAVE, State::NONE, event_data_copied);
}
below_pen_ = nullptr;
if (bpen_now) {
State state = (device_type_ == NSPointingDeviceTypeEraser) ? State::ERASER_HOVERS : State::TIP_HOVERS;
if (pen_send(bpen_now, Fl::Pen::ENTER, state, event_data_copied)) {
below_pen_ = subscriber_list_[bpen_now];
Fl::belowmouse(bpen_now);
}
}
}
receiver = below_pen_ ? below_pen_->widget() : nullptr;
if (!receiver)
return 0;
}
} else {
// Proximity events were handled earlier.
}
if (!receiver)
return 0;
if (is_down) {
if (!pushed) {
pushed_ = subscriber_list_[receiver];
Fl::pushed(receiver);
}
State trigger = button_to_trigger([event buttonNumber], true);
if ([event buttonNumber] == 0) {
Fl::e_is_click = 1;
Fl::Private::e_x_down = (int)ev.x;
Fl::Private::e_y_down = (int)ev.y;
if ([event clickCount] > 1)
Fl::e_clicks++;
else
Fl::e_clicks = 0;
pen_send(receiver, Fl::Pen::TOUCH, trigger, event_data_copied);
} else {
pen_send(receiver, Fl::Pen::BUTTON_PUSH, trigger, event_data_copied);
}
} else if (is_up) {
if ( (ev.state & State::ANY_DOWN) == State::NONE ) {
Fl::pushed(nullptr);
pushed_ = nullptr;
}
State trigger = button_to_trigger([event buttonNumber], true);
if ([event buttonNumber] == 0)
pen_send(receiver, Fl::Pen::LIFT, trigger, event_data_copied);
else
pen_send(receiver, Fl::Pen::BUTTON_RELEASE, trigger, event_data_copied);
} else if (is_motion) {
if ( Fl::e_is_click &&
( (fabs((int)ev.x - Fl::Private::e_x_down) > 5) ||
(fabs((int)ev.y - Fl::Private::e_y_down) > 5) ) )
Fl::e_is_click = 0;
if (pushed) {
pen_send(receiver, Fl::Pen::DRAW, State::NONE, event_data_copied);
} else {
pen_send(receiver, Fl::Pen::HOVER, State::NONE, event_data_copied);
}
}
// Always return 1 because at this point, we capture pen events and don't
// want mouse events anymore!
return 1;
}
#endif
|