Rise
The Vieneo Province
Loading...
Searching...
No Matches
freetrackclient.c
Go to the documentation of this file.
1/***********************************************************************************
2 * * FTTypes FTTypes contains the specific type definitions for the *
3 * * FreeTrack protocol. *
4 * * It was loosely translated from FTTypes.pas *
5 * * which was created by the FreeTrack-team. *
6 * * *
7 * * Wim Vriend (Developing) *
8 * * Ron Hendriks (Testing and Research) *
9 * * *
10 * * Homepage <http://facetracknoir.sourceforge.net/home/default.htm> *
11 * * *
12 * * This program is distributed in the hope that it will be useful, but *
13 * * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
14 * * or FITNESS FOR A PARTICULAR PURPOSE. *
15 * * *
16 * * *
17 * * The FreeTrackClient sources were translated from the original Delphi sources *
18 * * created by the FreeTrack developers. *
19 */
20
21#ifdef __GNUC__
22#pragma GCC diagnostic ignored "-Wvariadic-macros"
23#pragma GCC diagnostic ignored "-Wunused-parameter"
24#endif
25
26#include <string.h>
27#include <windows.h>
28
29#include "fttypes.h"
30
31#if defined _MSC_VER && !defined _WIN64
32# define FT_EXPORT(t) t __stdcall
33#else
34# define FT_EXPORT(t) __declspec(dllexport) t
35#endif
36
37
38#if 0
39# include <stdio.h>
40static FILE *debug_stream = fopen("c:\\FreeTrackClient.log", "a");
41# define dbg_report(...) if (debug_stream) { fprintf(debug_stream, __VA_ARGS__); fflush(debug_stream); }
42#else
43#define dbg_report(...) ((void)0)
44#endif
45
46static HANDLE hFTMemMap = 0;
47static FTHeap* ipc_heap = 0;
48static HANDLE ipc_mutex = 0;
49static uint32_t last_data_id = 0;
50static const char* dllVersion = "1.0.0.0";
51static const char* dllProvider = "FreeTrack";
52
53static void impl_close_mapping(void)
54{
55 if (ipc_heap != NULL) UnmapViewOfFile((const void*)ipc_heap);
56 if (hFTMemMap != NULL) CloseHandle(hFTMemMap);
57 if (ipc_mutex != NULL) CloseHandle(ipc_mutex);
58 ipc_heap = NULL;
59 hFTMemMap = NULL;
60 ipc_mutex = NULL;
61 last_data_id = 0;
62}
63
65{
66 if (ipc_heap != NULL)
67 return TRUE;
68
69 // The tracker owns this record. Creating an empty record here used to
70 // report a successful zero pose and override Rise's manual camera input.
71 hFTMemMap = OpenFileMappingA(FILE_MAP_READ, FALSE, FREETRACK_HEAP);
72
73 if (hFTMemMap == NULL)
74 return (ipc_heap = NULL), FALSE;
75
76 // Read only the original 92-byte pose; legacy producers need not expose
77 // opentrack's extra game-identification fields.
78 ipc_heap = (FTHeap*) MapViewOfFile(hFTMemMap, FILE_MAP_READ, 0, 0, sizeof(FTData));
79 ipc_mutex = CreateMutexA(NULL, FALSE, FREETRACK_MUTEX);
80 if (ipc_heap == NULL || ipc_mutex == NULL) {
82 return FALSE;
83 }
84
85 return TRUE;
86}
87
88FT_EXPORT(BOOL) FTGetData(FTData* data)
89{
90 FTData snapshot;
91 DWORD wait_result;
92 if (data == NULL || impl_open_mapping() == FALSE)
93 return FALSE;
94
95 // Never stall the frame thread waiting for a tracker. A missed sample is
96 // retried next frame without changing the caller's camera pose.
97 wait_result = WaitForSingleObject(ipc_mutex, 0);
98 if (wait_result == WAIT_ABANDONED) {
99 // A producer may have exited halfway through a write.
101 ReleaseMutex(ipc_mutex);
102 return FALSE;
103 }
104 if (wait_result != WAIT_OBJECT_0)
105 return FALSE;
106 memcpy((void*)&snapshot, (const void*)&ipc_heap->data, sizeof(FTData));
107 ReleaseMutex(ipc_mutex);
108
109 // A centered pose is valid. Freshness comes from the producer's frame
110 // counter, not from nonzero angles or camera dimensions. Equality also
111 // leaves manual input alone when a tracker pauses, exits, or disconnects.
112 if (snapshot.DataID == last_data_id)
113 return FALSE;
114 last_data_id = snapshot.DataID;
115 memcpy((void*)data, (const void*)&snapshot, sizeof(FTData));
116 return TRUE;
117}
118
119BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
120{
121 (void)instance;
122 (void)reserved;
123 if (reason == DLL_PROCESS_DETACH)
125 return TRUE;
126}
127
128/*
129// For some mysterious reason, the previously existing function FTReportID has been changed to FTReportName, but with an integer as argument.
130// 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).
131// Thanks guys!
132*/
133FT_EXPORT(void) FTReportName( int name )
134{
135 (void)name;
136 dbg_report("FTReportName request (ID = %d).\n", name);
137}
138
139FT_EXPORT(void) FTReportID( int name )
140{
141 (void)name;
142 dbg_report("FTReportID request (ID = %d).\n", name);
143}
144
145FT_EXPORT(const char*) FTGetDllVersion(void)
146{
147 dbg_report("FTGetDllVersion request.\n");
148 return dllVersion;
149}
150
151FT_EXPORT(const char*) FTProvider(void)
152{
153 dbg_report("FTProvider request.\n");
154 return dllProvider;
155}
156
157#if defined _MSC_VER && !defined _WIN64
158#pragma comment (linker, "/export:FTReportID=_FTReportID@4")
159#pragma comment (linker, "/export:FTReportName=_FTReportName@4")
160#pragma comment (linker, "/export:FTGetDllVersion=_FTGetDllVersion@0")
161#pragma comment (linker, "/export:FTProvider=_FTProvider@0")
162#pragma comment (linker, "/export:FTGetData=_FTGetData@4")
163#endif
SCRIPT_CONTROL SCRIPT_STATE const void SCRIPT_CONTROL SCRIPT_STATE const int SCRIPT_TABDEF const BYTE SCRIPT_STRING_ANALYSIS BOOL
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
static const char * dllVersion
#define FT_EXPORT(t)
static const char * dllProvider
BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
#define dbg_report(...)
static FTHeap * ipc_heap
static HANDLE ipc_mutex
static void impl_close_mapping(void)
static uint32_t last_data_id
static HANDLE hFTMemMap
static BOOL impl_open_mapping(void)
struct FTHeap__ FTHeap
#define FREETRACK_MUTEX
Definition fttypes.h:28
struct FTData__ FTData
#define FREETRACK_HEAP
Definition fttypes.h:25
typedef void(S_CALLTYPE *SteamAPIWarningMessageHook_t)(int
uint32_t DataID
Definition fttypes.h:33
FTData data
Definition fttypes.h:64