/
events.go
207 lines (195 loc) · 4.63 KB
/
events.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package allegro
/*
#cgo pkg-config: allegro-5.0
#include <allegro5/allegro.h>
#include "./event_getter.h"
*/
import "C"
import "sync"
type EventSource struct {
// Send anything on this channel to stop any GetEvents calls on this source
stopped chan bool
// The actual event source
src *C.ALLEGRO_EVENT_SOURCE
}
func createEventSource(src *C.ALLEGRO_EVENT_SOURCE) *EventSource {
var es EventSource
es.stopped = make(chan bool)
es.src = src
return &es
}
func (es *EventSource) StopGetEvents() {
es.stopped <- true
}
func GetEvents(sources []*EventSource) chan interface{} {
ch := make(chan interface{}, 10)
sourcesStopped := make(chan bool)
for _, evSrc := range sources {
go func() {
<-evSrc.stopped
sourcesStopped <- true
}()
}
go func() {
var queue *C.ALLEGRO_EVENT_QUEUE
queue = C.al_create_event_queue()
defer C.al_destroy_event_queue(queue)
for _, src := range sources {
ptr := (*C.ALLEGRO_EVENT_SOURCE)(src.src)
C.al_register_event_source(queue, ptr)
}
stopped := false
for !stopped {
var gotEv sync.Mutex
var al_event C.ALLEGRO_EVENT
gotEv.Lock()
go func() {
C.al_wait_for_event(queue, &al_event)
gotEv.Unlock()
}()
go func() {
<-sourcesStopped
stopped = true
gotEv.Unlock()
}()
gotEv.Lock()
ev := toEv(al_event)
if ev != nil {
ch <- ev
}
}
}()
return ch
}
func toEv(ev C.ALLEGRO_EVENT) interface{} {
any := C.get_any(ev)
src := createEventSource(any.source)
ts := float64(any.timestamp)
joystick := C.get_joystick(ev)
mouse := C.get_mouse(ev)
display := C.get_display(ev)
keyboard := C.get_keyboard(ev)
timer := C.get_timer(ev)
switch C.get_type(ev) {
case C.ALLEGRO_EVENT_JOYSTICK_AXIS:
return JoystickAxisEvent{
src, ts,
(*Joystick)(joystick.id),
int(joystick.stick),
int(joystick.axis),
float32(joystick.pos)}
case C.ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN:
return JoystickButtonDownEvent{
src, ts,
(*Joystick)(joystick.id),
int(joystick.button)}
case C.ALLEGRO_EVENT_JOYSTICK_BUTTON_UP:
return JoystickButtonUpEvent{
src, ts,
(*Joystick)(joystick.id),
int(joystick.button)}
case C.ALLEGRO_EVENT_JOYSTICK_CONFIGURATION:
return JoystickConfigurationEvent{src, ts}
case C.ALLEGRO_EVENT_KEY_DOWN:
return KeyDownEvent{
src, ts,
int(keyboard.keycode),
(*Display)(keyboard.display)}
case C.ALLEGRO_EVENT_KEY_UP:
return KeyUpEvent{
src, ts,
int(keyboard.keycode),
(*Display)(keyboard.display)}
case C.ALLEGRO_EVENT_KEY_CHAR:
return KeyCharEvent{
src, ts,
int(keyboard.keycode),
int(keyboard.unichar),
int(keyboard.modifiers),
bool(keyboard.repeat),
(*Display)(keyboard.display)}
case C.ALLEGRO_EVENT_MOUSE_AXES:
return MouseAxesEvent{
src, ts,
int(mouse.x),
int(mouse.y),
int(mouse.z),
int(mouse.w),
int(mouse.dx),
int(mouse.dy),
int(mouse.dz),
int(mouse.dw),
(*Display)(mouse.display)}
case C.ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
return MouseButtonDown{
src, ts,
int(mouse.x),
int(mouse.y),
int(mouse.z),
int(mouse.w),
int(mouse.button),
(*Display)(mouse.display)}
case C.ALLEGRO_EVENT_MOUSE_BUTTON_UP:
return MouseButtonUp{
src, ts,
int(mouse.x),
int(mouse.y),
int(mouse.z),
int(mouse.w),
int(mouse.button),
(*Display)(mouse.display)}
case C.ALLEGRO_EVENT_MOUSE_WARPED:
return MouseWarpEvent{
src, ts,
int(mouse.x),
int(mouse.y),
int(mouse.z),
int(mouse.w),
int(mouse.dx),
int(mouse.dy),
int(mouse.dz),
int(mouse.dw),
(*Display)(mouse.display)}
case C.ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY:
return MouseEnterDisplayEvent{
src, ts,
int(mouse.x), int(mouse.y),
int(mouse.z), int(mouse.w),
(*Display)(mouse.display)}
case C.ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY:
return MouseLeaveDisplayEvent{
src, ts,
int(mouse.x), int(mouse.y),
int(mouse.z), int(mouse.w),
(*Display)(mouse.display)}
case C.ALLEGRO_EVENT_TIMER:
return TimerEvent{
src, ts,
(*Timer)(timer.source),
int64(timer.count)}
case C.ALLEGRO_EVENT_DISPLAY_EXPOSE:
return DisplayExposeEvent{
src, ts,
(*Display)(display.source),
int(display.x), int(display.y),
int(display.width), int(display.height)}
case C.ALLEGRO_EVENT_DISPLAY_CLOSE:
return DisplayCloseEvent{
src, ts,
(*Display)(display.source)}
case C.ALLEGRO_EVENT_DISPLAY_SWITCH_OUT:
return DisplaySwitchOutEvent{
src, ts,
(*Display)(display.source)}
case C.ALLEGRO_EVENT_DISPLAY_SWITCH_IN:
return DisplaySwitchInEvent{
src, ts,
(*Display)(display.source)}
case C.ALLEGRO_EVENT_DISPLAY_ORIENTATION:
return DisplayOrientationEvent{
src, ts,
(*Display)(display.source),
int(display.orientation)}
}
return nil
}