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
|
/*
* This is an example that demonstrates how to connect to the EyeX Engine and subscribe to the eye position data stream.
*
* Copyright 2013-2014 Tobii Technology AB. All rights reserved.
*/
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
#include <assert.h>
#include "eyex/EyeX.h"
#pragma comment (lib, "Tobii.EyeX.Client.lib")
// ID of the global interactor that provides our data stream; must be unique within the application.
static const TX_STRING InteractorId = "Fluttershy";
// global variables
static TX_HANDLE g_hGlobalInteractorSnapshot = TX_EMPTY_HANDLE;
/*
* Initializes g_hGlobalInteractorSnapshot with an interactor that has the Eye Position behavior.
*/
BOOL InitializeGlobalInteractorSnapshot(TX_CONTEXTHANDLE hContext)
{
TX_HANDLE hInteractor = TX_EMPTY_HANDLE;
TX_HANDLE hBehaviorWithoutParameters = TX_EMPTY_HANDLE;
BOOL success;
success = txCreateGlobalInteractorSnapshot(
hContext,
InteractorId,
&g_hGlobalInteractorSnapshot,
&hInteractor) == TX_RESULT_OK;
success &= txCreateInteractorBehavior(hInteractor, &hBehaviorWithoutParameters, TX_BEHAVIORTYPE_EYEPOSITIONDATA) == TX_RESULT_OK;
txReleaseObject(&hInteractor);
return success;
}
/*
* Callback function invoked when a snapshot has been committed.
*/
void TX_CALLCONVENTION OnSnapshotCommitted(TX_CONSTHANDLE hAsyncData, TX_USERPARAM param)
{
// check the result code using an assertion.
// this will catch validation errors and runtime errors in debug builds. in release builds it won't do anything.
TX_RESULT result = TX_RESULT_UNKNOWN;
txGetAsyncDataResultCode(hAsyncData, &result);
assert(result == TX_RESULT_OK || result == TX_RESULT_CANCELLED);
}
/*
* Callback function invoked when the status of the connection to the EyeX Engine has changed.
*/
void TX_CALLCONVENTION OnEngineConnectionStateChanged(TX_CONNECTIONSTATE connectionState, TX_USERPARAM userParam)
{
switch (connectionState) {
case TX_CONNECTIONSTATE_CONNECTED: {
BOOL success;
printf("The connection state is now CONNECTED (We are connected to the EyeX Engine)\n");
// commit the snapshot with the global interactor as soon as the connection to the engine is established.
// (it cannot be done earlier because committing means "send to the engine".)
success = txCommitSnapshotAsync(g_hGlobalInteractorSnapshot, OnSnapshotCommitted, NULL) == TX_RESULT_OK;
if (!success) {
printf("Failed to initialize the data stream.\n");
}
else {
printf("Waiting for eye position data to start streaming...\n");
}
}
break;
case TX_CONNECTIONSTATE_DISCONNECTED:
printf("The connection state is now DISCONNECTED (We are disconnected from the EyeX Engine)\n");
break;
case TX_CONNECTIONSTATE_TRYINGTOCONNECT:
printf("The connection state is now TRYINGTOCONNECT (We are trying to connect to the EyeX Engine)\n");
break;
case TX_CONNECTIONSTATE_SERVERVERSIONTOOLOW:
printf("The connection state is now SERVER_VERSION_TOO_LOW: this application requires a more recent version of the EyeX Engine to run.\n");
break;
case TX_CONNECTIONSTATE_SERVERVERSIONTOOHIGH:
printf("The connection state is now SERVER_VERSION_TOO_HIGH: this application requires an older version of the EyeX Engine to run.\n");
break;
}
}
/*
* Handles an event from the Eye Position data stream.
*/
void OnEyePositionDataEvent(TX_HANDLE hEyePositionDataBehavior)
{
COORD position = {0,8};
TX_EYEPOSITIONDATAEVENTPARAMS eventParams;
if (txGetEyePositionDataEventParams(hEyePositionDataBehavior, &eventParams) == TX_RESULT_OK) {
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position);
// Output information about the left eye.
printf("LEFT EYE\n");
printf("========\n");
printf("3D position: (%.1f, %.1f, %.1f) \n",
eventParams.LeftEyeX, eventParams.LeftEyeY, eventParams.LeftEyeZ);
printf("Normalized : (%.1f, %.1f, %.1f) \n",
eventParams.LeftEyeXNormalized, eventParams.LeftEyeYNormalized, eventParams.LeftEyeZNormalized);
// Output information about the right eye.
printf("\n");
printf("RIGHT EYE\n");
printf("=========\n");
printf("3D position: (%.1f, %.1f, %.1f) \n",
eventParams.RightEyeX, eventParams.RightEyeY, eventParams.RightEyeZ);
printf("Normalized : (%.1f, %.1f, %.1f) \n",
eventParams.RightEyeXNormalized, eventParams.RightEyeYNormalized, eventParams.RightEyeZNormalized);
printf("\n");
printf("The 3D position consists of X,Y,Z coordinates expressed in millimeters \n");
printf("in relation to the center of the screen where the eye tracker is mounted. \n");
printf("\n");
printf("The normalized coordinates are expressed in relation to the track box, \n");
printf("i.e. the volume in which the eye tracker is theoretically able to track eyes. \n");
printf("- (0,0,0) represents the upper, right corner closest to the eye tracker. \n");
printf("- (1,1,1) represents the lower, left corner furthest away from the eye tracker. \n");
} else {
printf("Failed to interpret eye position data event packet.\n");
}
}
/*
* Callback function invoked when an event has been received from the EyeX Engine.
*/
void TX_CALLCONVENTION HandleEvent(TX_CONSTHANDLE hAsyncData, TX_USERPARAM userParam)
{
TX_HANDLE hEvent = TX_EMPTY_HANDLE;
TX_HANDLE hBehavior = TX_EMPTY_HANDLE;
txGetAsyncDataContent(hAsyncData, &hEvent);
// NOTE. Uncomment the following line of code to view the event object. The same function can be used with any interaction object.
//OutputDebugStringA(txDebugObject(hEvent));
if (txGetEventBehavior(hEvent, &hBehavior, TX_BEHAVIORTYPE_EYEPOSITIONDATA) == TX_RESULT_OK) {
OnEyePositionDataEvent(hBehavior);
txReleaseObject(&hBehavior);
}
// NOTE since this is a very simple application with a single interactor and a single data stream,
// our event handling code can be very simple too. A more complex application would typically have to
// check for multiple behaviors and route events based on interactor IDs.
txReleaseObject(&hEvent);
}
/*
* Application entry point.
*/
int main(int argc, char* argv[])
{
TX_CONTEXTHANDLE hContext = TX_EMPTY_HANDLE;
TX_TICKET hConnectionStateChangedTicket = TX_INVALID_TICKET;
TX_TICKET hEventHandlerTicket = TX_INVALID_TICKET;
BOOL success;
// initialize and enable the context that is our link to the EyeX Engine.
success = txInitializeEyeX(TX_EYEXCOMPONENTOVERRIDEFLAG_NONE, NULL, NULL, NULL, NULL) == TX_RESULT_OK;
success &= txCreateContext(&hContext, TX_FALSE) == TX_RESULT_OK;
success &= InitializeGlobalInteractorSnapshot(hContext);
success &= txRegisterConnectionStateChangedHandler(hContext, &hConnectionStateChangedTicket, OnEngineConnectionStateChanged, NULL) == TX_RESULT_OK;
success &= txRegisterEventHandler(hContext, &hEventHandlerTicket, HandleEvent, NULL) == TX_RESULT_OK;
success &= txEnableConnection(hContext) == TX_RESULT_OK;
// let the events flow until a key is pressed.
if (success) {
printf("Initialization was successful.\n");
} else {
printf("Initialization failed.\n");
}
printf("Press any key to exit...\n");
_getch();
printf("Exiting.\n");
// disable and delete the context.
txDisableConnection(hContext);
txReleaseObject(&g_hGlobalInteractorSnapshot);
success = txShutdownContext(hContext, TX_CLEANUPTIMEOUT_DEFAULT, TX_FALSE) == TX_RESULT_OK;
success &= txReleaseContext(&hContext) == TX_RESULT_OK;
success &= txUninitializeEyeX() == TX_RESULT_OK;
if (!success) {
printf("EyeX could not be shut down cleanly. Did you remember to release all handles?\n");
}
return 0;
}
|