ffi-xinput 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/ffi-xinput.rb +114 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4e8cd01dd106167d3bca6c4d997cc4d8524e44016aa9ce8eda321f1273ba8cf6
4
+ data.tar.gz: 20d2f8ce2c5170c85fc88f03f69bb165be21894724404ab887abebcf22277865
5
+ SHA512:
6
+ metadata.gz: 5088629c8d1e91b828fb68a1d3cc59d5df65c12153c563e7fc2308a51253996a19375ddae4b713008825b1ce31623add089be1e5bb206e119ae2e1b9cc97bbae
7
+ data.tar.gz: 5b69b3e5816a15713ba17409d85a2a3051a6e0db2a6691ebca5f0d0646e92ec783a31e95b55d29a1ae921e6e0f3ec0b78490dbe8c1cd7e5ec248e306ac3fd157
data/lib/ffi-xinput.rb ADDED
@@ -0,0 +1,114 @@
1
+ require "ffi"
2
+
3
+ module FFI::XInput
4
+
5
+ extend FFI::Library
6
+
7
+ ffi_lib "xinput1_3"
8
+ ffi_convention :stdcall
9
+
10
+ attach_function :XInputEnable, [:bool], :void
11
+ attach_function :XInputGetState, [:ulong, :pointer], :ulong
12
+ attach_function :XInputSetState, [:ulong, :pointer], :ulong
13
+
14
+ class XINPUT_GAMEPAD < FFI::Struct
15
+
16
+ layout :wButtons, :ushort,
17
+ :bLeftTrigger, :uchar,
18
+ :bRightTrigger, :uchar,
19
+ :sThumbLX, :short,
20
+ :sThumbLY, :short,
21
+ :sThumbRX, :short,
22
+ :sThumbRY, :short
23
+
24
+ end
25
+
26
+ class XINPUT_STATE < FFI::Struct
27
+
28
+ layout :dwPacketNumber, :ulong,
29
+ :Gamepad, XINPUT_GAMEPAD
30
+
31
+ end
32
+
33
+ class XINPUT_VIBRATION < FFI::Struct
34
+
35
+ layout :wLeftMotorSpeed, :ushort,
36
+ :wRightMotorSpeed, :ushort
37
+
38
+ end
39
+
40
+ XINPUT_GAMEPAD_DPAD_UP = 0x0001
41
+ XINPUT_GAMEPAD_DPAD_DOWN = 0x0002
42
+ XINPUT_GAMEPAD_DPAD_LEFT = 0x0004
43
+ XINPUT_GAMEPAD_DPAD_RIGHT = 0x0008
44
+ XINPUT_GAMEPAD_START = 0x0010
45
+ XINPUT_GAMEPAD_BACK = 0x0020
46
+ XINPUT_GAMEPAD_LEFT_THUMB = 0x0040
47
+ XINPUT_GAMEPAD_RIGHT_THUMB = 0x0080
48
+ XINPUT_GAMEPAD_LEFT_SHOULDER = 0x0100
49
+ XINPUT_GAMEPAD_RIGHT_SHOULDER = 0x0200
50
+ XINPUT_GAMEPAD_A = 0x1000
51
+ XINPUT_GAMEPAD_B = 0x2000
52
+ XINPUT_GAMEPAD_X = 0x4000
53
+ XINPUT_GAMEPAD_Y = 0x8000
54
+
55
+ end
56
+
57
+ class XInput
58
+
59
+ include FFI::XInput
60
+ extend FFI::XInput
61
+
62
+ attr_reader :id
63
+
64
+ def initialize(id)
65
+ @id = id
66
+ end
67
+
68
+ def state
69
+ return XInput.state(id)
70
+ end
71
+
72
+ def self.state(id)
73
+ state = XINPUT_STATE.new
74
+ result = XInputGetState(id, state)
75
+ gamepad = state[:Gamepad]
76
+
77
+ state = {}
78
+
79
+ state[:up] = gamepad[:wButtons] & XINPUT_GAMEPAD_DPAD_UP > 0
80
+ state[:down] = gamepad[:wButtons] & XINPUT_GAMEPAD_DPAD_DOWN > 0
81
+ state[:left] = gamepad[:wButtons] & XINPUT_GAMEPAD_DPAD_LEFT > 0
82
+ state[:right] = gamepad[:wButtons] & XINPUT_GAMEPAD_DPAD_RIGHT > 0
83
+ state[:start] = gamepad[:wButtons] & XINPUT_GAMEPAD_START > 0
84
+ state[:back] = gamepad[:wButtons] & XINPUT_GAMEPAD_BACK > 0
85
+ state[:left_thumb] = gamepad[:wButtons] & XINPUT_GAMEPAD_LEFT_THUMB > 0
86
+ state[:right_thumb] = gamepad[:wButtons] & XINPUT_GAMEPAD_RIGHT_THUMB > 0
87
+ state[:left_shoulder] = gamepad[:wButtons] & XINPUT_GAMEPAD_LEFT_SHOULDER > 0
88
+ state[:right_shoulder] = gamepad[:wButtons] & XINPUT_GAMEPAD_RIGHT_SHOULDER > 0
89
+ state[:a] = gamepad[:wButtons] & XINPUT_GAMEPAD_A > 0
90
+ state[:b] = gamepad[:wButtons] & XINPUT_GAMEPAD_B > 0
91
+ state[:x] = gamepad[:wButtons] & XINPUT_GAMEPAD_X > 0
92
+ state[:y] = gamepad[:wButtons] & XINPUT_GAMEPAD_Y > 0
93
+ state[:left_trigger] = gamepad[:bLeftTrigger].to_f / 0xFF.to_f
94
+ state[:right_trigger] = gamepad[:bRightTrigger].to_f / 0xFF.to_f
95
+ state[:left_thumb_x] = gamepad[:sThumbLX].to_f / 0x8000.to_f
96
+ state[:left_thumb_y] = gamepad[:sThumbLY].to_f / 0x8000.to_f
97
+ state[:right_thumb_x] = gamepad[:sThumbRX].to_f / 0x8000.to_f
98
+ state[:right_thumb_y] = gamepad[:sThumbRY].to_f / 0x8000.to_f
99
+
100
+ return state
101
+ end
102
+
103
+ def vibrate(left = 0, right = 0)
104
+ return XInput.vibrate(@id, left, right)
105
+ end
106
+
107
+ def self.vibrate(id, left = 0, right = 0)
108
+ vibration = XINPUT_VIBRATION.new
109
+ vibration[:wLeftMotorSpeed] = left * 0xFFFF
110
+ vibration[:wRightMotorSpeed] = right * 0xFFFF
111
+ return XInputSetState(id, vibration)
112
+ end
113
+
114
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ffi-xinput
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Allison Bailey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: allison@isans.net
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/ffi-xinput.rb
20
+ homepage: https://github.com/Miyuki333/ffi-xinput
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.7.6
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: An FFI wrapper for the XInput controller library.
44
+ test_files: []