aboutsummaryrefslogtreecommitdiff
path: root/lib/ioctl.cpp
blob: 32af5fad73bcafa8169b95f09b7d61ddb05000b0 (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
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/input.h>
#include "nan.h"

NAN_METHOD(eviocgrab) {
  assert(info.Length() == 2);
  if (!info[0]->IsUint32()) Nan::ThrowTypeError("Argument 0 Must be an Integer");
  if (!info[1]->IsUint32()) Nan::ThrowTypeError("Argument 1 Must be an Integer");
  int fd   = Nan::To<int32_t>(info[0]).ToChecked();
  int grab = Nan::To<int32_t>(info[1]).ToChecked();

  int res = ioctl(fd, EVIOCGRAB, grab);
  if (res < 0) return Nan::ThrowError(
    Nan::ErrnoException(errno, "ioctl", nullptr, nullptr));

  info.GetReturnValue().Set(res);
}

NAN_METHOD(eviocgid) {
  assert(info.Length() == 1);
  if (!info[0]->IsUint32()) Nan::ThrowTypeError("Argument 0 Must be an Integer");
  int fd = Nan::To<int32_t>(info[0]).ToChecked();

  struct input_id id;
  if (ioctl(fd, EVIOCGID, &id) == -1) return Nan::ThrowError(
    Nan::ErrnoException(errno, "ioctl", nullptr, nullptr));

  v8::Local<v8::Object> size = Nan::New<v8::Object>();
  Nan::Set(size, Nan::New("bustype").ToLocalChecked(), Nan::New((double)id.bustype));
  Nan::Set(size, Nan::New("vendor" ).ToLocalChecked(), Nan::New((double)id.vendor ));
  Nan::Set(size, Nan::New("product").ToLocalChecked(), Nan::New((double)id.product));
  Nan::Set(size, Nan::New("version").ToLocalChecked(), Nan::New((double)id.version));
  info.GetReturnValue().Set(size);
}

NAN_MODULE_INIT(InitAll) {
  NAN_EXPORT(target, eviocgrab);
  NAN_EXPORT(target, eviocgid);
}

NODE_MODULE(ioctl, InitAll)