-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmikey.cc
85 lines (67 loc) · 1.98 KB
/
mikey.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
#include <nan.h>
#include <IOKit/hid/IOHIDLib.h>
#include <IOKit/IOKitLib.h>
using namespace v8;
namespace mikey {
// Singleton Manager for Apple MiKey HID
class MikeyManager {
public:
void SendKeyEvent(const char *keyEvent) {
if (hasCallback) {
Handle<Value> argv[] = { NanNew<String>(keyEvent) };
//(new NanCallback(callback))->Call(1, argv);
callback->Call(1, argv);
}
}
void setCallback(NanCallback *cb) {
hasCallback = TRUE;
callback = cb;
}
// Returns singletone instance of the MikeyManager
static MikeyManager* GetInstance() {
if (!instance) {
instance = new MikeyManager;
}
return instance;
}
private:
// Constructor
MikeyManager() {
hasCallback = FALSE;
InitHIDManager();
}
// Destructor
~MikeyManager() {}
// Copy constructor
MikeyManager(MikeyManager const&) {}
// Assignament operator
MikeyManager& operator=(MikeyManager const&) {}
// Singleton reference
static MikeyManager *instance;
bool hasCallback;
void InitHIDManager() { /* @TODO(mrfabbri) */ }
NanCallback *callback;
};
// initializing singleton reference pointer (not the instance)
MikeyManager *MikeyManager::instance = 0;
NAN_METHOD(SetListener) {
NanScope();
Local<Function> callbackHandle = args[0].As<Function>();
NanCallback *callback = new NanCallback(callbackHandle);
MikeyManager::GetInstance()->setCallback(callback);
NanReturnUndefined();
}
// emulate
NAN_METHOD(SendKeyEvent) {
NanScope();
String::Utf8Value keyEvent(args[0]);
MikeyManager::GetInstance()->SendKeyEvent(*keyEvent);
NanReturnUndefined();
}
void Init(Handle<Object> exports) {
NODE_SET_METHOD(exports, "setListener", SetListener);
// exports->Set(NanNew("addListener"), NanNew<FunctionTemplate>(SetListener)->GetFunction());
NODE_SET_METHOD(exports, "sendKeyEvent", SendKeyEvent);
}
NODE_MODULE(mikey, Init)
}