- Fix hangs on linux/x11. x11-related code remains outdated since 068a51c19, which seperates GL calls to dedicated thread. Update x11 code to work with GL thread. - Fix bug on coordinate system. x11 has same coordinate system with android, (0,0) on top-left. Change-Id: I0b3ab97fd4842b1c9f730e1c90a5840f540fcb7a Reviewed-on: https://go-review.googlesource.com/10623 Reviewed-by: David Crawshaw <crawshaw@golang.org>
117 lines
2.2 KiB
Go
117 lines
2.2 KiB
Go
// Copyright 2014 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// +build linux,!android
|
|
|
|
package app
|
|
|
|
/*
|
|
Simple on-screen app debugging for X11. Not an officially supported
|
|
development target for apps, as screens with mice are very different
|
|
than screens with touch panels.
|
|
|
|
On Ubuntu 14.04 'Trusty', you may have to install these libraries:
|
|
sudo apt-get install libegl1-mesa-dev libgles2-mesa-dev libx11-dev
|
|
*/
|
|
|
|
/*
|
|
#cgo LDFLAGS: -lEGL -lGLESv2 -lX11
|
|
|
|
void createWindow(void);
|
|
void eventLoop(void);
|
|
void swapBuffers(void);
|
|
*/
|
|
import "C"
|
|
import (
|
|
"runtime"
|
|
"sync"
|
|
|
|
"golang.org/x/mobile/event"
|
|
"golang.org/x/mobile/geom"
|
|
"golang.org/x/mobile/gl"
|
|
)
|
|
|
|
func run(cbs []Callbacks) {
|
|
runtime.LockOSThread()
|
|
callbacks = cbs
|
|
|
|
go gl.Start(func() {
|
|
C.createWindow()
|
|
})
|
|
|
|
close(mainCalled)
|
|
stateStart(callbacks)
|
|
C.eventLoop()
|
|
}
|
|
|
|
//export onResize
|
|
func onResize(w, h int) {
|
|
// TODO(nigeltao): don't assume 72 DPI. DisplayWidth / DisplayWidthMM
|
|
// is probably the best place to start looking.
|
|
if geom.PixelsPerPt == 0 {
|
|
geom.PixelsPerPt = 1
|
|
}
|
|
configAlt.Width = geom.Pt(w)
|
|
configAlt.Height = geom.Pt(h)
|
|
configSwap(callbacks)
|
|
gl.Viewport(0, 0, w, h)
|
|
}
|
|
|
|
var touchEvents struct {
|
|
sync.Mutex
|
|
pending []event.Touch
|
|
}
|
|
|
|
func sendTouch(ty event.TouchType, x, y float32) {
|
|
touchEvents.Lock()
|
|
touchEvents.pending = append(touchEvents.pending, event.Touch{
|
|
ID: 0,
|
|
Type: ty,
|
|
Loc: geom.Point{
|
|
X: geom.Pt(x / geom.PixelsPerPt),
|
|
Y: geom.Pt(y / geom.PixelsPerPt),
|
|
},
|
|
})
|
|
touchEvents.Unlock()
|
|
}
|
|
|
|
//export onTouchStart
|
|
func onTouchStart(x, y float32) { sendTouch(event.TouchStart, x, y) }
|
|
|
|
//export onTouchMove
|
|
func onTouchMove(x, y float32) { sendTouch(event.TouchMove, x, y) }
|
|
|
|
//export onTouchEnd
|
|
func onTouchEnd(x, y float32) { sendTouch(event.TouchEnd, x, y) }
|
|
|
|
//export onDraw
|
|
func onDraw() {
|
|
touchEvents.Lock()
|
|
pending := touchEvents.pending
|
|
touchEvents.pending = nil
|
|
touchEvents.Unlock()
|
|
for _, cb := range callbacks {
|
|
if cb.Touch != nil {
|
|
for _, e := range pending {
|
|
cb.Touch(e)
|
|
}
|
|
}
|
|
}
|
|
|
|
for _, cb := range callbacks {
|
|
if cb.Draw != nil {
|
|
cb.Draw()
|
|
}
|
|
}
|
|
|
|
gl.Do(func() {
|
|
C.swapBuffers()
|
|
})
|
|
}
|
|
|
|
//export onStop
|
|
func onStop() {
|
|
stateStop(callbacks)
|
|
}
|