event.go 19 KB

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