-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdevice_info.rb
More file actions
55 lines (48 loc) · 1.95 KB
/
device_info.rb
File metadata and controls
55 lines (48 loc) · 1.95 KB
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
require "ffi"
module HidApi
# Represents the hid_device_info struct returned as part of
# the HidApi::hid_enumerate operation
#
# Because the struct acts as a linked list, every item represents both a
# device in the enumeration AND an Enumerable representing the rest of the
# list.
class DeviceInfo < FFI::Struct
include Enumerable
def self.release(pointer)
HidApi.hid_free_enumeration(pointer) unless pointer.null?
end
# Struct layout from http://www.signal11.us/oss/hidapi/hidapi/doxygen/html/structhid__device__info.html
layout :path, :string, # char * path
:vendor_id, :ushort, # unsigned short vendor_id
:product_id, :ushort, # unsigned short product_id
:serial_number, WideString, # wchar_t * serial_number
:release_number, :ushort, # unsigned short release_number
:manufacturer_string, WideString, # wchar_t * manufacturer_string
:product_string, WideString, # wchar_t * product_string
:usage_page, :ushort, # unsigned short usage_page
:usage, :ushort, # unsigned short usage
:interface_number, :int, # int interface_number
:next, DeviceInfo.auto_ptr # struct hid_device_info * next
# Makes the struct members available as methods
members.each do |f|
define_method(f) do
self[f]
end
end
def inspect
product_string.tap do |s|
s << format(" (%s)", path) unless path.empty?
end
end
# Exposes the linked list structure in an Enumerable-compatible format
def each
return enum_for(:each) unless block_given?
pointer = self
loop do
break if pointer.null?
yield pointer
pointer = pointer.next
end
end
end
end