| 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 || darwin || windows |
| 6 | |
| 7 | package appscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/ |
| 8 | |
| 9 | import ( |
| 10 | "golang.org/x/mobile/event/lifecyclescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/lifecycle`/" |
| 11 | "golang.org/x/mobile/event/sizescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/size`/" |
| 12 | "golang.org/x/mobile/glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/" |
| 13 | _ "golang.org/x/mobile/internal/mobileinitscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/internal/mobileinit`/" |
| 14 | ) |
| 15 | |
| 16 | // Main is called by the main.main function to run the mobile application. |
| 17 | // |
| 18 | // It calls f on the App, in a separate goroutine, as some OS-specific |
| 19 | // libraries require being on 'the main thread'. |
| 20 | func Mainscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/Main().(f func(Appscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/App#)) { |
| 21 | mainscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/main().(f) |
| 22 | } |
| 23 | |
| 24 | // App is how a GUI mobile application interacts with the OS. |
| 25 | type Appscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/App# interface { |
| 26 | // Events returns the events channel. It carries events from the system to |
| 27 | // the app. The type of such events include: |
| 28 | // - lifecycle.Event |
| 29 | // - mouse.Event |
| 30 | // - paint.Event |
| 31 | // - size.Event |
| 32 | // - touch.Event |
| 33 | // from the golang.org/x/mobile/event/etc packages. Other packages may |
| 34 | // define other event types that are carried on this channel. |
| 35 | Eventsscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/App#Events.() <-chan interface{} |
| 36 | |
| 37 | // Send sends an event on the events channel. It does not block. |
| 38 | Sendscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/App#Send.(event interface{}) |
| 39 | |
| 40 | // Publish flushes any pending drawing commands, such as OpenGL calls, and |
| 41 | // swaps the back buffer to the screen. |
| 42 | Publishscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/App#Publish.() PublishResultscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/PublishResult# |
| 43 | |
| 44 | // TODO: replace filters (and the Events channel) with a NextEvent method? |
| 45 | |
| 46 | // Filter calls each registered event filter function in sequence. |
| 47 | Filterscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/App#Filter.(event interface{}) interface{} |
| 48 | |
| 49 | // RegisterFilter registers a event filter function to be called by Filter. The |
| 50 | // function can return a different event, or return nil to consume the event, |
| 51 | // but the function can also return its argument unchanged, where its purpose |
| 52 | // is to trigger a side effect rather than modify the event. |
| 53 | RegisterFilterscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/App#RegisterFilter.(f func(interface{}) interface{}) |
| 54 | } |
| 55 | |
| 56 | // PublishResult is the result of an App.Publish call. |
| 57 | type PublishResultscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/PublishResult# struct { |
| 58 | // BackBufferPreserved is whether the contents of the back buffer was |
| 59 | // preserved. If false, the contents are undefined. |
| 60 | BackBufferPreservedscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/PublishResult#BackBufferPreserved. bool |
| 61 | } |
| 62 | |
| 63 | var theAppscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/theApp. = &appscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#{ |
| 64 | eventsOutscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#eventsOut.: make(chan interface{}), |
| 65 | lifecycleStagescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#lifecycleStage.: lifecyclescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/lifecycle`/.StageDeadscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/lifecycle`/StageDead., |
| 66 | publishscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#publish.: make(chan struct{}), |
| 67 | publishResultscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#publishResult.: make(chan PublishResultscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/PublishResult#), |
| 68 | } |
| 69 | |
| 70 | func initscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/init().() { |
| 71 | theAppscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/theApp..eventsInscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#eventsIn. = pumpscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/pump().(theAppscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/theApp..eventsOutscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#eventsOut.) |
| 72 | theAppscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/theApp..glctxscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#glctx., theAppscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/theApp..workerscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#worker. = glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.NewContextscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/NewContext().() |
| 73 | } |
| 74 | |
| 75 | func (a *appscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#) sendLifecyclescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#sendLifecycle().(to lifecyclescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/lifecycle`/.Stagescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/lifecycle`/Stage#) { |
| 76 | if a.lifecycleStagescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#lifecycleStage. == to { |
| 77 | return |
| 78 | } |
| 79 | a.eventsInscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#eventsIn. <- lifecyclescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/lifecycle`/.Eventscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/lifecycle`/Event#{ |
| 80 | Fromscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/lifecycle`/Event#From.: a.lifecycleStagescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#lifecycleStage., |
| 81 | Toscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/lifecycle`/Event#To.: to, |
| 82 | DrawContextscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/lifecycle`/Event#DrawContext.: a.glctxscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#glctx., |
| 83 | } |
| 84 | a.lifecycleStagescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#lifecycleStage. = to |
| 85 | } |
| 86 | |
| 87 | type appscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app# struct { |
| 88 | filtersscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#filters. []func(interface{}) interface{} |
| 89 | |
| 90 | eventsOutscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#eventsOut. chan interface{} |
| 91 | eventsInscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#eventsIn. chan interface{} |
| 92 | lifecycleStagescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#lifecycleStage. lifecyclescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/lifecycle`/.Stagescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/lifecycle`/Stage# |
| 93 | publishscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#publish. chan struct{} |
| 94 | publishResultscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#publishResult. chan PublishResultscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/PublishResult# |
| 95 | |
| 96 | glctxscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#glctx. glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.Contextscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context# |
| 97 | workerscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#worker. glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.Workerscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Worker# |
| 98 | } |
| 99 | |
| 100 | func (a *appscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#) Eventsscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#Events().() <-chan interface{} { |
| 101 | return a.eventsOutscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#eventsOut. |
| 102 | } |
| 103 | |
| 104 | func (a *appscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#) Sendscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#Send().(event interface{}) { |
| 105 | a.eventsInscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#eventsIn. <- event |
| 106 | } |
| 107 | |
| 108 | func (a *appscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#) Publishscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#Publish().() PublishResultscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/PublishResult# { |
| 109 | // gl.Flush is a lightweight (on modern GL drivers) blocking call |
| 110 | // that ensures all GL functions pending in the gl package have |
| 111 | // been passed onto the GL driver before the app package attempts |
| 112 | // to swap the screen buffer. |
| 113 | // |
| 114 | // This enforces that the final receive (for this paint cycle) on |
| 115 | // gl.WorkAvailable happens before the send on endPaint. |
| 116 | a.glctxscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#glctx..Flushscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#Flush.() |
| 117 | a.publishscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#publish. <- struct{}{} |
| 118 | return <-a.publishResultscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#publishResult. |
| 119 | } |
| 120 | |
| 121 | func (a *appscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#) Filterscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#Filter().(event interface{}) interface{} { |
| 122 | for _, f := range a.filtersscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#filters. { |
| 123 | event = f(event) |
| 124 | } |
| 125 | return event |
| 126 | } |
| 127 | |
| 128 | func (a *appscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#) RegisterFilterscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#RegisterFilter().(f func(interface{}) interface{}) { |
| 129 | a.filtersscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#filters. = append(a.filtersscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#filters., f) |
| 130 | } |
| 131 | |
| 132 | type stopPumpingscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/stopPumping# struct{} |
| 133 | |
| 134 | // pump returns a channel src such that sending on src will eventually send on |
| 135 | // dst, in order, but that src will always be ready to send/receive soon, even |
| 136 | // if dst currently isn't. It is effectively an infinitely buffered channel. |
| 137 | // |
| 138 | // In particular, goroutine A sending on src will not deadlock even if goroutine |
| 139 | // B that's responsible for receiving on dst is currently blocked trying to |
| 140 | // send to A on a separate channel. |
| 141 | // |
| 142 | // Send a stopPumping on the src channel to close the dst channel after all queued |
| 143 | // events are sent on dst. After that, other goroutines can still send to src, |
| 144 | // so that such sends won't block forever, but such events will be ignored. |
| 145 | func pumpscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/pump().(dst chan interface{}) (src chan interface{}) { |
| 146 | src = make(chan interface{}) |
| 147 | go func() { |
| 148 | // initialSize is the initial size of the circular buffer. It must be a |
| 149 | // power of 2. |
| 150 | const initialSize = 16 |
| 151 | i, j, buf, mask := 0, 0, make([]interface{}, initialSize), initialSize-1 |
| 152 | |
| 153 | srcActive := true |
| 154 | for { |
| 155 | maybeDst := dst |
| 156 | if i == j { |
| 157 | maybeDst = nil |
| 158 | } |
| 159 | if maybeDst == nil && !srcActive { |
| 160 | // Pump is stopped and empty. |
| 161 | break |
| 162 | } |
| 163 | |
| 164 | select { |
| 165 | case maybeDst <- buf[i&mask]: |
| 166 | buf[i&mask] = nil |
| 167 | i++ |
| 168 | |
| 169 | case e := <-src: |
| 170 | if _, ok := e.(stopPumpingscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/stopPumping#); ok { |
| 171 | srcActive = false |
| 172 | continue |
| 173 | } |
| 174 | |
| 175 | if !srcActive { |
| 176 | continue |
| 177 | } |
| 178 | |
| 179 | // Allocate a bigger buffer if necessary. |
| 180 | if i+len(buf) == j { |
| 181 | b := make([]interface{}, 2*len(buf)) |
| 182 | n := copy(b, buf[j&mask:]) |
| 183 | copy(b[n:], buf[:j&mask]) |
| 184 | i, j = 0, len(buf) |
| 185 | buf, mask = b, len(b)-1 |
| 186 | } |
| 187 | |
| 188 | buf[j&mask] = e |
| 189 | j++ |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | close(dst) |
| 194 | // Block forever. |
| 195 | for range src { |
| 196 | } |
| 197 | }() |
| 198 | return src |
| 199 | } |
| 200 | |
| 201 | // TODO: do this for all build targets, not just linux (x11 and Android)? If |
| 202 | // so, should package gl instead of this package call RegisterFilter?? |
| 203 | // |
| 204 | // TODO: does Android need this?? It seems to work without it (Nexus 7, |
| 205 | // KitKat). If only x11 needs this, should we move this to x11.go?? |
| 206 | func (a *appscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#) registerGLViewportFilterscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#registerGLViewportFilter().() { |
| 207 | a.RegisterFilterscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#RegisterFilter().(func(e interface{}) interface{} { |
| 208 | if e, ok := e.(sizescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/size`/.Eventscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/size`/Event#); ok { |
| 209 | a.glctxscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/app#glctx..Viewportscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#Viewport.(0, 0, e.WidthPxscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/size`/Event#WidthPx., e.HeightPxscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/size`/Event#HeightPx.) |
| 210 | } |
| 211 | return e |
| 212 | }) |
| 213 | } |