2
0
mirror of synced 2025-02-23 14:58:12 +00:00
mobile/app/x11.go
David Crawshaw 5cddc1460e app: introduce Config and start registration
Config provides a way to concurrently access Width and Height.

Register provides a way for packages to run code on app state change
without plumbing changes all the way to the process main function.
This is motivated by gl/glutil.Image which needs to rebuild its
textures on start/stop and can be deeply nested.
(See golang.org/cl/9707 for the followup.)

Tested manually on android and darwin/amd64. Doing this kind makes it
clear any CL modifying this code needs a lot of manual testing right
now, so some kind of trybot support is something I'm going to
prioritise.

Fixes golang/go#10686
Fixes golang/go#10461
Fixes golang/go#10442
Fixes golang/go#10226
Updates golang/go#10327

Change-Id: I2882ebf3995b6ed857cda823e94fbb17c54b43a8
Reviewed-on: https://go-review.googlesource.com/9708
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-05-06 17:32:53 +00:00

109 lines
2.1 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 runApp(void);
*/
import "C"
import (
"runtime"
"sync"
"golang.org/x/mobile/event"
"golang.org/x/mobile/geom"
)
var callbacks []Callbacks
func run(cbs []Callbacks) {
runtime.LockOSThread()
callbacks = cbs
C.runApp()
}
//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)
}
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.Height - 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()
}
}
}
//export onStart
func onStart() {
stateInit(callbacks)
}
//export onStop
func onStop() {
stateStop(callbacks)
}