joystick.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package al
  2. /*
  3. #include <stdlib.h>
  4. #include <allegro5/allegro.h>
  5. #include "helpers.h"
  6. */
  7. import "C"
  8. import "runtime"
  9. // Usful regexp for KATE: ALLEGRO_([A-Z0-9_]+)(.*) -> \1 = C.ALLEGRO_\1
  10. // Joystick functionality.
  11. type Joystick struct {
  12. handle *C.ALLEGRO_JOYSTICK
  13. }
  14. // Destroys the joystick. According to the Allegro documentation, this
  15. // does nothing.
  16. func (self *Joystick) Destroy() {
  17. // Some problems if this is enabled so make sure this does nothing...
  18. // C.al_release_joystick(self.handle)
  19. }
  20. // Wraps a C joystick handler into the Go Joystick wrapper.
  21. func wrapJoystickRaw(handle *C.ALLEGRO_JOYSTICK) *Joystick {
  22. if handle == nil {
  23. return nil
  24. }
  25. return &Joystick{handle}
  26. }
  27. // Wraps a C joystick handler into the Go Joystick wrapper.
  28. // Also sets a finalizer that calls joystick.Destroy().
  29. func wrapJoystick(handle *C.ALLEGRO_JOYSTICK) *Joystick {
  30. self := wrapJoystickRaw(handle)
  31. if self != nil {
  32. runtime.SetFinalizer(self, func(me *Joystick) { me.Destroy() })
  33. }
  34. return self
  35. }
  36. // Struct that holds the state of the joystick.
  37. type JoystickState C.ALLEGRO_JOYSTICK_STATE
  38. // Converts a wrapped joystick state to a C joystick state.
  39. func (self *JoystickState) toC() *C.ALLEGRO_JOYSTICK_STATE {
  40. return (*C.ALLEGRO_JOYSTICK_STATE)(self)
  41. }
  42. // Gets the state of the axis for the given stick on the joystick.
  43. // returns 0.0 if the stick or axis are out of range. May return
  44. // garbage for nonexisting sticks and axes.
  45. func (self *JoystickState) StickAxis(stick, axis int) float32 {
  46. if stick >= int(C._AL_MAX_JOYSTICK_STICKS) {
  47. return 0.0
  48. }
  49. if axis >= int(C._AL_MAX_JOYSTICK_AXES) {
  50. return 0.0
  51. }
  52. if axis < 0 {
  53. return 0.0
  54. }
  55. if stick < 0 {
  56. return 0.0
  57. }
  58. return float32(self.stick[C.int(stick)].axis[C.int(axis)])
  59. }
  60. // Gerts the state of the button with the given index on the joystick.
  61. // Will return -1 if the button is out of range.
  62. func (self *JoystickState) Button(button int) int {
  63. if button >= int(C._AL_MAX_JOYSTICK_BUTTONS) {
  64. return -1
  65. }
  66. if button < 0 {
  67. return -1
  68. }
  69. return int(self.button[C.int(button)])
  70. }
  71. // Joystick flags that determine the type of the joystick.
  72. const (
  73. JOYFLAG_DIGITAL = C.ALLEGRO_JOYFLAG_DIGITAL
  74. JOYFLAG_ANALOGUE = C.ALLEGRO_JOYFLAG_ANALOGUE
  75. )
  76. // Installs the Allegro Joystick module.
  77. func InstallJoystick() bool {
  78. return bool(C.al_install_joystick())
  79. }
  80. // Uninstalls the Allegro Joystick module.
  81. func UninstallJoystick() {
  82. C.al_uninstall_joystick()
  83. }
  84. // Returns true if the Allegro joystick module ws instaled, false if not.
  85. func IsJoystickInstalled() bool {
  86. return bool(C.al_is_joystick_installed())
  87. }
  88. // Returns the amount of joysticks detected.
  89. func NumJoysticks() int {
  90. return int(C.al_get_num_joysticks())
  91. }
  92. // Returns the joyn'th joystick, or nil if no such stick exists.
  93. func FindJoystick(joyn int) *Joystick {
  94. return wrapJoystick(C.al_get_joystick(C.int(joyn)))
  95. }
  96. // Joystick properties.
  97. // Returns true if the joystick self is active, false if not.
  98. func (self *Joystick) IsActive() bool {
  99. return bool(C.al_get_joystick_active(self.handle))
  100. }
  101. // Returns the name of the joystick self.
  102. func (self *Joystick) Name() string {
  103. return gostr(C.al_get_joystick_name(self.handle))
  104. }
  105. // Returns the amount of sticks the joystick self has.
  106. func (self *Joystick) NumSticks() int {
  107. return int(C.al_get_joystick_num_sticks(self.handle))
  108. }
  109. // Returns the joystick flags for the numbered stick on the joystick self.
  110. func (self *Joystick) StickFlags(stick int) int {
  111. return int(C.al_get_joystick_stick_flags(self.handle, C.int(stick)))
  112. }
  113. // Returns true if the numbered stick on joystick self is digital, false if not.
  114. // Note that theoretically, a stick could be both digital and analog...
  115. func (self *Joystick) IsStickDigital(stick int) bool {
  116. return (JOYFLAG_DIGITAL & self.StickFlags(stick)) == JOYFLAG_DIGITAL
  117. }
  118. // Returns true if the numbered stick on joystick self is analog, false if not
  119. // Note that theoretically, a stick could be both digital and analog...
  120. func (self *Joystick) IsStickAnalog(stick int) bool {
  121. return (JOYFLAG_ANALOGUE & self.StickFlags(stick)) == JOYFLAG_ANALOGUE
  122. }
  123. // Returns a string that describes the joystick flags for the numbered stick
  124. // on the joystick self. Will return "Analog" for an analog joystick,
  125. // "Digital" for a digital joystick, "Hybrid" fo one that's both and
  126. // "None" for one that's neither
  127. func (self *Joystick) StickFlagsName(stick int) string {
  128. if self.IsStickAnalog(stick) {
  129. if self.IsStickDigital(stick) {
  130. return "Hybrid"
  131. } else {
  132. return "Analog"
  133. }
  134. }
  135. if self.IsStickDigital(stick) {
  136. return "Digital"
  137. }
  138. return "None"
  139. }
  140. // Returns the name of the stick on the joystick self.
  141. func (self *Joystick) StickName(stick int) string {
  142. return gostr(C.al_get_joystick_stick_name(self.handle, C.int(stick)))
  143. }
  144. // Returns the amount of axes for the stick on the joystick self.
  145. func (self *Joystick) NumAxes(stick int) int {
  146. return int(C.al_get_joystick_num_axes(self.handle, C.int(stick)))
  147. }
  148. // Returns the name of the axis for the stick on the joystick self.
  149. func (self *Joystick) AxisName(stick, axis int) string {
  150. return gostr(C.al_get_joystick_axis_name(self.handle, C.int(stick), C.int(axis)))
  151. }
  152. // Returns the amount of buttons on the joystick self.
  153. func (self *Joystick) NumButtons() int {
  154. return int(C.al_get_joystick_num_buttons(self.handle))
  155. }
  156. // Returns the name of the button on the joystick self.
  157. func (self *Joystick) ButtonName(button int) string {
  158. return gostr(C.al_get_joystick_button_name(self.handle, C.int(button)))
  159. }
  160. // Gets the state of the joystick
  161. func (self *Joystick) State() *JoystickState {
  162. state := &JoystickState{}
  163. C.al_get_joystick_state(self.handle, state.toC())
  164. return state
  165. }
  166. // Reconfigure the joystics afetre a new one connected.
  167. func ReconfigureJoysticks() bool {
  168. return bool(C.al_reconfigure_joysticks())
  169. }
  170. func JoystickEventSource() * EventSource {
  171. return wrapEventSourceRaw(C.al_get_joystick_event_source())
  172. }