2014-09-29 03:22:23 +00:00
|
|
|
// 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 darwin
|
|
|
|
|
|
|
|
package app
|
|
|
|
|
2014-10-12 22:24:05 +00:00
|
|
|
// Simple on-screen app debugging for OS X. Not an officially supported
|
|
|
|
// development target for apps, as screens with mice are very different
|
|
|
|
// than screens with touch panels.
|
|
|
|
|
2014-09-29 03:22:23 +00:00
|
|
|
/*
|
|
|
|
#cgo CFLAGS: -x objective-c
|
|
|
|
#cgo LDFLAGS: -framework Cocoa -framework OpenGL -framework QuartzCore
|
|
|
|
#import <Cocoa/Cocoa.h>
|
2014-10-23 00:24:05 +00:00
|
|
|
#include <pthread.h>
|
2014-09-29 03:22:23 +00:00
|
|
|
|
|
|
|
void runApp(void);
|
2015-06-30 16:09:44 +00:00
|
|
|
void stopApp(void);
|
2015-05-28 10:08:52 +00:00
|
|
|
void makeCurrentContext(GLintptr);
|
2014-10-23 00:24:05 +00:00
|
|
|
uint64 threadID();
|
2014-09-29 03:22:23 +00:00
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
import (
|
2014-10-23 00:24:05 +00:00
|
|
|
"log"
|
2014-09-29 03:22:23 +00:00
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
|
2014-11-09 21:55:57 +00:00
|
|
|
"golang.org/x/mobile/event"
|
|
|
|
"golang.org/x/mobile/geom"
|
|
|
|
"golang.org/x/mobile/gl"
|
2014-09-29 03:22:23 +00:00
|
|
|
)
|
|
|
|
|
2014-10-23 00:24:05 +00:00
|
|
|
var initThreadID uint64
|
|
|
|
|
2014-09-29 03:22:23 +00:00
|
|
|
func init() {
|
2014-10-23 00:24:05 +00:00
|
|
|
// Lock the goroutine responsible for initialization to an OS thread.
|
2015-06-30 16:09:44 +00:00
|
|
|
// This means the goroutine running main (and calling runApp below)
|
|
|
|
// is locked to the OS thread that started the program. This is
|
2014-10-23 00:24:05 +00:00
|
|
|
// necessary for the correct delivery of Cocoa events to the process.
|
|
|
|
//
|
|
|
|
// A discussion on this topic:
|
|
|
|
// https://groups.google.com/forum/#!msg/golang-nuts/IiWZ2hUuLDA/SNKYYZBelsYJ
|
2014-09-29 03:22:23 +00:00
|
|
|
runtime.LockOSThread()
|
2014-10-23 00:24:05 +00:00
|
|
|
initThreadID = uint64(C.threadID())
|
2014-09-29 03:22:23 +00:00
|
|
|
}
|
|
|
|
|
2015-06-27 22:32:29 +00:00
|
|
|
func main(f func(App)) {
|
2014-10-23 00:24:05 +00:00
|
|
|
if tid := uint64(C.threadID()); tid != initThreadID {
|
2015-06-27 11:44:56 +00:00
|
|
|
log.Fatalf("app.Main called on thread %d, but app.init ran on %d", tid, initThreadID)
|
2014-10-23 00:24:05 +00:00
|
|
|
}
|
2015-06-27 11:44:56 +00:00
|
|
|
|
|
|
|
go func() {
|
2015-06-27 22:32:29 +00:00
|
|
|
f(app{})
|
2015-06-30 16:09:44 +00:00
|
|
|
C.stopApp()
|
2015-06-27 11:44:56 +00:00
|
|
|
// TODO(crawshaw): trigger runApp to return
|
|
|
|
}()
|
|
|
|
|
2014-09-29 03:22:23 +00:00
|
|
|
C.runApp()
|
|
|
|
}
|
|
|
|
|
2015-06-30 16:09:44 +00:00
|
|
|
// loop is the primary drawing loop.
|
|
|
|
//
|
|
|
|
// After Cocoa has captured the initial OS thread for processing Cocoa
|
|
|
|
// events in runApp, it starts loop on another goroutine. It is locked
|
|
|
|
// to an OS thread for its OpenGL context.
|
|
|
|
//
|
|
|
|
// Two Cocoa threads deliver draw signals to loop. The primary source of
|
|
|
|
// draw events is the CVDisplayLink timer, which is tied to the display
|
|
|
|
// vsync. Secondary draw events come from [NSView drawRect:] when the
|
|
|
|
// window is resized.
|
|
|
|
func loop(ctx C.GLintptr) {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
C.makeCurrentContext(ctx)
|
|
|
|
|
|
|
|
for range draw {
|
|
|
|
eventsIn <- event.Draw{}
|
|
|
|
loop1:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-gl.WorkAvailable:
|
|
|
|
gl.DoWork()
|
|
|
|
case <-endDraw:
|
|
|
|
C.CGLFlushDrawable(C.CGLGetCurrentContext())
|
|
|
|
break loop1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
drawDone <- struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
draw = make(chan struct{})
|
|
|
|
drawDone = make(chan struct{})
|
|
|
|
)
|
|
|
|
|
|
|
|
//export drawgl
|
|
|
|
func drawgl() {
|
|
|
|
draw <- struct{}{}
|
|
|
|
<-drawDone
|
|
|
|
}
|
|
|
|
|
|
|
|
//export startloop
|
|
|
|
func startloop(ctx C.GLintptr) {
|
|
|
|
go loop(ctx)
|
|
|
|
}
|
|
|
|
|
2015-06-27 11:44:56 +00:00
|
|
|
var windowHeight geom.Pt
|
|
|
|
|
2014-09-29 03:22:23 +00:00
|
|
|
//export setGeom
|
2015-06-27 11:44:56 +00:00
|
|
|
func setGeom(ppp float32, width, height int) {
|
|
|
|
pixelsPerPt = ppp
|
|
|
|
windowHeight = geom.Pt(float32(height) / pixelsPerPt)
|
|
|
|
eventsIn <- event.Config{
|
|
|
|
Width: geom.Pt(float32(width) / pixelsPerPt),
|
|
|
|
Height: windowHeight,
|
|
|
|
PixelsPerPt: pixelsPerPt,
|
2015-05-05 13:52:19 +00:00
|
|
|
}
|
2014-09-29 03:22:23 +00:00
|
|
|
}
|
|
|
|
|
2014-12-21 17:00:15 +00:00
|
|
|
var touchEvents struct {
|
2014-10-23 00:24:05 +00:00
|
|
|
sync.Mutex
|
|
|
|
pending []event.Touch
|
|
|
|
}
|
2014-09-29 03:22:23 +00:00
|
|
|
|
2015-06-30 23:57:23 +00:00
|
|
|
func sendTouch(c event.Change, x, y float32) {
|
2015-06-27 11:44:56 +00:00
|
|
|
eventsIn <- event.Touch{
|
2015-06-30 23:57:23 +00:00
|
|
|
ID: 0,
|
|
|
|
Change: c,
|
2014-09-29 03:22:23 +00:00
|
|
|
Loc: geom.Point{
|
2015-06-27 11:44:56 +00:00
|
|
|
X: geom.Pt(x / pixelsPerPt),
|
|
|
|
Y: windowHeight - geom.Pt(y/pixelsPerPt),
|
2014-09-29 03:22:23 +00:00
|
|
|
},
|
2015-06-27 11:44:56 +00:00
|
|
|
}
|
2014-09-29 03:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//export eventMouseDown
|
2015-06-30 23:57:23 +00:00
|
|
|
func eventMouseDown(x, y float32) { sendTouch(event.ChangeOn, x, y) }
|
2014-09-29 03:22:23 +00:00
|
|
|
|
2014-10-23 00:24:05 +00:00
|
|
|
//export eventMouseDragged
|
2015-06-30 23:57:23 +00:00
|
|
|
func eventMouseDragged(x, y float32) { sendTouch(event.ChangeNone, x, y) }
|
2014-09-29 03:22:23 +00:00
|
|
|
|
|
|
|
//export eventMouseEnd
|
2015-06-30 23:57:23 +00:00
|
|
|
func eventMouseEnd(x, y float32) { sendTouch(event.ChangeOff, x, y) }
|
2014-09-29 03:22:23 +00:00
|
|
|
|
2015-06-30 16:09:44 +00:00
|
|
|
//export lifecycleDead
|
|
|
|
func lifecycleDead() { sendLifecycle(event.LifecycleStageDead) }
|
2015-05-28 10:08:52 +00:00
|
|
|
|
2015-06-30 16:09:44 +00:00
|
|
|
//export lifecycleAlive
|
|
|
|
func lifecycleAlive() { sendLifecycle(event.LifecycleStageAlive) }
|
2014-09-29 03:22:23 +00:00
|
|
|
|
2015-06-30 16:09:44 +00:00
|
|
|
//export lifecycleVisible
|
|
|
|
func lifecycleVisible() { sendLifecycle(event.LifecycleStageVisible) }
|
2015-06-27 11:44:56 +00:00
|
|
|
|
2015-06-30 16:09:44 +00:00
|
|
|
//export lifecycleFocused
|
|
|
|
func lifecycleFocused() { sendLifecycle(event.LifecycleStageFocused) }
|