summaryrefslogtreecommitdiffhomepage
path: root/ftnoir_protocol_ft/FreeTrackClient.c
blob: b1b62123b4f0c39ea524e7fdd23d1acdf31b3319 (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
/********************************************************************************
* FreeTrackClientDll Implements the FreeTrack 2.0 interface for FT-enabled		*
*					games.														*
*					It uses the FreeTrack protocol (memory mapping) to			*
*					receive data from FaceTrackNoIR (or FreeTrack, or ...).		*
*																				*
* Copyright (C) 2013	Wim Vriend (Developing)									*
*						Ron Hendriks (Testing and Research)						*
*																				*
* Homepage				<http://facetracknoir.sourceforge.net/home/default.htm>	*
*																				*
* This program is free software; you can redistribute it and/or modify it		*
* under the terms of the GNU General Public License as published by the			*
* Free Software Foundation; either version 3 of the License, or (at your		*
* option) any later version.													*
*																				*
* This program is distributed in the hope that it will be useful, but			*
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY	*
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for	*
* more details.																	*
*																				*
* You should have received a copy of the GNU General Public License along		*
* with this program; if not, see <http://www.gnu.org/licenses/>.				*
*																				*
********************************************************************************/
/*
	Modifications (last one on top):

	20130208 - WVR: The old DLL from FreeTrack seems to crash ArmA2. And we need 64-bit support.
	
*/

/*
 * COMPILE WITH:
 * % i686-w64-mingw32-gcc -shared -o FreeTrackClient.dll -O2 FreeTrackClient.c
 * DO NOT USE MSVC! IT INTRODUCES FAULTY MANIFEST!!!
 * -- sthalik
 */

#ifdef WIN64
#pragma comment(linker, "/export:FTGetData")
#pragma comment(linker, "/export:FTReportName")
#pragma comment(linker, "/export:FTGetDllVersion")
#pragma comment(linker, "/export:FTProvider")
#endif

#define FT_DECLSPEC __declspec(dllexport)

#define FT_EXPORT(t) FT_DECLSPEC t __stdcall
#define	NP_AXIS_MAX				16383

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <tchar.h>

typedef struct TFreeTrackData {
	int DataID;
	int CamWidth;
    int CamHeight;
    // virtual pose
    float Yaw;   // positive yaw to the left
    float Pitch; // positive pitch up
    float Roll;  // positive roll to the left
    float X;
    float Y;
    float Z;
    // raw pose with no smoothing, sensitivity, response curve etc. 
    float RawYaw;
    float RawPitch;
    float RawRoll;
    float RawX;
    float RawY;
    float RawZ;
    // raw points, sorted by Y, origin top left corner
    float X1;
    float Y1;
    float X2;
    float Y2;
    float X3;
    float Y3;
    float X4;
    float Y4;
} TFreeTrackData;
typedef TFreeTrackData * PFreetrackData;

//
// Functions to create/open the file-mapping
// and to destroy it again.
//
float scale2AnalogLimits( float x, float min_x, float max_x );
float getDegreesFromRads ( float rads );
FT_EXPORT(BOOL) FTCreateMapping(void);

#if 0
static FILE *debug_stream;
#define dbg_report(...) if (debug_stream) { fprintf(debug_stream, __VA_ARGS__); fflush(debug_stream); }
#else
#define dbg_report(...)
#endif

//
// Handles to 'handle' the memory mapping
//
#define FT_MM_DATA "FT_SharedMem"
#define FREETRACK_MUTEX "FT_Mutext"

static HANDLE hFTMemMap = 0;
static TFreeTrackData *pMemData = 0;
static HANDLE hFTMutex = 0;
static const char* dllVersion = "1.0.0.0";
static const char* dllProvider = "FreeTrack";

static unsigned short gameid = 0;

//
// DllMain gets called, when the DLL is (un)loaded or a process attaches.
//
FT_EXPORT(BOOL) WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason) {
		case DLL_PROCESS_ATTACH:
#ifdef WIN64
		    dbg_report("\n= WIN64 =========================================================================================\n");
#else
		    dbg_report("\n= WIN32 =========================================================================================\n");
#endif
			dbg_report("DllMain: (0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
            dbg_report("DllMain: Attach request\n");
//			debug_stream = fopen("c:\\FreeTrackClient.log", "a");
            DisableThreadLibraryCalls(hinstDLL);
            break;

		case DLL_PROCESS_DETACH:
			dbg_report("DllMain: Detach\n");
			dbg_report("DllMain: (0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
		    dbg_report("==========================================================================================\n");
            break;
    }
    return TRUE;
}

/******************************************************************
 *		FTGetData (FreeTrackClient.1)
 */
#pragma comment(linker, "/export:FTGetData@4=FTGetData")
FT_EXPORT(BOOL) FTGetData(TFreeTrackData* data)
{
  static int frame = 0;
  static int prevDataID = 0;
  static int dlyTrackingOff = 0;
  static int tracking = 0;

//  dbg_report("NP_GetData called.");
  if (FTCreateMapping() == FALSE) return FALSE;

  if (hFTMutex && WaitForSingleObject(hFTMutex, 5) == WAIT_OBJECT_0) {
	if (pMemData) {

		//
		// When FaceTrackNoIR does not update frames (any more), don't update the data.
		//
		if (prevDataID != pMemData->DataID) {
			*data = *pMemData;
			dlyTrackingOff = 0;
		}
		else {
			dlyTrackingOff++;
			if (dlyTrackingOff > 20) {
				dlyTrackingOff = 100;
				tracking = FALSE;
			}
		}
		prevDataID = pMemData->DataID;
		
		//
		// Limit the range of DataID
		//
		if (pMemData->DataID > 1000) {
			pMemData->DataID = 0;
		}
		data->DataID = pMemData->DataID;

		//
		// Send the ID to FaceTrackNoIR, so it can display the game-name.
		// This could be a FreeTrack-specific ID
		//
		//sprintf(pMemData->GameID, "%d", gameid );

	}
	ReleaseMutex(hFTMutex);
  }
  return TRUE;
}

/******************************************************************
 *		FTReportName (FreeTrackClient.2)
 */
#pragma comment(linker, "/export:FTReportName@4=FTReportName")
//
// For some mysterious reason, the previously existing function FTReportID has been changed to FTReportName, but with an integer as argument.
// The Delphi-code from the FreeTrack repo suggest a char * as argument, so it cost me an afternoon to figure it out (and keep ArmA2 from crashing).
// Thanks guys!
//
FT_EXPORT(void) FTReportName( int name )
{
	dbg_report("FTReportName request (ID = %d).\n", name);
	gameid = name;												// They might have really passed the name here... but they didn't!
	return;
}

/******************************************************************
 *		FTGetDllVersion (FreeTrackClient.3)
 */
#pragma comment(linker, "/export:FTGetDllVersion@0=FTGetDllVersion")
FT_EXPORT(const char*) FTGetDllVersion(void)
{
    dbg_report("FTGetDllVersion request.\n");

	return dllVersion;
}

/******************************************************************
 *		FTProvider (FreeTrackClient.4)
 */
#pragma comment(linker, "/export:FTProvider@0=FTProvider")
FT_EXPORT(const char*) FTProvider(void)
{
    dbg_report("FTProvider request.\n");

	return dllProvider;
}

//
// Create a memory-mapping to the Freetrack data.
// It contains the tracking data, a handle to the main-window and the program-name of the Game!
//
//
#pragma comment(linker, "/export:FTCreateMapping@0=FTCreateMapping")
FT_EXPORT(BOOL) FTCreateMapping(void)
{
	BOOL bMappingExists = FALSE;
	PDWORD_PTR MsgResult = 0;

	//
	// Memory-mapping already exists!
	//
	if ( pMemData != NULL ) {
		return TRUE;
	}

    dbg_report("FTCreateMapping request (pMemData == NULL).\n");

	//
	// A FileMapping is used to create 'shared memory' between the FTClient and the FTServer.
	//
	// Try to create a FileMapping to the Shared Memory. This is done to check if it's already there (what
	// may mean the face-tracker program is already running).
	//
	// If one already exists: close it and open the file-mapping to the existing one.
	//
	hFTMemMap = CreateFileMappingA( INVALID_HANDLE_VALUE , 00 , PAGE_READWRITE , 0 , 
		                           sizeof( TFreeTrackData ), 
								   (LPCSTR) FT_MM_DATA );

	if ( ( hFTMemMap != 0 ) && ( GetLastError() == ERROR_ALREADY_EXISTS ) ) {
		dbg_report("FTCreateMapping: Mapping already exists.\n");
		bMappingExists = TRUE;				// So the server was (probably) already started!
		CloseHandle( hFTMemMap );
		hFTMemMap = 0;
	}

	//
	// Create a new FileMapping, Read/Write access
	//
	hFTMemMap = OpenFileMappingA( FILE_MAP_ALL_ACCESS , FALSE , (LPCSTR) FT_MM_DATA );
	if ( ( hFTMemMap != 0 ) ) {
		dbg_report("FTCreateMapping: Mapping opened.\n");
		pMemData = (TFreeTrackData *) MapViewOfFile(hFTMemMap, FILE_MAP_ALL_ACCESS, 0, 0, sizeof( TFreeTrackData ) );
	    hFTMutex = CreateMutexA(NULL, FALSE, FREETRACK_MUTEX);
	}
	else {
		return FALSE;
	}
	return TRUE;
}

//
// Destory the FileMapping to the shared memory
//
#pragma comment(linker, "/export:FTDestroyMapping@0=FTDestroyMapping")
FT_EXPORT(void) FTDestroyMapping(void)
{
	if ( pMemData != NULL ) {
		UnmapViewOfFile ( pMemData );
	}
	
	CloseHandle( hFTMutex );
	CloseHandle( hFTMemMap );
	pMemData = 0;
	hFTMemMap = 0;
}

//
// 4 convenience
//
float getDegreesFromRads ( float rads ) { 
	return (rads * 57.295781f); 
}

//
// Scale the measured value to the TIR values
//
float scale2AnalogLimits( float x, float min_x, float max_x ) {
double y;
double local_x;
	
	local_x = x;
	if (local_x > max_x) {
		local_x = max_x;
	}
	if (local_x < min_x) {
		local_x = min_x;
	}
	y = ( NP_AXIS_MAX * local_x ) / max_x;

	return (float) y;
}