-
Notifications
You must be signed in to change notification settings - Fork 1
/
TuioToWinTab.cpp
1949 lines (1725 loc) · 58.1 KB
/
TuioToWinTab.cpp
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
TUIO C++ Example - part of the reacTIVision project
http://reactivision.sourceforge.net/
Copyright (c) 2005-2017 Martin Kaltenbrunner <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
Any person wishing to distribute modifications to the Software is
requested to send the modifications to the original developer so that
they can be incorporated into the canonical version.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*------------------------------------------------------------------------------
WintabEmulator - Emulation.cpp
Copyright (c) 2013 Carl G. Ritson <[email protected]>
This file may be freely used, copied, or distributed without compensation
or licensing restrictions, but is done so without any warranty or implication
of merchantability or fitness for any particular purpose.
------------------------------------------------------------------------------*/
#include "stdafx.h"
#include "TuioToWinTab.h"
//
#include <tlhelp32.h>
#include <assert.h>
#include <windows.h>
#include "wintab.h"
#include "pktdef.h"
#include "logging.h"
#include <wchar.h>
//this is a touch input signature
#define MI_WP_SIGNATURE 0xFF515700
#define MI_SIGNATURE_MASK 0xFFFFFF00
#define IsPenEvent(dw) (((dw) & MI_SIGNATURE_MASK) == MI_WP_SIGNATURE)
#define MAX_STRING_BYTES LC_NAMELEN
#define MAX_HOOKS 16
#define MAX_POINTERS 4
#define TIME_CLOSE_MS 2
#define MOUSE_POINTER_ID 1
BOOL LDOWN = FALSE;
BOOL RDOWN = FALSE;
BOOL MDOWN = FALSE;
BOOL TUIOCURSOR = FALSE;
static BOOL logging = FALSE;
static BOOL debug = FALSE;
typedef struct _packet_data_t {
UINT serial;
UINT contact;
LONG time;
DWORD x, y;
DWORD buttons;
UINT pressure;
UINT pad[9];
} packet_data_t;
typedef struct _hook_t {
HHOOK handle;
DWORD thread;
} hook_t;
static BOOL enabled = FALSE;
static BOOL processing = FALSE;
static emu_settings_t config; // everything we actually need is stored here
static HMODULE module = NULL;
static HWND window = NULL;
static HWND real_window = NULL;
static hook_t hooks[MAX_HOOKS];
static UINT32 pointers[MAX_POINTERS];
static UINT n_pointers = 0;
static packet_data_t *queue = NULL;
static UINT next_serial = 1;
static UINT q_start, q_end, q_length;
static CRITICAL_SECTION q_lock;
static LOGCONTEXTA default_context;
static LPLOGCONTEXTA context = NULL;
std::vector<LPLOGCONTEXTA> ctx;
// todo: read these from ini file
// We already have a function to read/write .ini file in wintab32.cpp, but how do we use it in this case?
// my mistake is that I treat a DLL as a program running in memory, while DLL is just a library of functions\
// that means there is no starting point here, external program wants some function -> gets it from this library -> it runs and returns results.
// well, kind of, it must be more complicated than that
static std::string _address("localhost"); //settings->tuio_udp_address
static UINT tuio_udp = 1; //settings->tuio_udp
static UINT tuio_udp_port = 3333; //settings->tuio_udp_port
static BOOL listening = FALSE;
static UINT32 pressure_max = 1023; //settings->pressure_max
static UINT32 pressure_min = 0; //settings->pressure_min
//static LPLOGCONTEXTA contexts[MAX_CONTEXTS];
//these are not currently used and only needed for imprecise pixel based mouse movements (commented out)
//int screen_width = GetSystemMetrics(SM_CXSCREEN);
//int screen_height = GetSystemMetrics(SM_CYSCREEN);
// check if our vector has the context we need
BOOL ctxHas(HCTX hCtx){
context = (LPLOGCONTEXTA)hCtx;
return hCtx && std::find(ctx.begin(), ctx.end(), context) != ctx.end();
}
// EX emulation.cpp
// initialises wintab Context (for our emulation purposes?)
static void init_context(LOGCONTEXTA *ctx)
{
strncpy_s(ctx->lcName, "Windows", LC_NAMELEN);
ctx->lcOptions = CXO_SYSTEM | CXO_PEN;
ctx->lcStatus = 0;
ctx->lcLocks = 0;
ctx->lcMsgBase = 0x7ff0;
ctx->lcDevice = 0;
ctx->lcPktRate = 64;
ctx->lcPktData = PK_CURSOR | PK_X | PK_Y | PK_BUTTONS; // | PK_NORMAL_PRESSURE
ctx->lcPktMode = 0;
ctx->lcMoveMask = ctx->lcPktData;
ctx->lcBtnDnMask = 0xF8; // 11111000 5 btns for 5 btn pucks
ctx->lcBtnUpMask = 0xF8; // 11111000 5 btns
ctx->lcInOrgX = 0;
ctx->lcInOrgY = 0;
ctx->lcInOrgZ = 0;
ctx->lcInExtX = config.tablet_width;
ctx->lcInExtY = config.tablet_height;
ctx->lcInExtZ = 0;
ctx->lcOutOrgX = ctx->lcInOrgX;
ctx->lcOutOrgY = ctx->lcInOrgY;
ctx->lcOutOrgZ = ctx->lcInOrgZ;
ctx->lcOutExtX = ctx->lcInExtX;
ctx->lcOutExtY = ctx->lcInExtY;
ctx->lcOutExtZ = ctx->lcInExtZ;
ctx->lcSensX = 0; // FIXME
ctx->lcSensY = 0; // FIXME
ctx->lcSensZ = 0; // FIXME
ctx->lcSysMode = 0; // FIXME
ctx->lcSysOrgX = 0; // FIXME
ctx->lcSysOrgY = 0; // FIXME
ctx->lcSysExtX = 0; // FIXME
ctx->lcSysExtY = 0; // FIXME
ctx->lcSysSensX = 0; // FIXME
ctx->lcSysSensY = 0; // FIXME
}
static BOOL update_screen_metrics(LOGCONTEXTA *ctx)
{
// FIXME: need to think about how to handle multiple displays...
BOOL changed = FALSE;
// he gets width and height of a default(?) screen
// I kinda get it, he assumes, that win8 touch events are pixel based, so he sets Context size to the same values, but I can choose any value I want. right?
// int width = config.tablet_width = GetSystemMetrics(SM_CXSCREEN);
// int height = config.tablet_height = GetSystemMetrics(SM_CYSCREEN);
// some useless log file line
//LogEntry("screen metrics, width: %d, height: %d\n", area_width, area_height);
// // sets those values we initialised at the start if resolution have changed
// if (ctx->lcInExtX != width) {
// ctx->lcInExtX = width;
// changed = TRUE;
// }
// if (ctx->lcInExtY != height) {
// ctx->lcInExtY = height;
// changed = TRUE;
// }
// if (changed) {
// ctx->lcOutOrgX = ctx->lcInOrgX;
// ctx->lcOutOrgY = ctx->lcInOrgY;
// ctx->lcOutOrgZ = ctx->lcInOrgZ;
// ctx->lcOutExtX = ctx->lcInExtX;
// ctx->lcOutExtY = ctx->lcInExtY;
// ctx->lcOutExtZ = ctx->lcInExtZ;
//
//ctx->lcSysOrgX = ctx->lcInOrgX;
//ctx->lcSysOrgY = ctx->lcInOrgY;
//ctx->lcSysExtX = ctx->lcInExtX;
//ctx->lcSysExtY = ctx->lcInExtY;
// }
// yep and tells back if changed (maybe he uses this result at some point? idk)
return changed;
}
static UINT convert_contextw(LPLOGCONTEXTW dst, LPLOGCONTEXTA src)
{
if (dst) {
_snwprintf_s(dst->lcName, LC_NAMELEN - 1, L"%s", src->lcName);
dst->lcName[LC_NAMELEN - 1] = L'\0';
dst->lcOptions = src->lcOptions;
dst->lcStatus = src->lcStatus;
dst->lcLocks = src->lcLocks;
dst->lcMsgBase = src->lcMsgBase;
dst->lcDevice = src->lcDevice;
dst->lcPktRate = src->lcPktRate;
dst->lcPktData = src->lcPktData;
dst->lcPktMode = src->lcPktMode;
dst->lcMoveMask = src->lcMoveMask;
dst->lcBtnDnMask = src->lcBtnDnMask;
dst->lcBtnUpMask = src->lcBtnUpMask;
dst->lcInOrgX = src->lcInOrgX;
dst->lcInOrgY = src->lcInOrgY;
dst->lcInOrgZ = src->lcInOrgZ;
dst->lcInExtX = src->lcInExtX;
dst->lcInExtY = src->lcInExtY;
dst->lcInExtZ = src->lcInExtZ;
dst->lcOutOrgX = src->lcOutOrgX;
dst->lcOutOrgY = src->lcOutOrgY;
dst->lcOutOrgZ = src->lcOutOrgZ;
dst->lcOutExtX = src->lcOutExtX;
dst->lcOutExtY = src->lcOutExtY;
dst->lcOutExtZ = src->lcOutExtZ;
dst->lcSensX = src->lcSensX;
dst->lcSensY = src->lcSensY;
dst->lcSensZ = src->lcSensZ;
dst->lcSysMode = src->lcSysMode;
dst->lcSysOrgX = src->lcSysOrgX;
dst->lcSysOrgY = src->lcSysOrgY;
dst->lcSysExtX = src->lcSysExtX;
dst->lcSysExtY = src->lcSysExtY;
dst->lcSysSensX = src->lcSysSensX;
dst->lcSysSensY = src->lcSysSensY;
}
return sizeof(LOGCONTEXTW);
}
static UINT convert_contexta(LPLOGCONTEXTA dst, LPLOGCONTEXTW src)
{
if (dst) {
_snprintf_s(dst->lcName, LC_NAMELEN - 1, "%S", src->lcName);
dst->lcName[LC_NAMELEN - 1] = '\0';
dst->lcOptions = src->lcOptions;
dst->lcStatus = src->lcStatus;
dst->lcLocks = src->lcLocks;
dst->lcMsgBase = src->lcMsgBase;
dst->lcDevice = src->lcDevice;
dst->lcPktRate = src->lcPktRate;
dst->lcPktData = src->lcPktData;
dst->lcPktMode = src->lcPktMode;
dst->lcMoveMask = src->lcMoveMask;
dst->lcBtnDnMask = src->lcBtnDnMask;
dst->lcBtnUpMask = src->lcBtnUpMask;
dst->lcInOrgX = src->lcInOrgX;
dst->lcInOrgY = src->lcInOrgY;
dst->lcInOrgZ = src->lcInOrgZ;
dst->lcInExtX = src->lcInExtX;
dst->lcInExtY = src->lcInExtY;
dst->lcInExtZ = src->lcInExtZ;
dst->lcOutOrgX = src->lcOutOrgX;
dst->lcOutOrgY = src->lcOutOrgY;
dst->lcOutOrgZ = src->lcOutOrgZ;
dst->lcOutExtX = src->lcOutExtX;
dst->lcOutExtY = src->lcOutExtY;
dst->lcOutExtZ = src->lcOutExtZ;
dst->lcSensX = src->lcSensX;
dst->lcSensY = src->lcSensY;
dst->lcSensZ = src->lcSensZ;
dst->lcSysMode = src->lcSysMode;
dst->lcSysOrgX = src->lcSysOrgX;
dst->lcSysOrgY = src->lcSysOrgY;
dst->lcSysExtX = src->lcSysExtX;
dst->lcSysExtY = src->lcSysExtY;
dst->lcSysSensX = src->lcSysSensX;
dst->lcSysSensY = src->lcSysSensY;
}
return sizeof(LOGCONTEXTA);
}
static UINT copy_contexta(LOGCONTEXTA *dst, LOGCONTEXTA *src)
{
if (dst) {
memcpy(dst, src, sizeof(LOGCONTEXTA));
}
return sizeof(LOGCONTEXTA);
}
static void _allocate_queue(void)
{
UINT queue_bytes = sizeof(packet_data_t) * q_length;
if (queue) free(queue);
queue = (packet_data_t *)malloc(queue_bytes);
memset(queue, 0, queue_bytes);
q_start = 0;
q_end = 0;
}
static void allocate_queue(void)
{
EnterCriticalSection(&q_lock);
_allocate_queue();
LeaveCriticalSection(&q_lock);
}
static void release_queue(void)
{
EnterCriticalSection(&q_lock);
free(queue);
q_length = 0;
q_start = 0;
q_end = 0;
LeaveCriticalSection(&q_lock);
}
static void set_queue_length(int length)
{
EnterCriticalSection(&q_lock);
q_length = length + 1;
_allocate_queue();
LeaveCriticalSection(&q_lock);
}
// XXX: only call this when holding the queue lock
static UINT queue_size(void)
{
if (q_start <= q_end) {
return (q_end - q_start);
}
else {
return (q_length - q_start) + q_end;
}
}
static inline BOOL time_is_close(LONG a, LONG b)
{
LONG d = (a - b);
d *= d;
return (d <= (TIME_CLOSE_MS * TIME_CLOSE_MS));
}
// XXX: only call this when holding the queue lock
static BOOL duplicate_packet(packet_data_t *pkt)
{
UINT idx = (q_start + (queue_size() - 1)) % q_length;
return (pkt->x == queue[idx].x)
&& (pkt->y == queue[idx].y)
&& time_is_close(pkt->time, queue[idx].time)
&& (pkt->pressure == queue[idx].pressure)
&& (pkt->contact == queue[idx].contact)
&& (pkt->buttons == queue[idx].buttons);
}
static BOOL enqueue_packet(packet_data_t *pkt)
{
UINT size, idx;
BOOL ret = FALSE;
EnterCriticalSection(&q_lock);
if (q_length > 0) {
if (!duplicate_packet(pkt)) {
size = queue_size();
idx = q_end;
q_end = (q_end + 1) % q_length;
if (size >= (q_length - 1))
q_start = (q_start + 1) % q_length;
pkt->serial = next_serial++;
memcpy(&(queue[idx]), pkt, sizeof(packet_data_t));
ret = TRUE;
}
}
LeaveCriticalSection(&q_lock);
return ret;
}
static BOOL dequeue_packet(UINT serial, packet_data_t *pkt)
{
UINT idx;
BOOL ret = FALSE;
EnterCriticalSection(&q_lock);
idx = q_start;
while (idx != q_end) {
if (queue[idx].serial == serial) {
break;
}
else {
idx = (idx + 1) % q_length;
}
}
if (idx != q_end) {
q_start = (idx + 1) % q_length;
memcpy(pkt, &(queue[idx]), sizeof(packet_data_t));
ret = TRUE;
}
LeaveCriticalSection(&q_lock);
return ret;
}
static UINT copy_handle(LPVOID lpOutput, HANDLE hVal)
{
if (lpOutput) {
*((HANDLE *)lpOutput) = hVal;
}
return sizeof(HANDLE);
}
static UINT copy_wtpkt(LPVOID lpOutput, WTPKT wtVal)
{
if (lpOutput) {
*((WTPKT *)lpOutput) = wtVal;
}
return sizeof(WTPKT);
}
static UINT copy_int(LPVOID lpOutput, int nVal)
{
if (lpOutput) {
*((int *)lpOutput) = nVal;
}
return sizeof(int);
}
static UINT copy_uint(LPVOID lpOutput, UINT nVal)
{
if (lpOutput) {
*((UINT *)lpOutput) = nVal;
}
return sizeof(UINT);
}
static UINT copy_long(LPVOID lpOutput, LONG nVal)
{
if (lpOutput) {
*((LONG *)lpOutput) = nVal;
}
return sizeof(LONG);
}
static UINT copy_dword(LPVOID lpOutput, DWORD nVal)
{
if (lpOutput) {
*((DWORD *)lpOutput) = nVal;
}
return sizeof(DWORD);
}
static UINT copy_strw(LPVOID lpOutput, wchar_t *str)
{
int ret = 0;
if (lpOutput) {
wchar_t *out = (wchar_t *)lpOutput;
_snwprintf_s(out, MAX_STRING_BYTES, _TRUNCATE, str);
//out[MAX_STRING_BYTES - 1] = L'\0';
}
return ((wcslen(str)) * sizeof(wchar_t));
}
static UINT copy_stra(LPVOID lpOutput, CHAR *str)
{
int ret = 0;
if (lpOutput) {
char *out = (char *)lpOutput;
_snprintf_s(out, MAX_STRING_BYTES, _TRUNCATE, "%s", str);
out[MAX_STRING_BYTES - 1] = '\0';
}
return ((strlen(str) + 1) * sizeof(char));
}
static UINT copy_axis(LPVOID lpOutput, LONG axMin, LONG axMax, LONG axUnits, FIX32 axResolution)
{
if (lpOutput) {
LPAXIS ax = (LPAXIS)lpOutput;
ax->axMin = axMin;
ax->axMax = axMax;
ax->axUnits = axUnits;
ax->axResolution = axResolution;
}
return sizeof(AXIS);
}
static UINT fill_orientation(LPVOID lpOutput)
{
if (lpOutput) {
LPORIENTATION o = (LPORIENTATION)lpOutput;
memset(o, 0, sizeof(ORIENTATION));
}
return sizeof(ORIENTATION);
}
static UINT fill_rotation(LPVOID lpOutput)
{
if (lpOutput) {
LPROTATION o = (LPROTATION)lpOutput;
memset(o, 0, sizeof(ROTATION));
}
return sizeof(ROTATION);
}
static UINT write_packet(LPVOID lpPtr, packet_data_t *pkt)
{
LPBYTE ptr = (LPBYTE)lpPtr;
UINT data = ctx.back()->lcPktData;
UINT mode = ctx.back()->lcPktMode;
UINT n = 0;
if (data & PK_CONTEXT)
n += copy_handle((LPVOID)(ptr ? ptr + n : NULL), ctx[0]);
if (data & PK_STATUS)
n += copy_uint((LPVOID)(ptr ? ptr + n : NULL), 0);
if (data & PK_TIME)
n += copy_dword((LPVOID)(ptr ? ptr + n : NULL), pkt->time);
if (data & PK_CHANGED)
n += copy_wtpkt((LPVOID)(ptr ? ptr + n : NULL), 0xffff); // FIXME
if (data & PK_SERIAL_NUMBER)
n += copy_uint((LPVOID)(ptr ? ptr + n : NULL), pkt->serial);
if (data & PK_CURSOR)
n += copy_uint((LPVOID)(ptr ? ptr + n : NULL), 0x0); // XXX: check
if (data & PK_BUTTONS)
n += copy_uint((LPVOID)(ptr ? ptr + n : NULL), pkt->buttons);
if (data & PK_X)
n += copy_long((LPVOID)(ptr ? ptr + n : NULL), pkt->x);
if (data & PK_Y)
n += copy_long((LPVOID)(ptr ? ptr + n : NULL), pkt->y);
if (data & PK_Z)
n += copy_long((LPVOID)(ptr ? ptr + n : NULL), 0);
if (data & PK_NORMAL_PRESSURE) {
if (mode & PK_NORMAL_PRESSURE)
n += copy_int((LPVOID)(ptr ? ptr + n : NULL), 0); // FIXME
else
n += copy_uint((LPVOID)(ptr ? ptr + n : NULL), pkt->pressure);
}
if (data & PK_TANGENT_PRESSURE) {
if (mode & PK_TANGENT_PRESSURE)
n += copy_int((LPVOID)(ptr ? ptr + n : NULL), 0); // FIXME
else
n += copy_uint((LPVOID)(ptr ? ptr + n : NULL), pkt->pressure); // FIXME
}
if (data & PK_ORIENTATION)
n += fill_orientation((LPVOID)(ptr ? ptr + n : NULL));
if (data & PK_ROTATION)
n += fill_rotation((LPVOID)(ptr ? ptr + n : NULL));
return n;
}
static void LogPointerInfo(POINTER_INFO *pi)
{
LogEntry(
"pointerType = %x\n"
"pointerId = %x\n"
"frameId = %x\n"
"pointerFlags = %x\n"
"sourceDevice = %x\n"
"hwndTarget = %x\n"
"ptPixelLocation = %d %d\n"
"ptHimetricLocation = %d %d\n"
"ptPixelLocationRaw = %d %d\n"
"ptHimetricLocationRaw = %d %d\n"
"dwTime = %x\n"
"historyCount = %d\n"
"inputData = %x\n"
"dwKeyStates = %x\n"
"ButtonChangeType = %x\n",
pi->pointerType,
pi->pointerId,
pi->frameId,
pi->pointerFlags,
pi->sourceDevice,
pi->hwndTarget,
pi->ptPixelLocation.x, pi->ptPixelLocation.y,
pi->ptHimetricLocation.x, pi->ptHimetricLocation.y,
pi->ptPixelLocationRaw.x, pi->ptPixelLocationRaw.y,
pi->ptHimetricLocationRaw.x, pi->ptHimetricLocationRaw.y,
pi->dwTime,
pi->historyCount,
pi->InputData,
pi->dwKeyStates,
pi->ButtonChangeType);
}
static void LogPacket(packet_data_t *pkt)
{
LogEntry(
"packet:\n"
" serial = %x\n"
" contact = %d\n"
" time = %d\n"
" x = %d\n"
" y = %d\n"
" buttons = %x\n"
" pressure = %d\n",
pkt->serial,
pkt->contact,
pkt->time,
pkt->x, pkt->y,
pkt->buttons,
pkt->pressure);
}
static void adjustPosition(packet_data_t *pkt)
{
pkt->x = pkt->x + config.shiftX;
pkt->y = pkt->y + config.shiftY;
}
//this one is unused and not needed
static void adjustPressure(packet_data_t *pkt)
{
if (config.pressureCurve) {
double p0, p1, v0, v1;
double v = (double)pkt->pressure;
int i0, i1;
for (i1 = 1; i1 < 4; ++i1) {
if (config.pressurePoint[i1] > pkt->pressure)
break;
}
i0 = i1 - 1;
p0 = (double)config.pressurePoint[i0];
p1 = (double)config.pressurePoint[i1];
v0 = (double)config.pressureValue[i0];
v1 = (double)config.pressureValue[i1];
v0 = v0 * ((p1 - v) / (p1 - p0));
v1 = v1 * ((v - p0) / (p1 - p0));
pkt->pressure = (UINT)(v0 + v1);
}
if (config.pressureExpand) {
double midPoint = (double)(config.pressureMin + ((config.pressureMax - config.pressureMin) / 2));
double expand = ((double)(config.pressureMax - config.pressureMin)) / 1024.0;
double value = (double)pkt->pressure;
value -= midPoint;
value /= expand;
value += 512.0;
if (value < 0.0)
value = 0.0;
else if (value > 1023.0)
value = 1023.0;
pkt->pressure = (UINT)value;
}
else {
if (pkt->pressure < config.pressureMin)
pkt->pressure = config.pressureMin;
else if (pkt->pressure > config.pressureMax)
pkt->pressure = config.pressureMax;
}
}
static void eraseMessage(LPMSG msg)
{
// we can't actually delete messages, so change its type
//if (logging && debug)
// LogEntry("erase %04x\n", msg->message);
msg->message = 0x0;
}
static void setWindowFeedback(HWND hWnd)
{
FEEDBACK_TYPE settings[] = {
FEEDBACK_PEN_BARRELVISUALIZATION,
FEEDBACK_PEN_TAP,
FEEDBACK_PEN_DOUBLETAP,
FEEDBACK_PEN_PRESSANDHOLD,
FEEDBACK_PEN_RIGHTTAP
};
BOOL setting;
BOOL ret;
int i;
//LogEntry("configuring feedback for window: %p\n", hWnd);
for (i = 0; i < (sizeof(settings) / sizeof(FEEDBACK_TYPE)); ++i) {
setting = FALSE;
//ret = SetWindowFeedbackSetting(hWnd,
// settings[i],
// 0,
// sizeof(BOOL),
// &setting
//);
//LogEntry(" setting: %d, ret: %d\n", settings[i], ret);
}
}
static BOOL CALLBACK setFeedbackForThreadWindow(HWND hWnd, LPARAM lParam)
{
setWindowFeedback(hWnd);
return TRUE;
}
// visual feedback for windows we've got messages from (like tap animation etc)
static void setFeedbackForWindows(void)
{
if (config.disableFeedback) {
// enumerate windows of each thread (previouly detected)
int i;
for (i = 0; i < MAX_HOOKS; ++i) {
if (hooks[i].thread)
EnumThreadWindows(hooks[i].thread, setFeedbackForThreadWindow, NULL);
}
}
}
// this thing processes hooked events
// my problem is that he uses hooks to receive messages, but TUIO mechanism is http/udp based and has nothing to do with these events
// I'm going to use mouse presses to translate them to wintab for now
LRESULT CALLBACK emuHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
//LPCWPSTRUCT msg = (LPCWPSTRUCT)lParam;
LPMSG msg = (LPMSG)lParam;
DWORD thread = GetCurrentThreadId();
HHOOK hook = NULL;
UINT i;
if (nCode < 0)
goto end;
for (i = 0; (i < MAX_HOOKS) && !hook; ++i) {
if (hooks[i].thread == thread)
hook = hooks[i].handle;
}
if (enabled && processing && queue) {
POINTER_INPUT_TYPE pointerType = PT_POINTER;
UINT32 pointerId = MOUSE_POINTER_ID;
BOOL leavingWindow = FALSE;
BOOL ignore = FALSE;
LPARAM ext;
switch (msg->message) {
//this handles MOUSE buttons, for now it's the default behaviour, pointer from TUIO, buttons from mouse.
//in future versions this should become an option loaded from the wintab32.ini file
//if (pointerType == PT_PEN) {
// // win8 only
// ret = GetPointerPenInfo(pointerId, &info);
// if (!leavingWindow) {
// buttons[0] = IS_POINTER_FIRSTBUTTON_WPARAM(msg->wParam);
// buttons[1] = IS_POINTER_SECONDBUTTON_WPARAM(msg->wParam);
// buttons[2] = IS_POINTER_THIRDBUTTON_WPARAM(msg->wParam);
// buttons[3] = IS_POINTER_FOURTHBUTTON_WPARAM(msg->wParam);
// buttons[4] = IS_POINTER_FIFTHBUTTON_WPARAM(msg->wParam);
// contact = IS_POINTER_INCONTACT_WPARAM(msg->wParam);
// }
//} else
/// do we need to do the following?
/// SkipPointerFrameMessages(info.pointerInfo.frameId);
case WM_LBUTTONDBLCLK:
case WM_RBUTTONDBLCLK:
case WM_NCLBUTTONDBLCLK:
case WM_NCRBUTTONDBLCLK:
case WM_LBUTTONDOWN:
case WM_NCLBUTTONDOWN:
case WM_LBUTTONUP:
case WM_NCLBUTTONUP:
case WM_RBUTTONDOWN:
case WM_NCRBUTTONDOWN:
case WM_RBUTTONUP:
case WM_NCRBUTTONUP:
case WM_MBUTTONDOWN:
case WM_MBUTTONUP:
case WM_MBUTTONDBLCLK:
//if (!leavingWindow) {
LDOWN = msg->wParam == 0x0001;
RDOWN = msg->wParam == 0x0002;
MDOWN = msg->wParam == 0x0010;
//}
//we only need to delete mouse button events if there is a tuio cursor at the tablet surface currently
//if it's not on a table then we're going to grab some mouse to manipulate some things with it
//we leave it be if we emulate mouse
//not sure why, but eraseMessage doesn't prevent mouse from moving or clicking
if ((config.tuio_mouse==0) && TUIOCURSOR) {
eraseMessage(msg);
}
break;
case WM_MOUSEMOVE:
case WM_NCMOUSEMOVE:
//we need mouse movement events while drawing only if we emulate mouse
//we leave it be if we emulate mouse
if ((config.tuio_mouse==0) && LDOWN && TUIOCURSOR){
eraseMessage(msg);
}
//case WM_NCMOUSELEAVE:
//ext = GetMessageExtraInfo();
//leavingWindow = (msg->message == WM_NCMOUSELEAVE);
//LogEntry("%p %p %04x wParam:%x lParam:%x ext:%x, ignore: %d\n", hook, msg->hwnd, msg->message, msg->wParam, msg->lParam, ext, ignore);
//LogEntry(" x:%d y:%d\n", GET_X_LPARAM(msg->lParam), GET_Y_LPARAM(msg->lParam));
default:
break;
}
}
end:
//And THE next hook, yaaay
return CallNextHookEx(hook, nCode, wParam, lParam);
}
// this thing counts threads and returns the number
static int findThreadsForHooking(void)
{
THREADENTRY32 te32;
HANDLE hThreadSnap;
// this is winapi function that return process id
DWORD pid = GetCurrentProcessId();
int n = 0;
// gets thread snapshot
hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hThreadSnap == INVALID_HANDLE_VALUE)
return 0;
te32.dwSize = sizeof(THREADENTRY32);
if (!Thread32First(hThreadSnap, &te32)) {
CloseHandle(hThreadSnap);
return 0;
}
do {
if (te32.th32OwnerProcessID == pid) {
hooks[n].thread = te32.th32ThreadID;
n++;
}
} while (Thread32Next(hThreadSnap, &te32) && (n < MAX_HOOKS));
CloseHandle(hThreadSnap);
// counts threads in current process
return n;
}
// I've heard that hooks are slow, but at this pont I just need a working driver, so we'll stick to this
// hmmmm, we actually can duplicate wintab messages with mouse events when any button isn't pressed so that way cursor will be visible and at the same time will not interfere with precise drawing process
static void installHook(int id, hook_t *hook)
{
hook->handle = SetWindowsHookEx(
WH_GETMESSAGE, //WH_CALLWNDPROCRET,
emuHookProc,
NULL,
hook->thread
);
// LogEntry("hook %d, thread = %08x, handle = %p\n", id, hook->thread, hook->handle);
}
// hmmm, it hooks something
static void installHooks(void)
{
int n_hooks;
int i;
// clear array
for (i = 0; i < MAX_HOOKS; ++i) {
hooks[i].handle = NULL;
hooks[i].thread = 0;
}
// map threads
// he gets number of hooks needed by counting threads of current process
n_hooks = findThreadsForHooking();
// install hooks per thread
for (i = 0; i < n_hooks; ++i) {
installHook(i, &(hooks[i]));
}
// setup feedback overrides
// whatever that means
setFeedbackForWindows();
}
static void uninstallHooks(void)
{
int i = 0;
while ((hooks[i].thread != 0) && (i < MAX_HOOKS)) {
if (hooks[i].handle)
UnhookWindowsHookEx(hooks[i].handle);
hooks[i].handle = NULL;
hooks[i].thread = 0;
i++;
}
}
void emuEnableThread(DWORD thread)
{
int i;
// don't hook if we are not enabled
if (!enabled)
return;
//LogEntry("emuEnableThread(%08x)\n", thread);
// find a free hook structure
for (i = 0; i < MAX_HOOKS; ++i) {
if (hooks[i].handle && hooks[i].thread == thread) {
// thread is already hooked
return;
}
else if (hooks[i].thread == 0) {
hooks[i].thread = thread;
installHook(i, &(hooks[i]));
// FIXME: override windows feedback?
return;
}
}
// all hook structures are in use
}
void emuDisableThread(DWORD thread)
{
int i;
// no need to do anything if we are not enabled
if (!enabled)
return;
//LogEntry("emuDisableThread(%08x)\n", thread);
// find hook and remove it
for (i = 0; i < MAX_HOOKS; ++i) {
if (hooks[i].thread == thread) {
if (hooks[i].handle)
UnhookWindowsHookEx(hooks[i].handle);
hooks[i].handle = NULL;
hooks[i].thread = 0;
return;
}
}
}
//Start listening to TUIO messages
static void enableProcessing(void)
{
if (!enabled) {
installHooks();
setupReceiver();
enabled = TRUE;
}
processing = TRUE;
}
static void disableProcessing(BOOL hard)
{
processing = FALSE;
if (hard) {
uninstallHooks();
enabled = FALSE;
}
}
void emuSetModule(HMODULE hModule)
{
module = hModule;
}
#if 0
static void findPointers(void)
{
POINTER_DEVICE_INFO deviceData[MAX_POINTERS];
UINT32 deviceCount = 0;
BOOL ret = FALSE;