event.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. // Converts wrapper EventUnion pointer to C Allegro event pointer
  109. func (self *EventUnion) toC() *C.ALLEGRO_EVENT {
  110. return (*C.ALLEGRO_EVENT)(self)
  111. }
  112. // Returns an unsafe pointer to the event
  113. func (self *EventUnion) toPointer() unsafe.Pointer {
  114. return unsafe.Pointer(self.toC())
  115. }
  116. // Converts wrapper EventUnion pointer to an EventUnion interface struct
  117. func (evun *EventUnion) Event() Event {
  118. ae := (*C.ALLEGRO_ANY_EVENT)(evun.toPointer())
  119. switch (C.int(ae._type)) {
  120. case EVENT_JOYSTICK_AXIS : fallthrough
  121. case EVENT_JOYSTICK_BUTTON_DOWN : fallthrough
  122. case EVENT_JOYSTICK_BUTTON_UP : fallthrough
  123. case EVENT_JOYSTICK_CONFIGURATION :
  124. return (*JoystickEvent)(evun.toPointer())
  125. case EVENT_KEY_CHAR : fallthrough
  126. case EVENT_KEY_DOWN : fallthrough
  127. case EVENT_KEY_UP :
  128. return (*KeyboardEvent)(evun.toPointer())
  129. case EVENT_MOUSE_AXES : fallthrough
  130. case EVENT_MOUSE_BUTTON_DOWN : fallthrough
  131. case EVENT_MOUSE_BUTTON_UP : fallthrough
  132. case EVENT_MOUSE_ENTER_DISPLAY : fallthrough
  133. case EVENT_MOUSE_LEAVE_DISPLAY : fallthrough
  134. case EVENT_MOUSE_WARPED :
  135. return (*MouseEvent)(evun.toPointer())
  136. case EVENT_TIMER :
  137. return (*TimerEvent)(evun.toPointer())
  138. case EVENT_DISPLAY_EXPOSE : fallthrough
  139. case EVENT_DISPLAY_RESIZE : fallthrough
  140. case EVENT_DISPLAY_CLOSE : fallthrough
  141. case EVENT_DISPLAY_LOST : fallthrough
  142. case EVENT_DISPLAY_FOUND : fallthrough
  143. case EVENT_DISPLAY_SWITCH_IN : fallthrough
  144. case EVENT_DISPLAY_SWITCH_OUT : fallthrough
  145. case EVENT_DISPLAY_ORIENTATION :
  146. return (*DisplayEvent)(evun.toPointer())
  147. default: break
  148. }
  149. if EVENT_TYPE_IS_USER(int(ae._type)) {
  150. return (*UserEvent)(evun.toPointer())
  151. }
  152. return (*AnyEvent)(evun.toPointer())
  153. }
  154. /* These wrappers implement the interface Event for all event types */
  155. func (ev AnyEvent ) Type() int { return int(ev._type); }
  156. func (ev DisplayEvent ) Type() int { return int(ev._type); }
  157. func (ev JoystickEvent) Type() int { return int(ev._type); }
  158. func (ev KeyboardEvent) Type() int { return int(ev._type); }
  159. func (ev MouseEvent ) Type() int { return int(ev._type); }
  160. func (ev TimerEvent ) Type() int { return int(ev._type); }
  161. func (ev TouchEvent ) Type() int { return int(ev._type); }
  162. func (ev UserEvent ) Type() int { return int(ev._type); }
  163. func (ev AnyEvent ) Source() EventSourcer { return wrapEventSourceRaw (ev.source); }
  164. func (ev DisplayEvent ) Source() EventSourcer { return wrapDisplayRaw (ev.source); }
  165. func (ev JoystickEvent) Source() EventSourcer { return wrapJoystickRaw (ev.source); }
  166. func (ev KeyboardEvent) Source() EventSourcer { return wrapKeyboardRaw (ev.source); }
  167. func (ev MouseEvent ) Source() EventSourcer { return wrapMouseRaw (ev.source); }
  168. func (ev TimerEvent ) Source() EventSourcer { return wrapTimerRaw (ev.source); }
  169. func (ev TouchEvent ) Source() EventSourcer { return wrapTouchInputRaw (ev.source); }
  170. func (ev UserEvent ) Source() EventSourcer { return wrapEventSourceRaw (ev.source); }
  171. func (ev AnyEvent ) Timestamp() float64 { return float64(ev.timestamp); }
  172. func (ev DisplayEvent ) Timestamp() float64 { return float64(ev.timestamp); }
  173. func (ev JoystickEvent) Timestamp() float64 { return float64(ev.timestamp); }
  174. func (ev KeyboardEvent) Timestamp() float64 { return float64(ev.timestamp); }
  175. func (ev MouseEvent ) Timestamp() float64 { return float64(ev.timestamp); }
  176. func (ev TimerEvent ) Timestamp() float64 { return float64(ev.timestamp); }
  177. func (ev TouchEvent ) Timestamp() float64 { return float64(ev.timestamp); }
  178. func (ev UserEvent ) Timestamp() float64 { return float64(ev.timestamp); }
  179. func (eu * EventUnion) AnyEvent () *AnyEvent { return (*AnyEvent )(eu.toPointer()); }
  180. func (eu * EventUnion) DisplayEvent () *DisplayEvent { if !eu.IsDisplayEvent () { return nil; } else { return (*DisplayEvent )(eu.toPointer());}; }
  181. func (eu * EventUnion) JoystickEvent() *JoystickEvent { if !eu.IsJoystickEvent() { return nil; } else { return (*JoystickEvent)(eu.toPointer());}; }
  182. func (eu * EventUnion) KeyboardEvent() *KeyboardEvent { if !eu.IsKeyboardEvent() { return nil; } else { return (*KeyboardEvent)(eu.toPointer());}; }
  183. func (eu * EventUnion) MouseEvent () *MouseEvent { if !eu.IsMouseEvent () { return nil; } else { return (*MouseEvent )(eu.toPointer());}; }
  184. func (eu * EventUnion) TimerEvent () *TimerEvent { if !eu.IsTimerEvent () { return nil; } else { return (*TimerEvent )(eu.toPointer());}; }
  185. func (eu * EventUnion) TouchEvent () *TouchEvent { if !eu.IsTouchEvent () { return nil; } else { return (*TouchEvent )(eu.toPointer());}; }
  186. func (eu * EventUnion) UserEvent () *UserEvent { if !eu.IsUserEvent () { return nil; } else { return (*UserEvent )(eu.toPointer());}; }
  187. // Returns the type of the event.
  188. func (self *EventUnion) Type() int {
  189. return self.AnyEvent().Type()
  190. }
  191. // Returns the timestamp of the event.
  192. func (self *EventUnion) Timestamp() float64 {
  193. return self.AnyEvent().Timestamp()
  194. }
  195. // Returns the event source of the event
  196. func (self *EventUnion) Source() EventSourcer {
  197. return self.AnyEvent().Source()
  198. }
  199. // Returns true if this is a dispay event, false if not.
  200. func (self *EventUnion) IsDisplayEvent() bool {
  201. t := self.Type()
  202. return ((t >= EVENT_DISPLAY_EXPOSE) && (t <= EVENT_DISPLAY_ORIENTATION) || ((t>= EVENT_DISPLAY_CONNECTED) && (t<= EVENT_DISPLAY_DISCONNECTED) ))
  203. }
  204. // Returns true if this is a mouse event, false if not.
  205. func (self *EventUnion) IsMouseEvent() bool {
  206. t := self.Type()
  207. return (t >= EVENT_MOUSE_AXES) && (t <= EVENT_MOUSE_WARPED)
  208. }
  209. // Returns true if this is a Joystick event, false if not.
  210. func (self *EventUnion) IsJoystickEvent() bool {
  211. t := self.Type()
  212. return (t >= EVENT_JOYSTICK_AXIS) && (t <= EVENT_JOYSTICK_CONFIGURATION)
  213. }
  214. // Returns true if this is a keyboard event, false if not.
  215. func (self *EventUnion) IsKeyboardEvent() bool {
  216. t := self.Type()
  217. return (t >= EVENT_KEY_DOWN) && (t <= EVENT_KEY_UP)
  218. }
  219. // Returns true if this is a keyboard event, false if not.
  220. func (self *EventUnion) IsTouchEvent() bool {
  221. t := self.Type()
  222. return (t >= EVENT_TOUCH_BEGIN) && (t <= EVENT_TOUCH_CANCEL)
  223. }
  224. // Returns true if this is a timer event, false if not.
  225. func (self *EventUnion) IsTimerEvent() bool {
  226. t := self.Type()
  227. return (t == EVENT_TIMER)
  228. }
  229. // Returns true if this is a user event, false if not.
  230. func (self *EventUnion) IsUserEvent() bool {
  231. t := self.Type()
  232. return EVENT_TYPE_IS_USER(t)
  233. }
  234. // Returns the X position of the display event.
  235. func (de DisplayEvent) X() int {
  236. return int(de.x)
  237. }
  238. // Returns the Y position of the display event.
  239. func (de DisplayEvent) Y() int {
  240. return int(de.y)
  241. }
  242. // Returns the width of the display event.
  243. func (de DisplayEvent) Width() int {
  244. return int(de.width)
  245. }
  246. // Returns the height of the display event.
  247. func (de DisplayEvent) Height() int {
  248. return int(de.height)
  249. }
  250. // Returns the orientation of the display event.
  251. func (de DisplayEvent) Orientation() int {
  252. return int(de.orientation)
  253. }
  254. // Returns the stick number of the joystick event.
  255. func (je JoystickEvent) Stick() int {
  256. return int(je.stick)
  257. }
  258. // Returns the axis number of the joystick event.
  259. func (je JoystickEvent) Axis() int {
  260. return int(je.axis)
  261. }
  262. // Returns the button number of the joystick event.
  263. func (je JoystickEvent) Button() int {
  264. return int(je.button)
  265. }
  266. // Returns the position of the joystick event.
  267. func (je JoystickEvent) Pos() int {
  268. return int(je.pos)
  269. }
  270. // Returns the display that has emitted the keyboard event.
  271. func (ke KeyboardEvent) Display() *Display {
  272. return wrapDisplayRaw(ke.display)
  273. }
  274. // Returns the key code of the keyboard event.
  275. func (ke KeyboardEvent) Keycode() int {
  276. return int(ke.keycode)
  277. }
  278. // Returns the unicode character of the keyboard event.
  279. func (ke KeyboardEvent) Unichar() rune {
  280. return rune(ke.unichar)
  281. }
  282. // Returns the modifiers of the keyboard event.
  283. func (ke KeyboardEvent) Modifiers() int {
  284. return int(ke.modifiers)
  285. }
  286. // Returns is the keyboard event was automatically repeated or not.
  287. func (ke KeyboardEvent) Repeat() bool {
  288. return bool(ke.repeat)
  289. }
  290. // Returns the X position of the mouse event.
  291. func (me MouseEvent) X() int {
  292. return int(me.x)
  293. }
  294. // Returns the Y position of the mouse event.
  295. func (me MouseEvent) Y() int {
  296. return int(me.y)
  297. }
  298. // Returns the Z position of the mouse event.
  299. func (me MouseEvent) Z() int {
  300. return int(me.z)
  301. }
  302. // Returns the W position of the mouse event.
  303. func (me MouseEvent) W() int {
  304. return int(me.w)
  305. }
  306. // Returns the delta of the X position of the mouse event.
  307. func (me MouseEvent) DX() int {
  308. return int(me.dx)
  309. }
  310. // Returns the delta of the Y position of the mouse event.
  311. func (me MouseEvent) DY() int {
  312. return int(me.dy)
  313. }
  314. // Returns the delta of the Z position of the mouse event.
  315. func (me MouseEvent) DZ() int {
  316. return int(me.dz)
  317. }
  318. // Returns the delta of the W position of the mouse event.
  319. func (me MouseEvent) DW() int {
  320. return int(me.dw)
  321. }
  322. // Returns the button of the mouse event.
  323. func (me MouseEvent) Button() int {
  324. return int(me.button)
  325. }
  326. // Returns the pressure of the mouse event.
  327. func (me MouseEvent) Pressure() float32 {
  328. return float32(me.pressure)
  329. }
  330. // Returns the display that has emitted the mouse event.
  331. func (me MouseEvent) Display() *Display {
  332. return wrapDisplayRaw(me.display)
  333. }
  334. // Returns the error of the timer event.
  335. func (te TimerEvent) Error() float64 {
  336. return float64(te.error)
  337. }
  338. // Returns the ticks of the timer event.
  339. func (te TimerEvent) Count() int64 {
  340. return int64(te.count)
  341. }
  342. // Returns the X position of the touch input event.
  343. func (te TouchEvent) X() int {
  344. return int(te.x)
  345. }
  346. // Returns the Y position of the touch input event.
  347. func (te TouchEvent) Y() int {
  348. return int(te.y)
  349. }
  350. // Returns the ID of the touch input event.
  351. func (te TouchEvent) ID() int {
  352. return int(te.id)
  353. }
  354. // Returns whether the touch input event is primary or not.
  355. func (te TouchEvent) Primary() bool {
  356. return bool(te.primary)
  357. }
  358. // Returns the delta of the X position of the touch input event.
  359. func (te TouchEvent) DX() int {
  360. return int(te.dx)
  361. }
  362. // Returns the delta of the Y position of the touch input event.
  363. func (te TouchEvent) DY() int {
  364. return int(te.dy)
  365. }
  366. /* The safest way to use user events from GO is to use integer handles
  367. * as offsets into a GO-allocated map. */
  368. func (ue UserEvent) Data1Pointer() unsafe.Pointer { return unsafe.Pointer(uintptr(ue.data1));}
  369. func (ue UserEvent) Data1Integer() int64 { return int64(ue.data1); }
  370. func (ue UserEvent) Data2Pointer() unsafe.Pointer { return unsafe.Pointer(uintptr(ue.data2));}
  371. func (ue UserEvent) Data2Integer() int64 { return int64(ue.data2); }
  372. func (ue UserEvent) Data3Pointer() unsafe.Pointer { return unsafe.Pointer(uintptr(ue.data3));}
  373. func (ue UserEvent) Data3Integer() int64 { return int64(ue.data3); }
  374. func (ue UserEvent) Data4Pointer() unsafe.Pointer { return unsafe.Pointer(uintptr(ue.data4));}
  375. func (ue UserEvent) Data4Integer() int64 { return int64(ue.data4); }
  376. func (ue UserEvent) SetData1Integer(v int64) { ue.data1 = C.intptr_t(v); }
  377. func (ue UserEvent) SetData2Integer(v int64) { ue.data2 = C.intptr_t(v); }
  378. func (ue UserEvent) SetData3Integer(v int64) { ue.data3 = C.intptr_t(v); }
  379. func (ue UserEvent) SetData4Integer(v int64) { ue.data4 = C.intptr_t(v); }
  380. /*
  381. The compiler accept this, but it's unlikely to work correctly.
  382. func (ue UserEvent) Data1() interface{} { return (interface{})(ue.Data1Pointer());}
  383. func (ue UserEvent) Data2() interface{} { return (interface{})(ue.Data2Pointer());}
  384. func (ue UserEvent) Data3() interface{} { return (interface{})(ue.Data3Pointer());}
  385. func (ue UserEvent) Data4() interface{} { return (interface{})(ue.Data4Pointer());}
  386. */
  387. // Event queues.
  388. type EventQueue struct {
  389. handle *C.ALLEGRO_EVENT_QUEUE
  390. }
  391. // Destroys the event queue.
  392. func (self *EventQueue) Destroy() {
  393. if self.handle != nil {
  394. C.al_destroy_event_queue(self.handle)
  395. }
  396. self.handle = nil
  397. }
  398. // Wraps an event queue, but does not set a finalizer.
  399. func wrapEventQueueRaw(handle *C.ALLEGRO_EVENT_QUEUE) *EventQueue {
  400. if handle == nil {
  401. return nil
  402. }
  403. return &EventQueue{handle}
  404. }
  405. func (eq * EventQueue) toC() (handle *C.ALLEGRO_EVENT_QUEUE) {
  406. return eq.handle
  407. }
  408. // Wraps an event queue and sets a finalizer that calls Destroy
  409. func wrapEventQueue(handle *C.ALLEGRO_EVENT_QUEUE) *EventQueue {
  410. result := wrapEventQueueRaw(handle)
  411. if result != nil {
  412. runtime.SetFinalizer(result, func(me *EventQueue) { me.Destroy() })
  413. }
  414. return result
  415. }
  416. // Create an event queue.
  417. func CreateEventQueue() *EventQueue {
  418. return wrapEventQueue(C.al_create_event_queue())
  419. }
  420. // Register an event source with self.
  421. func (self *EventQueue) RegisterEventSource(src *EventSource) {
  422. C.al_register_event_source(self.handle, src.toC())
  423. }
  424. // Unregister an event source with self.
  425. func (self *EventQueue) UnregisterEventSource(src *EventSource) {
  426. C.al_unregister_event_source(self.handle, src.toC())
  427. }
  428. // Returns true if the event queue self is empty, false if not.
  429. func (self *EventQueue) IsEmpty() bool {
  430. return bool(C.al_is_event_queue_empty(self.handle))
  431. }
  432. // Returns the next event from the event queue as well as a bool
  433. // to signify if an event was fetched sucessfully or not.
  434. func (self *EventQueue) GetNextEvent() (event *EventUnion, ok bool) {
  435. event = &EventUnion{}
  436. ok = bool(C.al_get_next_event(self.handle, event.toC()))
  437. return event, ok
  438. }
  439. // Peeks at the next event in the event queue and returns it as well as a bool
  440. // to signify if an event was fetched sucessfully or not.
  441. func (self *EventQueue) PeekNextEvent() (event *EventUnion, ok bool) {
  442. event = &EventUnion{}
  443. ok = bool(C.al_peek_next_event(self.handle, event.toC()))
  444. return event, ok
  445. }
  446. // Drops the next event from the event queue
  447. func (self *EventQueue) DropNextEvent() bool {
  448. return bool(C.al_drop_next_event(self.handle))
  449. }
  450. // Flushes the event queue
  451. func (self *EventQueue) Flush() {
  452. C.al_flush_event_queue(self.handle)
  453. }
  454. // Waits for the next event from the event queue
  455. func (self *EventQueue) WaitForEvent() (event *EventUnion) {
  456. event = &EventUnion{}
  457. C.al_wait_for_event(self.handle, event.toC())
  458. return event
  459. }
  460. // Waits for secs seconds the next event from the event queue
  461. func (self *EventQueue) WaitForEventTimed(secs float32) (event *EventUnion, ok bool) {
  462. event = &EventUnion{}
  463. ok = bool(C.al_wait_for_event_timed(self.handle, event.toC(), C.float(secs)))
  464. return event, ok
  465. }
  466. func (queue *EventQueue) WaitForEventUntil(timeout * Timeout) (event *EventUnion, ok bool) {
  467. event = &EventUnion{}
  468. ok = bool(C.al_wait_for_event_until(queue.toC(), event.toC(), timeout.toC()))
  469. return event, ok
  470. }