Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GHOST_TabletManagerWin32.cpp « intern « ghost « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0b69dcb300de22597240948dab94cba0b036edda (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
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
// safe & friendly WinTab wrapper
// by Mike Erwin, July 2010

#include "GHOST_TabletManagerWin32.h"
#include "GHOST_WindowWin32.h"
#include "GHOST_System.h"
#include "GHOST_EventCursor.h"
#include "GHOST_EventButton.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define PACKETDATA PK_CURSOR | PK_X | PK_Y | PK_BUTTONS | PK_NORMAL_PRESSURE | PK_ORIENTATION
#define PACKETMODE PK_BUTTONS
// #define PACKETTILT PKEXT_ABSOLUTE

#include "pktdef.h"

#define MAX_QUEUE_SIZE 100

GHOST_TabletManagerWin32::GHOST_TabletManagerWin32()
	{
	dropTool();

	// open WinTab
	lib_Wintab = LoadLibrary("wintab32.dll");

	if (lib_Wintab)
		{
		// connect function pointers
		func_Open = (WTOPENA) GetProcAddress(lib_Wintab,"WTOpenA");
		func_Close = (WTCLOSE) GetProcAddress(lib_Wintab,"WTClose");
		func_Info = (WTINFOA) GetProcAddress(lib_Wintab,"WTInfoA");
		func_QueueSizeSet = (WTQUEUESIZESET) GetProcAddress(lib_Wintab,"WTQueueSizeSet");
		func_PacketsGet = (WTPACKETSGET) GetProcAddress(lib_Wintab,"WTPacketsGet");
		func_Packet = (WTPACKET) GetProcAddress(lib_Wintab,"WTPacket");

		WORD specV, implV;
		func_Info(WTI_INTERFACE, IFC_SPECVERSION, &specV);
		func_Info(WTI_INTERFACE, IFC_IMPLVERSION, &implV);
		printf("WinTab version %d.%d (%d.%d)\n",
			HIBYTE(specV), LOBYTE(specV), HIBYTE(implV), LOBYTE(implV));

		// query for overall capabilities and ranges
		char tabletName[LC_NAMELEN];
		if (func_Info(WTI_DEVICES, DVC_NAME, tabletName))
			puts(tabletName);

		AXIS xRange, yRange;
		func_Info(WTI_DEVICES, DVC_X, &xRange);
		func_Info(WTI_DEVICES, DVC_Y, &yRange);

		printf("active area: %dx%d\n", xRange.axMax, yRange.axMax);

		func_Info(WTI_DEVICES, DVC_NCSRTYPES, &cursorCount);
		func_Info(WTI_DEVICES, DVC_FIRSTCSR, &cursorBase);

		AXIS pressureRange;
		hasPressure = func_Info(WTI_DEVICES, DVC_NPRESSURE, &pressureRange);// && pressureRange.axMax != 0;

		printf("pressure sensitivity: ");
		if (hasPressure)
			{
			printf("%d to %d\n", pressureRange.axMin, pressureRange.axMax);
			pressureScale = 1.f / pressureRange.axMax;
			}
		else
			{
			printf("none\n");
			pressureScale = 0.f;
			}

		printf("tilt sensitivity:\n");
		AXIS tiltRange[3];
		hasTilt = func_Info(WTI_DEVICES, DVC_ORIENTATION, tiltRange);

		if (hasTilt)
			{
			// cheat by using available data from Intuos4. test on other tablets!!!
			azimuthScale = 1.f / HIWORD(tiltRange[1].axResolution);
			altitudeScale = 1.f / tiltRange[1].axMax;

			// leave this code in place to help support tablets I haven't tested
			const char* axisName[] = {"azimuth","altitude","twist"};
			const char* unitName[] = {NULL,"inch","cm","circle"};
			for (int i = 0; i < 3; ++i)
				{
				AXIS const& t = tiltRange[i];
				if (t.axResolution)
					printf("%s: %d to %d values per %d.%d %s\n",
						axisName[i], t.axMin, t.axMax,
						HIWORD(t.axResolution), LOWORD(t.axResolution),
						unitName[t.axUnits]);
				}
			}
		else
			{
			printf("none\n");
			}

#if 0 // WTX_TILT -- cartesian tilt extension, no conversion needed
		// this isn't working for [mce], so let it rest for now
		printf("raw tilt sensitivity:\n");
		hasTilt = false;
		UINT tag = 0;
		UINT extensionCount;
		func_Info(WTI_INTERFACE, IFC_NEXTENSIONS, &extensionCount);
		for (UINT i = 0; i < extensionCount; ++i)
//		for (UINT i = 0; func_Info(WTI_EXTENSIONS + i, EXT_TAG, &tag); ++i)
			{
			printf("trying extension %d\n", i);
			func_Info(WTI_EXTENSIONS + i, EXT_TAG, &tag);
			if (tag == WTX_TILT)
				{
				hasTilt = true;
				break;
				}
			}

		if (hasTilt)
			{
			func_Info(WTI_EXTENSIONS + tag, EXT_MASK, &tiltMask);
			AXIS tiltRange[2];
			func_Info(WTI_EXTENSIONS + tag, EXT_AXES, tiltRange);
			printf("%d to %d along x\n", tiltRange[0].axMin, tiltRange[0].axMax);
			printf("%d to %d along y\n", tiltRange[1].axMin, tiltRange[1].axMax);
			tiltScaleX = 1.f / tiltRange[0].axMax;
			tiltScaleY = 1.f / tiltRange[1].axMax;
			}
		else
			{
			printf("none\n");
			tiltScaleX = tiltScaleY = 0.f;
			}
#endif // WTX_TILT
		}
	}

GHOST_TabletManagerWin32::~GHOST_TabletManagerWin32()
	{
	// close WinTab
	FreeLibrary(lib_Wintab);
	}

bool GHOST_TabletManagerWin32::available()
	{
	return lib_Wintab // driver installed
		&& func_Info(0,0,NULL); // tablet plugged in
	}

HCTX GHOST_TabletManagerWin32::contextForWindow(GHOST_WindowWin32* window)
	{
	std::map<GHOST_WindowWin32*,HCTX>::iterator i = contexts.find(window);
	if (i == contexts.end())
		return 0; // not found
	else
		return i->second;
	}

void GHOST_TabletManagerWin32::openForWindow(GHOST_WindowWin32* window)
	{
	if (contextForWindow(window) != 0)
		// this window already has a tablet context
		return;

	// set up context
	LOGCONTEXT archetype;
//	func_Info(WTI_DEFSYSCTX, 0, &archetype);
	func_Info(WTI_DEFCONTEXT, 0, &archetype);

	strcpy(archetype.lcName, "blender special");
	archetype.lcPktData = PACKETDATA;
	archetype.lcPktMode = PACKETMODE;
//	archetype.lcOptions |= CXO_MESSAGES | CXO_CSRMESSAGES;
	archetype.lcOptions |= CXO_SYSTEM | CXO_MESSAGES | CXO_CSRMESSAGES;

	// we want first 5 buttons
	archetype.lcBtnDnMask = 0x1f;
	archetype.lcBtnUpMask = 0x1f;

// BEGIN derived from Wacom's TILTTEST.C:
	AXIS TabletX, TabletY;
	func_Info(WTI_DEVICES,DVC_X,&TabletX);
	func_Info(WTI_DEVICES,DVC_Y,&TabletY);
	archetype.lcInOrgX = 0;
	archetype.lcInOrgY = 0;
	archetype.lcInExtX = TabletX.axMax;
	archetype.lcInExtY = TabletY.axMax;
    /* output the data in screen coords */
	archetype.lcOutOrgX = archetype.lcOutOrgY = 0;
	archetype.lcOutExtX = GetSystemMetrics(SM_CXSCREEN);
    /* move origin to upper left */
	archetype.lcOutExtY = -GetSystemMetrics(SM_CYSCREEN);
// END

/*	if (hasTilt)
		{
		archetype.lcPktData |= tiltMask;
		archetype.lcMoveMask |= tiltMask;
		} */

	// open the context
	HCTX context = func_Open(window->getHWND(), &archetype, TRUE);

	// request a deep packet queue
	int tabletQueueSize = MAX_QUEUE_SIZE;
	while (!func_QueueSizeSet(context, tabletQueueSize))
		--tabletQueueSize;
	printf("tablet queue size: %d\n", tabletQueueSize);

	contexts[window] = context;
	}

void GHOST_TabletManagerWin32::closeForWindow(GHOST_WindowWin32* window)
	{
	HCTX context = contextForWindow(window);

	if (context)
		{
		func_Close(context);
		// also remove it from our books:
		contexts.erase(contexts.find(window));
		}
	}

void GHOST_TabletManagerWin32::convertTilt(ORIENTATION const& ort, TabletToolData& data)
	{
	// this code used to live in GHOST_WindowWin32
	// now it lives here

	float vecLen;
	float altRad, azmRad;	/* in radians */

	/*
	from the wintab spec:
	orAzimuth	Specifies the clockwise rotation of the
	cursor about the z axis through a full circular range.

	orAltitude	Specifies the angle with the x-y plane
	through a signed, semicircular range.  Positive values
	specify an angle upward toward the positive z axis;
	negative values specify an angle downward toward the negative z axis.

	wintab.h defines .orAltitude as a UINT but documents .orAltitude
	as positive for upward angles and negative for downward angles.
	WACOM uses negative altitude values to show that the pen is inverted;
	therefore we cast .orAltitude as an (int) and then use the absolute value.
	*/

	/* convert raw fixed point data to radians */
	altRad = fabs(ort.orAltitude) * altitudeScale * M_PI/2.0;
	azmRad = ort.orAzimuth * azimuthScale * M_PI*2.0;

	/* find length of the stylus' projected vector on the XY plane */
	vecLen = cos(altRad);

	/* from there calculate X and Y components based on azimuth */
	data.tilt_x = sin(azmRad) * vecLen;
	data.tilt_y = sin(M_PI/2.0 - azmRad) * vecLen;
	}

void GHOST_TabletManagerWin32::processPackets(GHOST_WindowWin32* window)
	{
	HCTX context = contextForWindow(window);

	if (context)
		{
		PACKET packets[MAX_QUEUE_SIZE];
		int n = func_PacketsGet(context, MAX_QUEUE_SIZE, packets);
	//	printf("processing %d packets\n", n);
	
		for (int i = 0; i < n; ++i)
			{
			PACKET const& packet = packets[i];
			TabletToolData data = {activeTool};
			int x = packet.pkX;
			int y = packet.pkY;
	
			if (activeTool.type == TABLET_MOUSE)
				if (x == prevMouseX && y == prevMouseY)
					// don't send any "mouse hasn't moved" events
					continue;
				else {
					prevMouseX = x;
					prevMouseY = y;
					}
	
			// every packet from a WT_PACKET message comes from the same tool
			switch (activeTool.type)
				{
				case TABLET_MOUSE:
					printf("mouse");
					break;
				case TABLET_PEN:
					printf("pen");
					break;
				case TABLET_ERASER:
					printf("eraser");
					break;
				default:
					printf("???");
				}
	
			printf(" (%d,%d)", x, y);
	
			if (activeTool.hasPressure)
				{
				if (packet.pkNormalPressure)
					{
					data.pressure = pressureScale * packet.pkNormalPressure;
					printf(" %d%%", (int)(100 * data.pressure));
					}
				else
					data.tool.hasPressure = false;
				}
	
			if (activeTool.hasTilt)
				{
				// ORIENTATION const& tilt = packet.pkOrientation;
				// printf(" /%d,%d/", tilt.orAzimuth, tilt.orAltitude);
				convertTilt(packet.pkOrientation, data);
	
				// data.tilt_x = tiltScaleX * packet.pkTilt.tiltX;
				// data.tilt_y = tiltScaleY * packet.pkTilt.tiltY;
				printf(" /%.2f,%.2f/", data.tilt_x, data.tilt_y);
				}
	
			putchar('\n');

			// at this point, construct a GHOST event and push it into the queue!
			// (having trouble with Wacom mouse scaling, so ignore it for now)

//			if (activeTool.type == TABLET_PEN || activeTool.type == TABLET_ERASER)
				{
				GHOST_System* system = (GHOST_System*) GHOST_ISystem::getSystem();

				if (packet.pkButtons)
					{
					// which button?
					GHOST_TButtonMask e_button;
					int buttonNumber = LOWORD(packet.pkButtons);
					e_button = (GHOST_TButtonMask) buttonNumber;

					// pressed or released?
					GHOST_TEventType e_action;
					int buttonAction = HIWORD(packet.pkButtons);
					if (buttonAction == TBN_DOWN)
						e_action = GHOST_kEventButtonDown;
					else
						e_action = GHOST_kEventButtonUp;

					printf("button %d %s\n", buttonNumber, buttonAction == TBN_DOWN ? "down" : "up");

					GHOST_EventButton* e = new GHOST_EventButton(system->getMilliSeconds(), e_action, window, e_button);
					
//					system->pushEvent(e);
					}
				else
					{
					GHOST_EventCursor* e = new GHOST_EventCursor(system->getMilliSeconds(), GHOST_kEventCursorMove, window, x, y);

					// use older TabletData struct for testing until mine is in place
					GHOST_TabletData& e_data = ((GHOST_TEventCursorData*) e->getData())->tablet;
					e_data.Active = (GHOST_TTabletMode) data.tool.type;
					e_data.Pressure = data.pressure;
					e_data.Xtilt = data.tilt_x;
					e_data.Ytilt = data.tilt_y;

//					system->pushEvent(e);
					}
				}
			}
		}
	}

void GHOST_TabletManagerWin32::changeTool(HCTX context, UINT serialNumber)
	{
	puts("-- changing tool --");

	dropTool();

	PACKET packet;
	func_Packet(context, serialNumber, &packet);
	UINT cursor = (packet.pkCursor - cursorBase) % cursorCount;

	// printf("%d mod %d = %d\n", packet.pkCursor - cursorBase, cursorCount, cursor);

	switch (cursor)
		{
		case 0: // older Intuos tablets can track two cursors at once
		case 3: // so we test for both here
			activeTool.type = TABLET_MOUSE;
			break;
		case 1:
		case 4:
			activeTool.type = TABLET_PEN;
			break;
		case 2:
		case 5:
			activeTool.type = TABLET_ERASER;
			break;
		default:
			activeTool.type = TABLET_NONE;
		}

	WTPKT toolData;
	func_Info(WTI_CURSORS + cursor, CSR_PKTDATA, &toolData);
	activeTool.hasPressure = toolData & PK_NORMAL_PRESSURE;
	activeTool.hasTilt = toolData & PK_ORIENTATION;
//	activeTool.hasTilt = toolData & tiltMask;

	if (activeTool.hasPressure)
		puts(" - pressure");

	if (activeTool.hasTilt)
		puts(" - tilt");

	// and just for fun:
	if (toolData & PK_BUTTONS)
		puts(" - buttons");
	}

void GHOST_TabletManagerWin32::dropTool()
	{
	activeTool.type = TABLET_NONE;
	activeTool.hasPressure = false;
	activeTool.hasTilt = false;
	
	prevMouseX = prevMouseY = 0;
	}