event.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. package al
  2. /*
  3. #cgo pkg-config: allegro-5
  4. #cgo CFLAGS: -I/usr/local/include
  5. #cgo linux LDFLAGS: -lc_nonshared
  6. #include <stdlib.h>
  7. #include <allegro5/allegro.h>
  8. #include <allegro5/events.h>
  9. #include "helpers.h"
  10. */
  11. import "C"
  12. import "unsafe"
  13. import "runtime"
  14. // EventUnion Type, not to avoid complications.
  15. // type EVENT_TYPE C.ALLEGRO_EVENT_TYPE
  16. // EventUnion Type constants
  17. const (
  18. EVENT_JOYSTICK_AXIS = C.ALLEGRO_EVENT_JOYSTICK_AXIS
  19. EVENT_JOYSTICK_BUTTON_DOWN = C.ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN
  20. EVENT_JOYSTICK_BUTTON_UP = C.ALLEGRO_EVENT_JOYSTICK_BUTTON_UP
  21. EVENT_JOYSTICK_CONFIGURATION = C.ALLEGRO_EVENT_JOYSTICK_CONFIGURATION
  22. EVENT_KEY_DOWN = C.ALLEGRO_EVENT_KEY_DOWN
  23. EVENT_KEY_CHAR = C.ALLEGRO_EVENT_KEY_CHAR
  24. EVENT_KEY_UP = C.ALLEGRO_EVENT_KEY_UP
  25. EVENT_MOUSE_AXES = C.ALLEGRO_EVENT_MOUSE_AXES
  26. EVENT_MOUSE_BUTTON_DOWN = C.ALLEGRO_EVENT_MOUSE_BUTTON_DOWN
  27. EVENT_MOUSE_BUTTON_UP = C.ALLEGRO_EVENT_MOUSE_BUTTON_UP
  28. EVENT_MOUSE_ENTER_DISPLAY = C.ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY
  29. EVENT_MOUSE_LEAVE_DISPLAY = C.ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY
  30. EVENT_MOUSE_WARPED = C.ALLEGRO_EVENT_MOUSE_WARPED
  31. EVENT_TIMER = C.ALLEGRO_EVENT_TIMER
  32. EVENT_DISPLAY_EXPOSE = C.ALLEGRO_EVENT_DISPLAY_EXPOSE
  33. EVENT_DISPLAY_RESIZE = C.ALLEGRO_EVENT_DISPLAY_RESIZE
  34. EVENT_DISPLAY_CLOSE = C.ALLEGRO_EVENT_DISPLAY_CLOSE
  35. EVENT_DISPLAY_LOST = C.ALLEGRO_EVENT_DISPLAY_LOST
  36. EVENT_DISPLAY_FOUND = C.ALLEGRO_EVENT_DISPLAY_FOUND
  37. EVENT_DISPLAY_SWITCH_IN = C.ALLEGRO_EVENT_DISPLAY_SWITCH_IN
  38. EVENT_DISPLAY_SWITCH_OUT = C.ALLEGRO_EVENT_DISPLAY_SWITCH_OUT
  39. EVENT_DISPLAY_ORIENTATION = C.ALLEGRO_EVENT_DISPLAY_ORIENTATION
  40. EVENT_TOUCH_BEGIN = C.ALLEGRO_EVENT_TOUCH_BEGIN
  41. EVENT_TOUCH_END = C.ALLEGRO_EVENT_TOUCH_END
  42. EVENT_TOUCH_MOVE = C.ALLEGRO_EVENT_TOUCH_MOVE
  43. EVENT_TOUCH_CANCEL = C.ALLEGRO_EVENT_TOUCH_CANCEL
  44. EVENT_DISPLAY_CONNECTED = C.ALLEGRO_EVENT_DISPLAY_CONNECTED
  45. EVENT_DISPLAY_DISCONNECTED = C.ALLEGRO_EVENT_DISPLAY_DISCONNECTED
  46. )
  47. func EVENT_TYPE_IS_USER(t int) bool {
  48. return ((t) >= 512)
  49. }
  50. func GET_EVENT_TYPE(a, b, c, d int) int {
  51. return AL_ID(a, b, c, d)
  52. }
  53. func getAnyEvenTimestamp(any *C.ALLEGRO_ANY_EVENT) float64 {
  54. return float64(any.timestamp)
  55. }
  56. // Wrapper interface for different event sources
  57. type EventSourcer interface {
  58. EventSource() * EventSource
  59. }
  60. func (disp * Display) EventSource() * EventSource {
  61. return (*EventSource)(unsafe.Pointer(disp))
  62. }
  63. func (joy * Joystick) EventSource() * EventSource {
  64. return (*EventSource)(unsafe.Pointer(joy))
  65. }
  66. func (key * Keyboard) EventSource() * EventSource {
  67. return (*EventSource)(unsafe.Pointer(key))
  68. }
  69. func (mouse * Mouse) EventSource() * EventSource {
  70. return (*EventSource)(unsafe.Pointer(mouse))
  71. }
  72. func (touch * TouchInput) EventSource() * EventSource {
  73. return (*EventSource)(unsafe.Pointer(touch))
  74. }
  75. func (timer * Timer) EventSource() * EventSource {
  76. return (*EventSource)(unsafe.Pointer(timer))
  77. }
  78. func (es * EventSource) EventSource() * EventSource {
  79. return es
  80. }
  81. // Event sources that emit events.
  82. type EventSource C.ALLEGRO_EVENT_SOURCE
  83. // Wraps an event source pointer but sets no finalizer (not needed anyway)
  84. func wrapEventSourceRaw(ptr *C.ALLEGRO_EVENT_SOURCE) *EventSource {
  85. return (*EventSource)(ptr)
  86. }
  87. // Converts wrapper EventUnion source pointer to C Allegro event source pointer
  88. func (self *EventSource) toC() *C.ALLEGRO_EVENT_SOURCE {
  89. return (*C.ALLEGRO_EVENT_SOURCE)(self)
  90. }
  91. // Events that the event system emits.
  92. type EventUnion C.ALLEGRO_EVENT
  93. // use an EventUnion interface to wrap the
  94. // C union's members in
  95. type Event interface {
  96. Type() int
  97. Source() EventSourcer
  98. Timestamp() float64
  99. }
  100. type AnyEvent C.ALLEGRO_ANY_EVENT
  101. type DisplayEvent C.ALLEGRO_DISPLAY_EVENT
  102. type JoystickEvent C.ALLEGRO_JOYSTICK_EVENT
  103. type KeyboardEvent C.ALLEGRO_KEYBOARD_EVENT
  104. type MouseEvent C.ALLEGRO_MOUSE_EVENT
  105. type TimerEvent C.ALLEGRO_TIMER_EVENT
  106. type TouchEvent C.ALLEGRO_TOUCH_EVENT
  107. type UserEvent C.ALLEGRO_USER_EVENT
  108. func (ev JoystickEvent ) MarkJoystick() { ; }
  109. func (ev KeyboardEvent ) MarkKey() { ; }
  110. func (ev MouseEvent ) MarkMouse() { ; }
  111. func (ev DisplayEvent ) MarkDisplay() { ; }
  112. func (ev TouchEvent ) MarkTouch() { ; }
  113. // Converts wrapper EventUnion pointer to C Allegro event pointer
  114. func (self *EventUnion) toC() *C.ALLEGRO_EVENT {
  115. return (*C.ALLEGRO_EVENT)(self)
  116. }
  117. // Returns an unsafe pointer to the event
  118. func (self *EventUnion) toPointer() unsafe.Pointer {
  119. return unsafe.Pointer(self.toC())
  120. }
  121. func (self *EventUnion) toUP() unsafe.Pointer {
  122. return unsafe.Pointer(self.toC())
  123. }
  124. // Converts wrapper EventUnion pointer to an Event interface struct
  125. func (evun *EventUnion) Event() Event {
  126. ae := (*C.ALLEGRO_ANY_EVENT)(evun.toPointer())
  127. switch (C.int(ae._type)) {
  128. case EVENT_JOYSTICK_AXIS : fallthrough
  129. case EVENT_JOYSTICK_BUTTON_DOWN : fallthrough
  130. case EVENT_JOYSTICK_BUTTON_UP : fallthrough
  131. case EVENT_JOYSTICK_CONFIGURATION :
  132. return (*JoystickEvent)(evun.toPointer())
  133. case EVENT_KEY_CHAR : fallthrough
  134. case EVENT_KEY_DOWN : fallthrough
  135. case EVENT_KEY_UP :
  136. return (*KeyboardEvent)(evun.toPointer())
  137. case EVENT_MOUSE_AXES : fallthrough
  138. case EVENT_MOUSE_BUTTON_DOWN : fallthrough
  139. case EVENT_MOUSE_BUTTON_UP : fallthrough
  140. case EVENT_MOUSE_ENTER_DISPLAY : fallthrough
  141. case EVENT_MOUSE_LEAVE_DISPLAY : fallthrough
  142. case EVENT_MOUSE_WARPED :
  143. return (*MouseEvent)(evun.toPointer())
  144. case EVENT_TIMER :
  145. return (*TimerEvent)(evun.toPointer())
  146. case EVENT_DISPLAY_EXPOSE : fallthrough
  147. case EVENT_DISPLAY_RESIZE : fallthrough
  148. case EVENT_DISPLAY_CLOSE : fallthrough
  149. case EVENT_DISPLAY_LOST : fallthrough
  150. case EVENT_DISPLAY_FOUND : fallthrough
  151. case EVENT_DISPLAY_SWITCH_IN : fallthrough
  152. case EVENT_DISPLAY_SWITCH_OUT : fallthrough
  153. case EVENT_DISPLAY_ORIENTATION :
  154. return (*DisplayEvent)(evun.toPointer())
  155. case EVENT_TOUCH_BEGIN : fallthrough
  156. case EVENT_TOUCH_MOVE : fallthrough
  157. case EVENT_TOUCH_CANCEL : fallthrough
  158. case EVENT_TOUCH_END :
  159. return (*TouchEvent)(evun.toPointer())
  160. default: break
  161. }
  162. if EVENT_TYPE_IS_USER(int(ae._type)) {
  163. return (*UserEvent)(evun.toPointer())
  164. }
  165. return (*AnyEvent)(evun.toPointer())
  166. }
  167. /* These wrappers implement the interface Event for all event types */
  168. func (ev AnyEvent ) Type() int { return int(ev._type); }
  169. func (ev DisplayEvent ) Type() int { return int(ev._type); }
  170. func (ev JoystickEvent) Type() int { return int(ev._type); }
  171. func (ev KeyboardEvent) Type() int { return int(ev._type); }
  172. func (ev MouseEvent ) Type() int { return int(ev._type); }
  173. func (ev TimerEvent ) Type() int { return int(ev._type); }
  174. func (ev TouchEvent ) Type() int { return int(ev._type); }
  175. func (ev UserEvent ) Type() int { return int(ev._type); }
  176. func (ev AnyEvent ) Source() EventSourcer { return wrapEventSourceRaw (ev.source); }
  177. func (ev DisplayEvent ) Source() EventSourcer { return wrapDisplayRaw (ev.source); }
  178. func (ev JoystickEvent) Source() EventSourcer { return wrapJoystickRaw (ev.source); }
  179. func (ev KeyboardEvent) Source() EventSourcer { return wrapKeyboardRaw (ev.source); }
  180. func (ev MouseEvent ) Source() EventSourcer { return wrapMouseRaw (ev.source); }
  181. func (ev TimerEvent ) Source() EventSourcer { return wrapTimerRaw (ev.source); }
  182. func (ev TouchEvent ) Source() EventSourcer { return wrapTouchInputRaw (ev.source); }
  183. func (ev UserEvent ) Source() EventSourcer { return wrapEventSourceRaw (ev.source); }
  184. func (ev AnyEvent ) Timestamp() float64 { return float64(ev.timestamp); }
  185. func (ev DisplayEvent ) Timestamp() float64 { return float64(ev.timestamp); }
  186. func (ev JoystickEvent) Timestamp() float64 { return float64(ev.timestamp); }
  187. func (ev KeyboardEvent) Timestamp() float64 { return float64(ev.timestamp); }
  188. func (ev MouseEvent ) Timestamp() float64 { return float64(ev.timestamp); }
  189. func (ev TimerEvent ) Timestamp() float64 { return float64(ev.timestamp); }
  190. func (ev TouchEvent ) Timestamp() float64 { return float64(ev.timestamp); }
  191. func (ev UserEvent ) Timestamp() float64 { return float64(ev.timestamp); }
  192. func (eu * EventUnion) AnyEvent () *AnyEvent { return (*AnyEvent )(eu.toPointer()); }
  193. func (eu * EventUnion) DisplayEvent () *DisplayEvent { if !eu.IsDisplayEvent () { return nil; } else { return (*DisplayEvent )(eu.toPointer());}; }
  194. func (eu * EventUnion) JoystickEvent() *JoystickEvent { if !eu.IsJoystickEvent() { return nil; } else { return (*JoystickEvent)(eu.toPointer());}; }
  195. func (eu * EventUnion) KeyboardEvent() *KeyboardEvent { if !eu.IsKeyboardEvent() { return nil; } else { return (*KeyboardEvent)(eu.toPointer());}; }
  196. func (eu * EventUnion) MouseEvent () *MouseEvent { if !eu.IsMouseEvent () { return nil; } else { return (*MouseEvent )(eu.toPointer());}; }
  197. func (eu * EventUnion) TimerEvent () *TimerEvent { if !eu.IsTimerEvent () { return nil; } else { return (*TimerEvent )(eu.toPointer());}; }
  198. func (eu * EventUnion) TouchEvent () *TouchEvent { if !eu.IsTouchEvent () { return nil; } else { return (*TouchEvent )(eu.toPointer());}; }
  199. func (eu * EventUnion) UserEvent () *UserEvent { if !eu.IsUserEvent () { return nil; } else { return (*UserEvent )(eu.toPointer());}; }
  200. // Returns the type of the event.
  201. func (self *EventUnion) Type() int {
  202. return self.AnyEvent().Type()
  203. }
  204. // Returns the timestamp of the event.
  205. func (self *EventUnion) Timestamp() float64 {
  206. return self.AnyEvent().Timestamp()
  207. }
  208. // Returns the event source of the event
  209. func (self *EventUnion) Source() EventSourcer {
  210. return self.AnyEvent().Source()
  211. }
  212. // Returns true if this is a dispay event, false if not.
  213. func (self *EventUnion) IsDisplayEvent() bool {
  214. t := self.Type()
  215. return ((t >= EVENT_DISPLAY_EXPOSE) && (t <= EVENT_DISPLAY_ORIENTATION) || ((t>= EVENT_DISPLAY_CONNECTED) && (t<= EVENT_DISPLAY_DISCONNECTED) ))
  216. }
  217. // Returns true if this is a mouse event, false if not.
  218. func (self *EventUnion) IsMouseEvent() bool {
  219. t := self.Type()
  220. return (t >= EVENT_MOUSE_AXES) && (t <= EVENT_MOUSE_WARPED)
  221. }
  222. // Returns true if this is a Joystick event, false if not.
  223. func (self *EventUnion) IsJoystickEvent() bool {
  224. t := self.Type()
  225. return (t >= EVENT_JOYSTICK_AXIS) && (t <= EVENT_JOYSTICK_CONFIGURATION)
  226. }
  227. // Returns true if this is a keyboard event, false if not.
  228. func (self *EventUnion) IsKeyboardEvent() bool {
  229. t := self.Type()
  230. return (t >= EVENT_KEY_DOWN) && (t <= EVENT_KEY_UP)
  231. }
  232. // Returns true if this is a keyboard event, false if not.
  233. func (self *EventUnion) IsTouchEvent() bool {
  234. t := self.Type()
  235. return (t >= EVENT_TOUCH_BEGIN) && (t <= EVENT_TOUCH_CANCEL)
  236. }
  237. // Returns true if this is a timer event, false if not.
  238. func (self *EventUnion) IsTimerEvent() bool {
  239. t := self.Type()
  240. return (t == EVENT_TIMER)
  241. }
  242. // Returns true if this is a user event, false if not.
  243. func (self *EventUnion) IsUserEvent() bool {
  244. t := self.Type()
  245. return EVENT_TYPE_IS_USER(t)
  246. }
  247. // Returns the X position of the display event.
  248. func (de DisplayEvent) X() int {
  249. return int(de.x)
  250. }
  251. // Returns the Y position of the display event.
  252. func (de DisplayEvent) Y() int {
  253. return int(de.y)
  254. }
  255. // Returns the width of the display event.
  256. func (de DisplayEvent) Width() int {
  257. return int(de.width)
  258. }
  259. // Returns the height of the display event.
  260. func (de DisplayEvent) Height() int {
  261. return int(de.height)
  262. }
  263. // Returns the orientation of the display event.
  264. func (de DisplayEvent) Orientation() int {
  265. return int(de.orientation)
  266. }
  267. // Returns the ID of the joystick for the joystick event.
  268. func (je JoystickEvent) ID() int {
  269. jsptr := je.id
  270. for i := 0 ; i < NumJoysticks() ; i++ {
  271. js := FindJoystick(i)
  272. if js.handle == jsptr {
  273. return i
  274. }
  275. }
  276. /* shouln't happen, but... */
  277. return 0xbad101
  278. }
  279. // Returns the stick number of the joystick event.
  280. func (je JoystickEvent) Stick() int {
  281. return int(je.stick)
  282. }
  283. // Returns the axis number of the joystick event.
  284. func (je JoystickEvent) Axis() int {
  285. return int(je.axis)
  286. }
  287. // Returns the button number of the joystick event.
  288. func (je JoystickEvent) Button() int {
  289. return int(je.button)
  290. }
  291. // Returns the position of the joystick axis during the event.
  292. func (je JoystickEvent) Pos() float32 {
  293. return float32(je.pos)
  294. }
  295. // Returns the display that has emitted the keyboard event.
  296. func (ke KeyboardEvent) Display() *Display {
  297. return wrapDisplayRaw(ke.display)
  298. }
  299. // Returns the key code of the keyboard event.
  300. func (ke KeyboardEvent) KeyCode() int {
  301. return int(ke.keycode)
  302. }
  303. // Returns the unicode character of the keyboard event.
  304. func (ke KeyboardEvent) Unichar() rune {
  305. return rune(ke.unichar)
  306. }
  307. // Returns the modifiers of the keyboard event.
  308. func (ke KeyboardEvent) Modifiers() int {
  309. return int(ke.modifiers)
  310. }
  311. // Returns is the keyboard event was automatically repeated or not.
  312. func (ke KeyboardEvent) Repeat() bool {
  313. return bool(ke.repeat)
  314. }
  315. // Returns the X position of the mouse event.
  316. func (me MouseEvent) X() int {
  317. return int(me.x)
  318. }
  319. // Returns the Y position of the mouse event.
  320. func (me MouseEvent) Y() int {
  321. return int(me.y)
  322. }
  323. // Returns the Z position of the mouse event.
  324. func (me MouseEvent) Z() int {
  325. return int(me.z)
  326. }
  327. // Returns the W position of the mouse event.
  328. func (me MouseEvent) W() int {
  329. return int(me.w)
  330. }
  331. // Returns the delta of the X position of the mouse event.
  332. func (me MouseEvent) DX() int {
  333. return int(me.dx)
  334. }
  335. // Returns the delta of the Y position of the mouse event.
  336. func (me MouseEvent) DY() int {
  337. return int(me.dy)
  338. }
  339. // Returns the delta of the Z position of the mouse event.
  340. func (me MouseEvent) DZ() int {
  341. return int(me.dz)
  342. }
  343. // Returns the delta of the W position of the mouse event.
  344. func (me MouseEvent) DW() int {
  345. return int(me.dw)
  346. }
  347. // Returns the button of the mouse event.
  348. func (me MouseEvent) Button() int {
  349. return int(me.button)
  350. }
  351. // Returns the pressure of the mouse event.
  352. func (me MouseEvent) Pressure() float32 {
  353. return float32(me.pressure)
  354. }
  355. // Returns the display that has emitted the mouse event.
  356. func (me MouseEvent) Display() *Display {
  357. return wrapDisplayRaw(me.display)
  358. }
  359. // Returns the error of the timer event.
  360. func (te TimerEvent) Error() float64 {
  361. return float64(te.error)
  362. }
  363. // Returns the ticks of the timer event.
  364. func (te TimerEvent) Count() int64 {
  365. return int64(te.count)
  366. }
  367. // Returns the X position of the touch input event.
  368. func (te TouchEvent) X() int {
  369. return int(te.x)
  370. }
  371. // Returns the Y position of the touch input event.
  372. func (te TouchEvent) Y() int {
  373. return int(te.y)
  374. }
  375. // Returns the ID of the touch input event.
  376. func (te TouchEvent) ID() int {
  377. return int(te.id)
  378. }
  379. // Returns whether the touch input event is primary or not.
  380. func (te TouchEvent) Primary() bool {
  381. return bool(te.primary)
  382. }
  383. // Returns the delta of the X position of the touch input event.
  384. func (te TouchEvent) DX() int {
  385. return int(te.dx)
  386. }
  387. // Returns the delta of the Y position of the touch input event.
  388. func (te TouchEvent) DY() int {
  389. return int(te.dy)
  390. }
  391. /* The safest way to use user events from Go is to use integer handles
  392. * as offsets into a GO-allocated map. */
  393. func (ue UserEvent) Data1Pointer() unsafe.Pointer { return unsafe.Pointer(uintptr(ue.data1));}
  394. func (ue UserEvent) Data1Integer() int64 { return int64(ue.data1); }
  395. func (ue UserEvent) Data2Pointer() unsafe.Pointer { return unsafe.Pointer(uintptr(ue.data2));}
  396. func (ue UserEvent) Data2Integer() int64 { return int64(ue.data2); }
  397. func (ue UserEvent) Data3Pointer() unsafe.Pointer { return unsafe.Pointer(uintptr(ue.data3));}
  398. func (ue UserEvent) Data3Integer() int64 { return int64(ue.data3); }
  399. func (ue UserEvent) Data4Pointer() unsafe.Pointer { return unsafe.Pointer(uintptr(ue.data4));}
  400. func (ue UserEvent) Data4Integer() int64 { return int64(ue.data4); }
  401. func (ue UserEvent) SetData1Integer(v int64) { ue.data1 = C.intptr_t(v); }
  402. func (ue UserEvent) SetData2Integer(v int64) { ue.data2 = C.intptr_t(v); }
  403. func (ue UserEvent) SetData3Integer(v int64) { ue.data3 = C.intptr_t(v); }
  404. func (ue UserEvent) SetData4Integer(v int64) { ue.data4 = C.intptr_t(v); }
  405. /*
  406. The compiler accept this, but it's unlikely to work correctly.
  407. func (ue UserEvent) Data1() interface{} { return (interface{})(ue.Data1Pointer());}
  408. func (ue UserEvent) Data2() interface{} { return (interface{})(ue.Data2Pointer());}
  409. func (ue UserEvent) Data3() interface{} { return (interface{})(ue.Data3Pointer());}
  410. func (ue UserEvent) Data4() interface{} { return (interface{})(ue.Data4Pointer());}
  411. */
  412. // Event queues.
  413. type EventQueue struct {
  414. handle *C.ALLEGRO_EVENT_QUEUE
  415. }
  416. // Destroys the event queue.
  417. func (self *EventQueue) Destroy() {
  418. if self.handle != nil {
  419. C.al_destroy_event_queue(self.handle)
  420. }
  421. self.handle = nil
  422. }
  423. // Wraps an event queue, but does not set a finalizer.
  424. func wrapEventQueueRaw(handle *C.ALLEGRO_EVENT_QUEUE) *EventQueue {
  425. if handle == nil {
  426. return nil
  427. }
  428. return &EventQueue{handle}
  429. }
  430. func (eq * EventQueue) toC() (handle *C.ALLEGRO_EVENT_QUEUE) {
  431. return eq.handle
  432. }
  433. // Wraps an event queue and sets a finalizer that calls Destroy
  434. func wrapEventQueue(handle *C.ALLEGRO_EVENT_QUEUE) *EventQueue {
  435. result := wrapEventQueueRaw(handle)
  436. if result != nil {
  437. runtime.SetFinalizer(result, func(me *EventQueue) { me.Destroy() })
  438. }
  439. return result
  440. }
  441. // Create an event queue.
  442. func CreateEventQueue() *EventQueue {
  443. return wrapEventQueue(C.al_create_event_queue())
  444. }
  445. // Register an event source with self.
  446. func (self *EventQueue) RegisterEventSource(src *EventSource) {
  447. C.al_register_event_source(self.handle, src.toC())
  448. }
  449. // Unregister an event source with self.
  450. func (self *EventQueue) UnregisterEventSource(src *EventSource) {
  451. C.al_unregister_event_source(self.handle, src.toC())
  452. }
  453. // Returns true if the event queue self is empty, false if not.
  454. func (self *EventQueue) IsEmpty() bool {
  455. return bool(C.al_is_event_queue_empty(self.handle))
  456. }
  457. // Returns the next event from the event queue as well as a bool
  458. // to signify if an event was fetched sucessfully or not.
  459. func (self *EventQueue) NextEvent() (event *EventUnion, ok bool) {
  460. event = &EventUnion{}
  461. ok = bool(C.al_get_next_event(self.handle, event.toC()))
  462. return event, ok
  463. }
  464. // Peeks at the next event in the event queue and returns it as well as a bool
  465. // to signify if an event was fetched sucessfully or not.
  466. func (self *EventQueue) PeekNextEvent() (event *EventUnion, ok bool) {
  467. event = &EventUnion{}
  468. ok = bool(C.al_peek_next_event(self.handle, event.toC()))
  469. return event, ok
  470. }
  471. // Drops the next event from the event queue
  472. func (self *EventQueue) DropNextEvent() bool {
  473. return bool(C.al_drop_next_event(self.handle))
  474. }
  475. // Flushes the event queue
  476. func (self *EventQueue) Flush() {
  477. C.al_flush_event_queue(self.handle)
  478. }
  479. // Waits for the next event from the event queue
  480. func (self *EventQueue) WaitForEvent() (event *EventUnion) {
  481. event = &EventUnion{}
  482. C.al_wait_for_event(self.handle, event.toC())
  483. return event
  484. }
  485. // Waits for secs seconds the next event from the event queue
  486. func (self *EventQueue) WaitForEventTimed(secs float32) (event *EventUnion, ok bool) {
  487. event = &EventUnion{}
  488. ok = bool(C.al_wait_for_event_timed(self.handle, event.toC(), C.float(secs)))
  489. return event, ok
  490. }
  491. func (queue *EventQueue) WaitForEventUntil(timeout * Timeout) (event *EventUnion, ok bool) {
  492. event = &EventUnion{}
  493. ok = bool(C.al_wait_for_event_until(queue.toC(), event.toC(), timeout.toC()))
  494. return event, ok
  495. }