-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathbinding.cc
136 lines (117 loc) · 4.55 KB
/
binding.cc
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
#include <node.h>
#include <nan.h>
#include <v8.h>
#include "gamepad/Gamepad.h"
using namespace v8;
Nan::Persistent<Object> persistentHandle;
Nan::AsyncResource asyncResource("gamepad");
NAN_METHOD(nGamepad_init) {
Nan::HandleScope scope;
Gamepad_init();
return;
}
NAN_METHOD(nGamepad_shutdown) {
Nan::HandleScope scope;
Gamepad_shutdown();
return;
}
NAN_METHOD(nGamepad_numDevices) {
Nan::HandleScope scope;
info.GetReturnValue().Set(Nan::New<Number>(Gamepad_numDevices()));
}
Local<Object> nGamepad_toObject(Gamepad_device* device) {
Local<Object> obj = Nan::New<Object>();
Nan::Set(obj, Nan::New("deviceID").ToLocalChecked(), Nan::New<Number>(device->deviceID));
Nan::Set(obj, Nan::New("description").ToLocalChecked(), Nan::New<String>(device->description).ToLocalChecked());
Nan::Set(obj, Nan::New("vendorID").ToLocalChecked(), Nan::New<Number>(device->vendorID));
Nan::Set(obj, Nan::New("productID").ToLocalChecked(), Nan::New<Number>(device->productID));
Local<Array> axes = Nan::New<Array>(device->numAxes);
for (unsigned int i = 0; i < device->numAxes; i++) {
Nan::Set(axes, i, Nan::New<Number>(device->axisStates[i]));
}
Nan::Set(obj, Nan::New("axisStates").ToLocalChecked(), axes);
Local<Array> buttons = Nan::New<Array>(device->numButtons);
for (unsigned int i = 0; i < device->numButtons; i++) {
Nan::Set(buttons, i, Nan::New<Boolean>(device->buttonStates[i]));
}
Nan::Set(obj, Nan::New("buttonStates").ToLocalChecked(), buttons);
return obj;
}
NAN_METHOD(nGamepad_deviceAtIndex) {
Nan::HandleScope scope;
int deviceIndex = info[0].As<Int32>()->Value();
struct Gamepad_device* device = Gamepad_deviceAtIndex(deviceIndex);
if (!device) return;
info.GetReturnValue().Set(nGamepad_toObject(device));
}
NAN_METHOD(nGamepad_detectDevices) {
Nan::HandleScope scope;
Gamepad_detectDevices();
return;
}
NAN_METHOD(nGamepad_processEvents) {
Nan::HandleScope scope;
Gamepad_processEvents();
return;
}
void nGamepad_deviceAttach_cb(struct Gamepad_device* device, void* context) {
Local<Value> info[] = {
Nan::New("attach").ToLocalChecked(),
Nan::New<Number>(device->deviceID),
nGamepad_toObject(device),
};
asyncResource.runInAsyncScope(Nan::New<Object>(persistentHandle), "on", 3, info);
}
void nGamepad_deviceRemove_cb(struct Gamepad_device* device, void* context) {
Local<Value> info[] = {
Nan::New("remove").ToLocalChecked(),
Nan::New<Number>(device->deviceID),
};
asyncResource.runInAsyncScope(Nan::New<Object>(persistentHandle), "on", 2, info);
}
void nGamepad_buttonDown_cb(struct Gamepad_device* device, unsigned int buttonID, double timestamp, void* context) {
Local<Value> info[] = {
Nan::New("down").ToLocalChecked(),
Nan::New<Number>(device->deviceID),
Nan::New<Number>(buttonID),
Nan::New<Number>(timestamp),
};
asyncResource.runInAsyncScope(Nan::New<Object>(persistentHandle), "on", 4, info);
}
void nGamepad_buttonUp_cb(struct Gamepad_device* device, unsigned int buttonID, double timestamp, void* context) {
Local<Value> info[] = {
Nan::New("up").ToLocalChecked(),
Nan::New<Number>(device->deviceID),
Nan::New<Number>(buttonID),
Nan::New<Number>(timestamp),
};
asyncResource.runInAsyncScope(Nan::New<Object>(persistentHandle), "on", 4, info);
}
void nGamepad_axisMove_cb(struct Gamepad_device* device, unsigned int axisID, float value, float lastValue, double timestamp, void * context) {
Local<Value> info[] = {
Nan::New("move").ToLocalChecked(),
Nan::New<Number>(device->deviceID),
Nan::New<Number>(axisID),
Nan::New<Number>(value),
Nan::New<Number>(lastValue),
Nan::New<Number>(timestamp),
};
asyncResource.runInAsyncScope(Nan::New<Object>(persistentHandle), "on", 6, info);
}
void init(Local<Object> target) {
Local<Object> handle = Nan::New<Object>();
persistentHandle.Reset(handle);
Nan::Set(target, Nan::New<String>("context").ToLocalChecked(), handle);
Gamepad_deviceAttachFunc(nGamepad_deviceAttach_cb, NULL);
Gamepad_deviceRemoveFunc(nGamepad_deviceRemove_cb, NULL);
Gamepad_buttonDownFunc(nGamepad_buttonDown_cb, NULL);
Gamepad_buttonUpFunc(nGamepad_buttonUp_cb, NULL);
Gamepad_axisMoveFunc(nGamepad_axisMove_cb, NULL);
Nan::SetMethod(target, "init", nGamepad_init);
Nan::SetMethod(target, "shutdown", nGamepad_shutdown);
Nan::SetMethod(target, "numDevices", nGamepad_numDevices);
Nan::SetMethod(target, "deviceAtIndex", nGamepad_deviceAtIndex);
Nan::SetMethod(target, "detectDevices", nGamepad_detectDevices);
Nan::SetMethod(target, "processEvents", nGamepad_processEvents);
}
NODE_MODULE(gamepad, init)