| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package gldriver |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
import "C" |
| 30 |
import ( |
| 31 |
"errors" |
| 32 |
"runtime" |
| 33 |
"time" |
| 34 |
"unsafe" |
| 35 |
|
| 36 |
"golang.org/x/exp/shiny/driver/internal/x11key" |
| 37 |
"golang.org/x/exp/shiny/screen" |
| 38 |
"golang.org/x/mobile/event/key" |
| 39 |
"golang.org/x/mobile/event/mouse" |
| 40 |
"golang.org/x/mobile/event/paint" |
| 41 |
"golang.org/x/mobile/event/size" |
| 42 |
"golang.org/x/mobile/geom" |
| 43 |
"golang.org/x/mobile/gl" |
| 44 |
) |
| 45 |
|
| 46 |
const useLifecycler = true |
| 47 |
|
| 48 |
const handleSizeEventsAtChannelReceive = true |
| 49 |
|
| 50 |
var theKeysyms x11key.KeysymTable |
| 51 |
|
| 52 |
func init() { |
| 53 |
|
| 54 |
|
| 55 |
runtime.LockOSThread() |
| 56 |
} |
| 57 |
|
| 58 |
func newWindow(opts *screen.NewWindowOptions) (uintptr, error) { |
| 59 |
width, height := optsSize(opts) |
| 60 |
|
| 61 |
title := opts.GetTitle() |
| 62 |
ctitle := C.CString(title) |
| 63 |
defer C.free(unsafe.Pointer(ctitle)) |
| 64 |
|
| 65 |
retc := make(chan uintptr) |
| 66 |
uic <- uiClosure{ |
| 67 |
f: func() uintptr { |
| 68 |
return uintptr(C.doNewWindow(C.int(width), C.int(height), ctitle, C.int(len(title)))) |
| 69 |
}, |
| 70 |
retc: retc, |
| 71 |
} |
| 72 |
return <-retc, nil |
| 73 |
} |
| 74 |
|
| 75 |
func initWindow(w *windowImpl) { |
| 76 |
w.glctx, w.worker = glctx, worker |
| 77 |
} |
| 78 |
|
| 79 |
func showWindow(w *windowImpl) { |
| 80 |
retc := make(chan uintptr) |
| 81 |
uic <- uiClosure{ |
| 82 |
f: func() uintptr { |
| 83 |
return uintptr(C.doShowWindow(C.uintptr_t(w.id))) |
| 84 |
}, |
| 85 |
retc: retc, |
| 86 |
} |
| 87 |
w.ctx = <-retc |
| 88 |
go drawLoop(w) |
| 89 |
} |
| 90 |
|
| 91 |
func closeWindow(id uintptr) { |
| 92 |
uic <- uiClosure{ |
| 93 |
f: func() uintptr { |
| 94 |
C.doCloseWindow(C.uintptr_t(id)) |
| 95 |
return 0 |
| 96 |
}, |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
func drawLoop(w *windowImpl) { |
| 101 |
glcontextc <- w.ctx.(uintptr) |
| 102 |
go func() { |
| 103 |
for range w.publish { |
| 104 |
publishc <- w |
| 105 |
} |
| 106 |
}() |
| 107 |
} |
| 108 |
|
| 109 |
var ( |
| 110 |
glcontextc = make(chan uintptr) |
| 111 |
publishc = make(chan *windowImpl) |
| 112 |
uic = make(chan uiClosure) |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
glctx gl.Context |
| 122 |
worker gl.Worker |
| 123 |
) |
| 124 |
|
| 125 |
|
| 126 |
type uiClosure struct { |
| 127 |
f func() uintptr |
| 128 |
retc chan uintptr |
| 129 |
} |
| 130 |
|
| 131 |
func main(f func(screen.Screen)) error { |
| 132 |
if gl.Version() == "GL_ES_2_0" { |
| 133 |
return errors.New("gldriver: ES 3 required on X11") |
| 134 |
} |
| 135 |
C.startDriver() |
| 136 |
glctx, worker = gl.NewContext() |
| 137 |
|
| 138 |
closec := make(chan struct{}) |
| 139 |
go func() { |
| 140 |
f(theScreen) |
| 141 |
close(closec) |
| 142 |
}() |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
heartbeat := time.NewTicker(time.Second / 60) |
| 152 |
workAvailable := worker.WorkAvailable() |
| 153 |
|
| 154 |
for { |
| 155 |
select { |
| 156 |
case <-closec: |
| 157 |
return nil |
| 158 |
case ctx := <-glcontextc: |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
C.makeCurrent(C.uintptr_t(ctx)) |
| 166 |
case w := <-publishc: |
| 167 |
C.swapBuffers(C.uintptr_t(w.ctx.(uintptr))) |
| 168 |
w.publishDone <- screen.PublishResult{} |
| 169 |
case req := <-uic: |
| 170 |
ret := req.f() |
| 171 |
if req.retc != nil { |
| 172 |
req.retc <- ret |
| 173 |
} |
| 174 |
case <-heartbeat.C: |
| 175 |
C.processEvents() |
| 176 |
case <-workAvailable: |
| 177 |
worker.DoWork() |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
func onExpose(id uintptr) { |
| 184 |
theScreen.mu.Lock() |
| 185 |
w := theScreen.windows[id] |
| 186 |
theScreen.mu.Unlock() |
| 187 |
|
| 188 |
if w == nil { |
| 189 |
return |
| 190 |
} |
| 191 |
|
| 192 |
w.Send(paint.Event{External: true}) |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
func onKeysym(k, unshifted, shifted uint32) { |
| 197 |
theKeysyms.Table[k][0] = unshifted |
| 198 |
theKeysyms.Table[k][1] = shifted |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
func onKey(id uintptr, state uint16, detail, dir uint8) { |
| 203 |
theScreen.mu.Lock() |
| 204 |
w := theScreen.windows[id] |
| 205 |
theScreen.mu.Unlock() |
| 206 |
|
| 207 |
if w == nil { |
| 208 |
return |
| 209 |
} |
| 210 |
|
| 211 |
r, c := theKeysyms.Lookup(detail, state) |
| 212 |
w.Send(key.Event{ |
| 213 |
Rune: r, |
| 214 |
Code: c, |
| 215 |
Modifiers: x11key.KeyModifiers(state), |
| 216 |
Direction: key.Direction(dir), |
| 217 |
}) |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
func onMouse(id uintptr, x, y int32, state uint16, button, dir uint8) { |
| 222 |
theScreen.mu.Lock() |
| 223 |
w := theScreen.windows[id] |
| 224 |
theScreen.mu.Unlock() |
| 225 |
|
| 226 |
if w == nil { |
| 227 |
return |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
btn := mouse.Button(button) |
| 233 |
switch btn { |
| 234 |
case 4: |
| 235 |
btn = mouse.ButtonWheelUp |
| 236 |
case 5: |
| 237 |
btn = mouse.ButtonWheelDown |
| 238 |
case 6: |
| 239 |
btn = mouse.ButtonWheelLeft |
| 240 |
case 7: |
| 241 |
btn = mouse.ButtonWheelRight |
| 242 |
} |
| 243 |
if btn.IsWheel() { |
| 244 |
if dir != uint8(mouse.DirPress) { |
| 245 |
return |
| 246 |
} |
| 247 |
dir = uint8(mouse.DirStep) |
| 248 |
} |
| 249 |
w.Send(mouse.Event{ |
| 250 |
X: float32(x), |
| 251 |
Y: float32(y), |
| 252 |
Button: btn, |
| 253 |
Modifiers: x11key.KeyModifiers(state), |
| 254 |
Direction: mouse.Direction(dir), |
| 255 |
}) |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
func onFocus(id uintptr, focused bool) { |
| 260 |
theScreen.mu.Lock() |
| 261 |
w := theScreen.windows[id] |
| 262 |
theScreen.mu.Unlock() |
| 263 |
|
| 264 |
if w == nil { |
| 265 |
return |
| 266 |
} |
| 267 |
|
| 268 |
w.lifecycler.SetFocused(focused) |
| 269 |
w.lifecycler.SendEvent(w, w.glctx) |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
func onConfigure(id uintptr, x, y, width, height, displayWidth, displayWidthMM int32) { |
| 274 |
theScreen.mu.Lock() |
| 275 |
w := theScreen.windows[id] |
| 276 |
theScreen.mu.Unlock() |
| 277 |
|
| 278 |
if w == nil { |
| 279 |
return |
| 280 |
} |
| 281 |
|
| 282 |
w.lifecycler.SetVisible(x+width > 0 && y+height > 0) |
| 283 |
w.lifecycler.SendEvent(w, w.glctx) |
| 284 |
|
| 285 |
const ( |
| 286 |
mmPerInch = 25.4 |
| 287 |
ptPerInch = 72 |
| 288 |
) |
| 289 |
pixelsPerMM := float32(displayWidth) / float32(displayWidthMM) |
| 290 |
w.Send(size.Event{ |
| 291 |
WidthPx: int(width), |
| 292 |
HeightPx: int(height), |
| 293 |
WidthPt: geom.Pt(width), |
| 294 |
HeightPt: geom.Pt(height), |
| 295 |
PixelsPerPt: pixelsPerMM * mmPerInch / ptPerInch, |
| 296 |
}) |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
func onDeleteWindow(id uintptr) { |
| 301 |
theScreen.mu.Lock() |
| 302 |
w := theScreen.windows[id] |
| 303 |
theScreen.mu.Unlock() |
| 304 |
|
| 305 |
if w == nil { |
| 306 |
return |
| 307 |
} |
| 308 |
|
| 309 |
w.lifecycler.SetDead(true) |
| 310 |
w.lifecycler.SendEvent(w, w.glctx) |
| 311 |
} |
| 312 |
|
| 313 |
func surfaceCreate() error { |
| 314 |
if C.surfaceCreate() == 0 { |
| 315 |
return errors.New("gldriver: surface creation failed") |
| 316 |
} |
| 317 |
return nil |
| 318 |
} |