Method: Uhid#read
- Defined in:
- ext/uhid/uhid.c
#read ⇒ Object
Read data from the host.
All returned hashes contain the type key that specifies the type of the received packet. Valid types are: START, STOP, OPEN, CLOSE, OUTPUT. Only OUTPUT Hashes contain a data field that holds the data received by the host.
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 |
# File 'ext/uhid/uhid.c', line 123
static VALUE
cuhid_read_data(VALUE self)
{
ID file_var = rb_intern("@file");
VALUE file = rb_ivar_get(self, file_var);
Check_Type(file, T_FILE);
VALUE file_fileno = rb_funcall(file, rb_intern("fileno"), 0);
int fd = NUM2INT(file_fileno);
struct uhid_event ev;
memset(&ev, 0, sizeof(ev));
ssize_t ret = read(fd, &ev, sizeof(ev));
if (ret == -1) { // EAGAIN
return Qnil;
}
VALUE h = rb_hash_new();
switch (ev.type) {
case UHID_START:
rb_hash_aset(h, rb_str_new_cstr("type"), rb_str_new_cstr("START"));
return h;
case UHID_STOP:
rb_hash_aset(h, rb_str_new_cstr("type"), rb_str_new_cstr("STOP"));
return h;
case UHID_OPEN:
rb_hash_aset(h, rb_str_new_cstr("type"), rb_str_new_cstr("OPEN"));
return h;
case UHID_CLOSE:
rb_hash_aset(h, rb_str_new_cstr("type"), rb_str_new_cstr("CLOSE"));
return h;
case UHID_OUTPUT:
rb_hash_aset(h, rb_str_new_cstr("type"), rb_str_new_cstr("OUTPUT"));
rb_hash_aset(h, rb_str_new_cstr("data"), rb_str_new((const char *) ev.u.output.data, ev.u.output.size));
return h;
default:
return Qnil;
}
}
|