summaryrefslogtreecommitdiffhomepage
path: root/freetrackclient/freetrackclient.c
blob: 2049d91e1f161cd2d6dd0097c6970fac34ef7b3d (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
/***********************************************************************************
 * * FTTypes            FTTypes contains the specific type definitions for the      *
 * *                    FreeTrack protocol.                                         *
 * *                    It was loosely translated from FTTypes.pas                  *
 * *                    which was created by the FreeTrack-team.                    *
 * *                                                                                *
 * * Wim Vriend (Developing)                                                        *
 * * Ron Hendriks (Testing and Research)                                            *
 * *                                                                                *
 * * Homepage               <http://facetracknoir.sourceforge.net/home/default.htm> *
 * *                                                                                *
 * * 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.                                           *
 * *                                                                                *
 * *                                                                                *
 * * The FreeTrackClient sources were translated from the original Delphi sources   *
 * * created by the FreeTrack developers.                                           *
 */

#ifndef _MSC_VER
#   warning "expect misnamed symbols"
#endif

#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wvariadic-macros"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif

#include <string.h>
#include <windows.h>

#include "fttypes.h"

#if !defined _WIN64
#   define FT_EXPORT(t) t __stdcall
#else
#   define FT_EXPORT(t) __declspec(dllexport) t
#endif


#if 0
#   include <stdio.h>
static FILE *debug_stream = fopen("c:\\FreeTrackClient.log", "a");
#   define dbg_report(...) if (debug_stream) { fprintf(debug_stream, __VA_ARGS__); fflush(debug_stream); }
#else
#define dbg_report(...) ((void)0)
#endif

static HANDLE hFTMemMap = 0;
static FTHeap* ipc_heap = 0;
static HANDLE ipc_mutex = 0;
static const char* dllVersion = "1.0.0.0";
static const char* dllProvider = "FreeTrack";

static BOOL impl_create_mapping(void)
{
    if (ipc_heap != NULL)
        return TRUE;

    hFTMemMap = CreateFileMappingA(INVALID_HANDLE_VALUE,
                                   NULL,
                                   PAGE_READWRITE,
                                   0,
                                   sizeof(FTHeap),
                                   (LPCSTR) FREETRACK_HEAP);

    if (hFTMemMap == NULL)
        return (ipc_heap = NULL), FALSE;

    ipc_heap = (FTHeap*) MapViewOfFile(hFTMemMap, FILE_MAP_WRITE, 0, 0, sizeof(FTHeap));
    ipc_mutex = CreateMutexA(NULL, FALSE, FREETRACK_MUTEX);

    return TRUE;
}

FT_EXPORT(BOOL) FTGetData(FTData* data)
{
    if (impl_create_mapping() == FALSE)
        return FALSE;

    if (ipc_mutex && WaitForSingleObject(ipc_mutex, 16) == WAIT_OBJECT_0) {
        memcpy(data, &ipc_heap->data, sizeof(FTData));
        if (ipc_heap->data.DataID > (1 << 29))
            ipc_heap->data.DataID = 0;
        ReleaseMutex(ipc_mutex);
    }
    return TRUE;
}

/*
// 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);
}

FT_EXPORT(void) FTReportID( int name )
{
    dbg_report("FTReportID request (ID = %d).\n", name);
}

FT_EXPORT(const char*) FTGetDllVersion(void)
{
    dbg_report("FTGetDllVersion request.\n");
    return dllVersion;
}

FT_EXPORT(const char*) FTProvider(void)
{
    dbg_report("FTProvider request.\n");
    return dllProvider;
}

#if defined _MSC_VER && !defined _WIN64
#pragma comment (linker, "/export:FTReportID=_FTReportID@4")
#pragma comment (linker, "/export:FTReportName=_FTReportName@4")
#pragma comment (linker, "/export:FTGetDllVersion=_FTGetDllVersion@0")
#pragma comment (linker, "/export:FTProvider=_FTProvider@0")
#pragma comment (linker, "/export:FTGetData=_FTGetData@4")
#endif