| 1 |
// Copyright 2014 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
//go:build linux && !android |
| 6 |
|
| 7 |
package app |
| 8 |
|
| 9 |
/* |
| 10 |
Simple on-screen app debugging for X11. Not an officially supported |
| 11 |
development target for apps, as screens with mice are very different |
| 12 |
than screens with touch panels. |
| 13 |
*/ |
| 14 |
|
| 15 |
/* |
| 16 |
#cgo LDFLAGS: -lEGL -lGLESv2 -lX11 |
| 17 |
|
| 18 |
void createWindow(void); |
| 19 |
void processEvents(void); |
| 20 |
void swapBuffers(void); |
| 21 |
*/ |
| 22 |
import "C" |
| 23 |
import ( |
| 24 |
"runtime" |
| 25 |
"time" |
| 26 |
|
| 27 |
"golang.org/x/mobile/event/lifecycle" |
| 28 |
"golang.org/x/mobile/event/paint" |
| 29 |
"golang.org/x/mobile/event/size" |
| 30 |
"golang.org/x/mobile/event/touch" |
| 31 |
"golang.org/x/mobile/geom" |
| 32 |
) |
| 33 |
|
| 34 |
func init() { |
| 35 |
theApp.registerGLViewportFilter() |
| 36 |
} |
| 37 |
|
| 38 |
func main(f func(App)) { |
| 39 |
runtime.LockOSThread() |
| 40 |
|
| 41 |
workAvailable := theApp.worker.WorkAvailable() |
| 42 |
|
| 43 |
C.createWindow() |
| 44 |
|
| 45 |
// TODO: send lifecycle events when e.g. the X11 window is iconified or moved off-screen. |
| 46 |
theApp.sendLifecycle(lifecycle.StageFocused) |
| 47 |
|
| 48 |
// TODO: translate X11 expose events to shiny paint events, instead of |
| 49 |
// sending this synthetic paint event as a hack. |
| 50 |
theApp.eventsIn <- paint.Event{} |
| 51 |
|
| 52 |
donec := make(chan struct{}) |
| 53 |
go func() { |
| 54 |
// close the donec channel in a defer statement |
| 55 |
// so that we could still be able to return even |
| 56 |
// if f panics. |
| 57 |
defer close(donec) |
| 58 |
|
| 59 |
f(theApp) |
| 60 |
}() |
| 61 |
|
| 62 |
// TODO: can we get the actual vsync signal? |
| 63 |
ticker := time.NewTicker(time.Second / 60) |
| 64 |
defer ticker.Stop() |
| 65 |
var tc <-chan time.Time |
| 66 |
|
| 67 |
for { |
| 68 |
select { |
| 69 |
case <-donec: |
| 70 |
return |
| 71 |
case <-workAvailable: |
| 72 |
theApp.worker.DoWork() |
| 73 |
case <-theApp.publish: |
| 74 |
C.swapBuffers() |
| 75 |
tc = ticker.C |
| 76 |
case <-tc: |
| 77 |
tc = nil |
| 78 |
theApp.publishResult <- PublishResult{} |
| 79 |
} |
| 80 |
C.processEvents() |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
//export onResize |
| 85 |
func onResize(w, h int) { |
| 86 |
// TODO(nigeltao): don't assume 72 DPI. DisplayWidth and DisplayWidthMM |
| 87 |
// is probably the best place to start looking. |
| 88 |
pixelsPerPt := float32(1) |
| 89 |
theApp.eventsIn <- size.Event{ |
| 90 |
WidthPx: w, |
| 91 |
HeightPx: h, |
| 92 |
WidthPt: geom.Pt(w), |
| 93 |
HeightPt: geom.Pt(h), |
| 94 |
PixelsPerPt: pixelsPerPt, |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
func sendTouch(t touch.Type, x, y float32) { |
| 99 |
theApp.eventsIn <- touch.Event{ |
| 100 |
X: x, |
| 101 |
Y: y, |
| 102 |
Sequence: 0, // TODO: button?? |
| 103 |
Type: t, |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
//export onTouchBegin |
| 108 |
func onTouchBegin(x, y float32) { sendTouch(touch.TypeBegin, x, y) } |
| 109 |
|
| 110 |
//export onTouchMove |
| 111 |
func onTouchMove(x, y float32) { sendTouch(touch.TypeMove, x, y) } |
| 112 |
|
| 113 |
//export onTouchEnd |
| 114 |
func onTouchEnd(x, y float32) { sendTouch(touch.TypeEnd, x, y) } |
| 115 |
|
| 116 |
var stopped bool |
| 117 |
|
| 118 |
//export onStop |
| 119 |
func onStop() { |
| 120 |
if stopped { |
| 121 |
return |
| 122 |
} |
| 123 |
stopped = true |
| 124 |
theApp.sendLifecycle(lifecycle.StageDead) |
| 125 |
theApp.eventsIn <- stopPumping{} |
| 126 |
} |