android.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. //go:build android
  5. // +build android
  6. /*
  7. Android Apps are built with -buildmode=c-shared. They are loaded by a
  8. running Java process.
  9. Before any entry point is reached, a global constructor initializes the
  10. Go runtime, calling all Go init functions. All cgo calls will block
  11. until this is complete. Next JNI_OnLoad is called. When that is
  12. complete, one of two entry points is called.
  13. All-Go apps built using NativeActivity enter at ANativeActivity_onCreate.
  14. Go libraries (for example, those built with gomobile bind) do not use
  15. the app package initialization.
  16. */
  17. package app
  18. /*
  19. #cgo LDFLAGS: -landroid -llog -lEGL -lGLESv2
  20. #include <android/configuration.h>
  21. #include <android/input.h>
  22. #include <android/keycodes.h>
  23. #include <android/looper.h>
  24. #include <android/native_activity.h>
  25. #include <android/native_window.h>
  26. #include <EGL/egl.h>
  27. #include <jni.h>
  28. #include <pthread.h>
  29. #include <stdlib.h>
  30. #include <stdbool.h>
  31. extern EGLDisplay display;
  32. extern EGLSurface surface;
  33. char* createEGLSurface(ANativeWindow* window);
  34. char* destroyEGLSurface();
  35. int32_t getKeyRune(JNIEnv* env, AInputEvent* e);
  36. void showKeyboard(JNIEnv* env, int keyboardType);
  37. void hideKeyboard(JNIEnv* env);
  38. void showFileOpen(JNIEnv* env, char* mimes);
  39. void showFileSave(JNIEnv* env, char* mimes, char* filename);
  40. void Java_org_golang_app_GoNativeActivity_filePickerReturned(JNIEnv *env, jclass clazz, jstring str);
  41. */
  42. import "C"
  43. import (
  44. "fmt"
  45. "log"
  46. "mime"
  47. "os"
  48. "runtime"
  49. "runtime/debug"
  50. "strings"
  51. "time"
  52. "unsafe"
  53. "fyne.io/fyne/v2/internal/driver/mobile/app/callfn"
  54. "fyne.io/fyne/v2/internal/driver/mobile/event/key"
  55. "fyne.io/fyne/v2/internal/driver/mobile/event/lifecycle"
  56. "fyne.io/fyne/v2/internal/driver/mobile/event/paint"
  57. "fyne.io/fyne/v2/internal/driver/mobile/event/size"
  58. "fyne.io/fyne/v2/internal/driver/mobile/event/touch"
  59. "fyne.io/fyne/v2/internal/driver/mobile/mobileinit"
  60. )
  61. // mimeMap contains standard mime entries that are missing on Android
  62. var mimeMap = map[string]string{
  63. ".txt": "text/plain",
  64. }
  65. // RunOnJVM runs fn on a new goroutine locked to an OS thread with a JNIEnv.
  66. //
  67. // RunOnJVM blocks until the call to fn is complete. Any Java
  68. // exception or failure to attach to the JVM is returned as an error.
  69. //
  70. // The function fn takes vm, the current JavaVM*,
  71. // env, the current JNIEnv*, and
  72. // ctx, a jobject representing the global android.context.Context.
  73. func RunOnJVM(fn func(vm, jniEnv, ctx uintptr) error) error {
  74. return mobileinit.RunOnJVM(fn)
  75. }
  76. //export setCurrentContext
  77. func setCurrentContext(vm *C.JavaVM, ctx C.jobject) {
  78. mobileinit.SetCurrentContext(unsafe.Pointer(vm), uintptr(ctx))
  79. }
  80. //export callMain
  81. func callMain(mainPC uintptr) {
  82. for _, name := range []string{"FILESDIR", "TMPDIR", "PATH", "LD_LIBRARY_PATH"} {
  83. n := C.CString(name)
  84. os.Setenv(name, C.GoString(C.getenv(n)))
  85. C.free(unsafe.Pointer(n))
  86. }
  87. // Set timezone.
  88. //
  89. // Note that Android zoneinfo is stored in /system/usr/share/zoneinfo,
  90. // but it is in some kind of packed TZiff file that we do not support
  91. // yet. As a stopgap, we build a fixed zone using the tm_zone name.
  92. var curtime C.time_t
  93. var curtm C.struct_tm
  94. C.time(&curtime)
  95. C.localtime_r(&curtime, &curtm)
  96. tzOffset := int(curtm.tm_gmtoff)
  97. tz := C.GoString(curtm.tm_zone)
  98. time.Local = time.FixedZone(tz, tzOffset)
  99. go callfn.CallFn(mainPC)
  100. }
  101. //export onStart
  102. func onStart(activity *C.ANativeActivity) {
  103. }
  104. //export onResume
  105. func onResume(activity *C.ANativeActivity) {
  106. }
  107. //export onSaveInstanceState
  108. func onSaveInstanceState(activity *C.ANativeActivity, outSize *C.size_t) unsafe.Pointer {
  109. return nil
  110. }
  111. //export onPause
  112. func onPause(activity *C.ANativeActivity) {
  113. }
  114. //export onStop
  115. func onStop(activity *C.ANativeActivity) {
  116. }
  117. //export onCreate
  118. func onCreate(activity *C.ANativeActivity) {
  119. // Set the initial configuration.
  120. //
  121. // Note we use unbuffered channels to talk to the activity loop, and
  122. // NativeActivity calls these callbacks sequentially, so configuration
  123. // will be set before <-windowRedrawNeeded is processed.
  124. windowConfigChange <- windowConfigRead(activity)
  125. }
  126. //export onDestroy
  127. func onDestroy(activity *C.ANativeActivity) {
  128. activityDestroyed <- struct{}{}
  129. }
  130. //export onWindowFocusChanged
  131. func onWindowFocusChanged(activity *C.ANativeActivity, hasFocus C.int) {
  132. }
  133. //export onNativeWindowCreated
  134. func onNativeWindowCreated(activity *C.ANativeActivity, window *C.ANativeWindow) {
  135. }
  136. //export onNativeWindowRedrawNeeded
  137. func onNativeWindowRedrawNeeded(activity *C.ANativeActivity, window *C.ANativeWindow) {
  138. // Called on orientation change and window resize.
  139. // Send a request for redraw, and block this function
  140. // until a complete draw and buffer swap is completed.
  141. // This is required by the redraw documentation to
  142. // avoid bad draws.
  143. windowRedrawNeeded <- window
  144. <-windowRedrawDone
  145. }
  146. //export onNativeWindowDestroyed
  147. func onNativeWindowDestroyed(activity *C.ANativeActivity, window *C.ANativeWindow) {
  148. windowDestroyed <- window
  149. }
  150. //export onInputQueueCreated
  151. func onInputQueueCreated(activity *C.ANativeActivity, q *C.AInputQueue) {
  152. inputQueue <- q
  153. <-inputQueueDone
  154. }
  155. //export onInputQueueDestroyed
  156. func onInputQueueDestroyed(activity *C.ANativeActivity, q *C.AInputQueue) {
  157. inputQueue <- nil
  158. <-inputQueueDone
  159. }
  160. //export onContentRectChanged
  161. func onContentRectChanged(activity *C.ANativeActivity, rect *C.ARect) {
  162. }
  163. //export setDarkMode
  164. func setDarkMode(dark C.bool) {
  165. darkMode = bool(dark)
  166. }
  167. type windowConfig struct {
  168. orientation size.Orientation
  169. pixelsPerPt float32
  170. }
  171. func windowConfigRead(activity *C.ANativeActivity) windowConfig {
  172. aconfig := C.AConfiguration_new()
  173. C.AConfiguration_fromAssetManager(aconfig, activity.assetManager)
  174. orient := C.AConfiguration_getOrientation(aconfig)
  175. density := C.AConfiguration_getDensity(aconfig)
  176. C.AConfiguration_delete(aconfig)
  177. // Calculate the screen resolution. This value is approximate. For example,
  178. // a physical resolution of 200 DPI may be quantized to one of the
  179. // ACONFIGURATION_DENSITY_XXX values such as 160 or 240.
  180. //
  181. // A more accurate DPI could possibly be calculated from
  182. // https://developer.android.com/reference/android/util/DisplayMetrics.html#xdpi
  183. // but this does not appear to be accessible via the NDK. In any case, the
  184. // hardware might not even provide a more accurate number, as the system
  185. // does not apparently use the reported value. See golang.org/issue/13366
  186. // for a discussion.
  187. var dpi int
  188. switch density {
  189. case C.ACONFIGURATION_DENSITY_DEFAULT:
  190. dpi = 160
  191. case C.ACONFIGURATION_DENSITY_LOW,
  192. C.ACONFIGURATION_DENSITY_MEDIUM,
  193. 213, // C.ACONFIGURATION_DENSITY_TV
  194. C.ACONFIGURATION_DENSITY_HIGH,
  195. 320, // ACONFIGURATION_DENSITY_XHIGH
  196. 480, // ACONFIGURATION_DENSITY_XXHIGH
  197. 640: // ACONFIGURATION_DENSITY_XXXHIGH
  198. dpi = int(density)
  199. case C.ACONFIGURATION_DENSITY_NONE:
  200. log.Print("android device reports no screen density")
  201. dpi = 72
  202. default:
  203. log.Printf("android device reports unknown density: %d", density)
  204. // All we can do is guess.
  205. if density > 0 {
  206. dpi = int(density)
  207. } else {
  208. dpi = 72
  209. }
  210. }
  211. o := size.OrientationUnknown
  212. switch orient {
  213. case C.ACONFIGURATION_ORIENTATION_PORT:
  214. o = size.OrientationPortrait
  215. case C.ACONFIGURATION_ORIENTATION_LAND:
  216. o = size.OrientationLandscape
  217. }
  218. return windowConfig{
  219. orientation: o,
  220. pixelsPerPt: float32(dpi) / 72,
  221. }
  222. }
  223. //export onConfigurationChanged
  224. func onConfigurationChanged(activity *C.ANativeActivity) {
  225. // A rotation event first triggers onConfigurationChanged, then
  226. // calls onNativeWindowRedrawNeeded. We extract the orientation
  227. // here and save it for the redraw event.
  228. windowConfigChange <- windowConfigRead(activity)
  229. }
  230. //export onLowMemory
  231. func onLowMemory(activity *C.ANativeActivity) {
  232. runtime.GC()
  233. debug.FreeOSMemory()
  234. }
  235. var (
  236. inputQueue = make(chan *C.AInputQueue)
  237. inputQueueDone = make(chan struct{})
  238. windowDestroyed = make(chan *C.ANativeWindow)
  239. windowRedrawNeeded = make(chan *C.ANativeWindow)
  240. windowRedrawDone = make(chan struct{})
  241. windowConfigChange = make(chan windowConfig)
  242. activityDestroyed = make(chan struct{})
  243. screenInsetTop, screenInsetBottom, screenInsetLeft, screenInsetRight int
  244. darkMode bool
  245. )
  246. func init() {
  247. theApp.registerGLViewportFilter()
  248. }
  249. func main(f func(App)) {
  250. mainUserFn = f
  251. // TODO: merge the runInputQueue and mainUI functions?
  252. go func() {
  253. if err := mobileinit.RunOnJVM(runInputQueue); err != nil {
  254. log.Fatalf("app: %v", err)
  255. }
  256. }()
  257. // Preserve this OS thread for:
  258. // 1. the attached JNI thread
  259. // 2. the GL context
  260. if err := mobileinit.RunOnJVM(mainUI); err != nil {
  261. log.Fatalf("app: %v", err)
  262. }
  263. }
  264. // driverShowVirtualKeyboard requests the driver to show a virtual keyboard for text input
  265. func driverShowVirtualKeyboard(keyboard KeyboardType) {
  266. err := mobileinit.RunOnJVM(func(vm, jniEnv, ctx uintptr) error {
  267. env := (*C.JNIEnv)(unsafe.Pointer(jniEnv)) // not a Go heap pointer
  268. C.showKeyboard(env, C.int(int32(keyboard)))
  269. return nil
  270. })
  271. if err != nil {
  272. log.Fatalf("app: %v", err)
  273. }
  274. }
  275. // driverHideVirtualKeyboard requests the driver to hide any visible virtual keyboard
  276. func driverHideVirtualKeyboard() {
  277. if err := mobileinit.RunOnJVM(hideSoftInput); err != nil {
  278. log.Fatalf("app: %v", err)
  279. }
  280. }
  281. func hideSoftInput(vm, jniEnv, ctx uintptr) error {
  282. env := (*C.JNIEnv)(unsafe.Pointer(jniEnv)) // not a Go heap pointer
  283. C.hideKeyboard(env)
  284. return nil
  285. }
  286. var fileCallback func(string, func())
  287. //export filePickerReturned
  288. func filePickerReturned(str *C.char) {
  289. if fileCallback == nil {
  290. return
  291. }
  292. fileCallback(C.GoString(str), nil)
  293. fileCallback = nil
  294. }
  295. //export insetsChanged
  296. func insetsChanged(top, bottom, left, right int) {
  297. screenInsetTop, screenInsetBottom, screenInsetLeft, screenInsetRight = top, bottom, left, right
  298. }
  299. func mimeStringFromFilter(filter *FileFilter) string {
  300. mimes := "*/*"
  301. if filter.MimeTypes != nil {
  302. mimes = strings.Join(filter.MimeTypes, "|")
  303. } else if filter.Extensions != nil {
  304. var mimeTypes []string
  305. for _, ext := range filter.Extensions {
  306. if mimeEntry, ok := mimeMap[ext]; ok {
  307. mimeTypes = append(mimeTypes, mimeEntry)
  308. continue
  309. }
  310. mimeType := mime.TypeByExtension(ext)
  311. if mimeType == "" {
  312. log.Println("Could not find mime for extension " + ext + ", allowing all")
  313. return "*/*" // could not find one, so allow all
  314. }
  315. mimeTypes = append(mimeTypes, mimeType)
  316. }
  317. mimes = strings.Join(mimeTypes, "|")
  318. }
  319. return mimes
  320. }
  321. func driverShowFileOpenPicker(callback func(string, func()), filter *FileFilter) {
  322. fileCallback = callback
  323. mimes := mimeStringFromFilter(filter)
  324. mimeStr := C.CString(mimes)
  325. defer C.free(unsafe.Pointer(mimeStr))
  326. open := func(vm, jniEnv, ctx uintptr) error {
  327. // TODO pass in filter...
  328. env := (*C.JNIEnv)(unsafe.Pointer(jniEnv)) // not a Go heap pointer
  329. C.showFileOpen(env, mimeStr)
  330. return nil
  331. }
  332. if err := mobileinit.RunOnJVM(open); err != nil {
  333. log.Fatalf("app: %v", err)
  334. }
  335. }
  336. func driverShowFileSavePicker(callback func(string, func()), filter *FileFilter, filename string) {
  337. fileCallback = callback
  338. mimes := mimeStringFromFilter(filter)
  339. mimeStr := C.CString(mimes)
  340. defer C.free(unsafe.Pointer(mimeStr))
  341. filenameStr := C.CString(filename)
  342. defer C.free(unsafe.Pointer(filenameStr))
  343. save := func(vm, jniEnv, ctx uintptr) error {
  344. env := (*C.JNIEnv)(unsafe.Pointer(jniEnv)) // not a Go heap pointer
  345. C.showFileSave(env, mimeStr, filenameStr)
  346. return nil
  347. }
  348. if err := mobileinit.RunOnJVM(save); err != nil {
  349. log.Fatalf("app: %v", err)
  350. }
  351. }
  352. var mainUserFn func(App)
  353. var DisplayMetrics struct {
  354. WidthPx int
  355. HeightPx int
  356. }
  357. func mainUI(vm, jniEnv, ctx uintptr) error {
  358. workAvailable := theApp.worker.WorkAvailable()
  359. donec := make(chan struct{})
  360. go func() {
  361. mainUserFn(theApp)
  362. close(donec)
  363. }()
  364. var pixelsPerPt float32
  365. for {
  366. select {
  367. case <-donec:
  368. return nil
  369. case cfg := <-windowConfigChange:
  370. pixelsPerPt = cfg.pixelsPerPt
  371. case w := <-windowRedrawNeeded:
  372. if C.surface == nil {
  373. if errStr := C.createEGLSurface(w); errStr != nil {
  374. return fmt.Errorf("%s (%s)", C.GoString(errStr), eglGetError())
  375. }
  376. DisplayMetrics.WidthPx = int(C.ANativeWindow_getWidth(w))
  377. DisplayMetrics.HeightPx = int(C.ANativeWindow_getHeight(w))
  378. }
  379. theApp.sendLifecycle(lifecycle.StageFocused)
  380. widthPx := int(C.ANativeWindow_getWidth(w))
  381. heightPx := int(C.ANativeWindow_getHeight(w))
  382. theApp.events.In() <- size.Event{
  383. WidthPx: widthPx,
  384. HeightPx: heightPx,
  385. WidthPt: float32(widthPx) / pixelsPerPt,
  386. HeightPt: float32(heightPx) / pixelsPerPt,
  387. InsetTopPx: screenInsetTop,
  388. InsetBottomPx: screenInsetBottom,
  389. InsetLeftPx: screenInsetLeft,
  390. InsetRightPx: screenInsetRight,
  391. PixelsPerPt: pixelsPerPt,
  392. Orientation: screenOrientation(widthPx, heightPx), // we are guessing orientation here as it was not always working
  393. DarkMode: darkMode,
  394. }
  395. theApp.events.In() <- paint.Event{External: true}
  396. case <-windowDestroyed:
  397. if C.surface != nil {
  398. if errStr := C.destroyEGLSurface(); errStr != nil {
  399. return fmt.Errorf("%s (%s)", C.GoString(errStr), eglGetError())
  400. }
  401. }
  402. C.surface = nil
  403. theApp.sendLifecycle(lifecycle.StageAlive)
  404. case <-activityDestroyed:
  405. theApp.sendLifecycle(lifecycle.StageDead)
  406. case <-workAvailable:
  407. theApp.worker.DoWork()
  408. case <-theApp.publish:
  409. // TODO: compare a generation number to redrawGen for stale paints?
  410. if C.surface != nil {
  411. // eglSwapBuffers blocks until vsync.
  412. if C.eglSwapBuffers(C.display, C.surface) == C.EGL_FALSE {
  413. log.Printf("app: failed to swap buffers (%s)", eglGetError())
  414. }
  415. }
  416. select {
  417. case windowRedrawDone <- struct{}{}:
  418. default:
  419. }
  420. theApp.publishResult <- PublishResult{}
  421. }
  422. }
  423. }
  424. func runInputQueue(vm, jniEnv, ctx uintptr) error {
  425. env := (*C.JNIEnv)(unsafe.Pointer(jniEnv)) // not a Go heap pointer
  426. // Android loopers select on OS file descriptors, not Go channels, so we
  427. // translate the inputQueue channel to an ALooper_wake call.
  428. l := C.ALooper_prepare(C.ALOOPER_PREPARE_ALLOW_NON_CALLBACKS)
  429. pending := make(chan *C.AInputQueue, 1)
  430. go func() {
  431. for q := range inputQueue {
  432. pending <- q
  433. C.ALooper_wake(l)
  434. }
  435. }()
  436. var q *C.AInputQueue
  437. for {
  438. if C.ALooper_pollAll(-1, nil, nil, nil) == C.ALOOPER_POLL_WAKE {
  439. select {
  440. default:
  441. case p := <-pending:
  442. if q != nil {
  443. processEvents(env, q)
  444. C.AInputQueue_detachLooper(q)
  445. }
  446. q = p
  447. if q != nil {
  448. C.AInputQueue_attachLooper(q, l, 0, nil, nil)
  449. }
  450. inputQueueDone <- struct{}{}
  451. }
  452. }
  453. if q != nil {
  454. processEvents(env, q)
  455. }
  456. }
  457. }
  458. func processEvents(env *C.JNIEnv, q *C.AInputQueue) {
  459. var e *C.AInputEvent
  460. for C.AInputQueue_getEvent(q, &e) >= 0 {
  461. if C.AInputQueue_preDispatchEvent(q, e) != 0 {
  462. continue
  463. }
  464. processEvent(env, e)
  465. C.AInputQueue_finishEvent(q, e, 0)
  466. }
  467. }
  468. func processEvent(env *C.JNIEnv, e *C.AInputEvent) {
  469. switch C.AInputEvent_getType(e) {
  470. case C.AINPUT_EVENT_TYPE_KEY:
  471. processKey(env, e)
  472. case C.AINPUT_EVENT_TYPE_MOTION:
  473. // At most one of the events in this batch is an up or down event; get its index and change.
  474. upDownIndex := C.size_t(C.AMotionEvent_getAction(e)&C.AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> C.AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT
  475. upDownType := touch.TypeMove
  476. switch C.AMotionEvent_getAction(e) & C.AMOTION_EVENT_ACTION_MASK {
  477. case C.AMOTION_EVENT_ACTION_DOWN, C.AMOTION_EVENT_ACTION_POINTER_DOWN:
  478. upDownType = touch.TypeBegin
  479. case C.AMOTION_EVENT_ACTION_UP, C.AMOTION_EVENT_ACTION_POINTER_UP:
  480. upDownType = touch.TypeEnd
  481. }
  482. for i, n := C.size_t(0), C.AMotionEvent_getPointerCount(e); i < n; i++ {
  483. t := touch.TypeMove
  484. if i == upDownIndex {
  485. t = upDownType
  486. }
  487. theApp.events.In() <- touch.Event{
  488. X: float32(C.AMotionEvent_getX(e, i)),
  489. Y: float32(C.AMotionEvent_getY(e, i)),
  490. Sequence: touch.Sequence(C.AMotionEvent_getPointerId(e, i)),
  491. Type: t,
  492. }
  493. }
  494. default:
  495. log.Printf("unknown input event, type=%d", C.AInputEvent_getType(e))
  496. }
  497. }
  498. func processKey(env *C.JNIEnv, e *C.AInputEvent) {
  499. deviceID := C.AInputEvent_getDeviceId(e)
  500. if deviceID == 0 {
  501. // Software keyboard input, leaving for scribe/IME.
  502. return
  503. }
  504. k := key.Event{
  505. Rune: rune(C.getKeyRune(env, e)),
  506. Code: convAndroidKeyCode(int32(C.AKeyEvent_getKeyCode(e))),
  507. }
  508. if k.Rune >= '0' && k.Rune <= '9' { // GBoard generates key events for numbers, but we see them in textChanged
  509. return
  510. }
  511. switch C.AKeyEvent_getAction(e) {
  512. case C.AKEY_STATE_DOWN:
  513. k.Direction = key.DirPress
  514. case C.AKEY_STATE_UP:
  515. k.Direction = key.DirRelease
  516. default:
  517. k.Direction = key.DirNone
  518. }
  519. // TODO(crawshaw): set Modifiers.
  520. theApp.events.In() <- k
  521. }
  522. func eglGetError() string {
  523. switch errNum := C.eglGetError(); errNum {
  524. case C.EGL_SUCCESS:
  525. return "EGL_SUCCESS"
  526. case C.EGL_NOT_INITIALIZED:
  527. return "EGL_NOT_INITIALIZED"
  528. case C.EGL_BAD_ACCESS:
  529. return "EGL_BAD_ACCESS"
  530. case C.EGL_BAD_ALLOC:
  531. return "EGL_BAD_ALLOC"
  532. case C.EGL_BAD_ATTRIBUTE:
  533. return "EGL_BAD_ATTRIBUTE"
  534. case C.EGL_BAD_CONTEXT:
  535. return "EGL_BAD_CONTEXT"
  536. case C.EGL_BAD_CONFIG:
  537. return "EGL_BAD_CONFIG"
  538. case C.EGL_BAD_CURRENT_SURFACE:
  539. return "EGL_BAD_CURRENT_SURFACE"
  540. case C.EGL_BAD_DISPLAY:
  541. return "EGL_BAD_DISPLAY"
  542. case C.EGL_BAD_SURFACE:
  543. return "EGL_BAD_SURFACE"
  544. case C.EGL_BAD_MATCH:
  545. return "EGL_BAD_MATCH"
  546. case C.EGL_BAD_PARAMETER:
  547. return "EGL_BAD_PARAMETER"
  548. case C.EGL_BAD_NATIVE_PIXMAP:
  549. return "EGL_BAD_NATIVE_PIXMAP"
  550. case C.EGL_BAD_NATIVE_WINDOW:
  551. return "EGL_BAD_NATIVE_WINDOW"
  552. case C.EGL_CONTEXT_LOST:
  553. return "EGL_CONTEXT_LOST"
  554. default:
  555. return fmt.Sprintf("Unknown EGL err: %d", errNum)
  556. }
  557. }
  558. var androidKeycoe = map[int32]key.Code{
  559. C.AKEYCODE_HOME: key.CodeHome,
  560. C.AKEYCODE_0: key.Code0,
  561. C.AKEYCODE_1: key.Code1,
  562. C.AKEYCODE_2: key.Code2,
  563. C.AKEYCODE_3: key.Code3,
  564. C.AKEYCODE_4: key.Code4,
  565. C.AKEYCODE_5: key.Code5,
  566. C.AKEYCODE_6: key.Code6,
  567. C.AKEYCODE_7: key.Code7,
  568. C.AKEYCODE_8: key.Code8,
  569. C.AKEYCODE_9: key.Code9,
  570. C.AKEYCODE_VOLUME_UP: key.CodeVolumeUp,
  571. C.AKEYCODE_VOLUME_DOWN: key.CodeVolumeDown,
  572. C.AKEYCODE_A: key.CodeA,
  573. C.AKEYCODE_B: key.CodeB,
  574. C.AKEYCODE_C: key.CodeC,
  575. C.AKEYCODE_D: key.CodeD,
  576. C.AKEYCODE_E: key.CodeE,
  577. C.AKEYCODE_F: key.CodeF,
  578. C.AKEYCODE_G: key.CodeG,
  579. C.AKEYCODE_H: key.CodeH,
  580. C.AKEYCODE_I: key.CodeI,
  581. C.AKEYCODE_J: key.CodeJ,
  582. C.AKEYCODE_K: key.CodeK,
  583. C.AKEYCODE_L: key.CodeL,
  584. C.AKEYCODE_M: key.CodeM,
  585. C.AKEYCODE_N: key.CodeN,
  586. C.AKEYCODE_O: key.CodeO,
  587. C.AKEYCODE_P: key.CodeP,
  588. C.AKEYCODE_Q: key.CodeQ,
  589. C.AKEYCODE_R: key.CodeR,
  590. C.AKEYCODE_S: key.CodeS,
  591. C.AKEYCODE_T: key.CodeT,
  592. C.AKEYCODE_U: key.CodeU,
  593. C.AKEYCODE_V: key.CodeV,
  594. C.AKEYCODE_W: key.CodeW,
  595. C.AKEYCODE_X: key.CodeX,
  596. C.AKEYCODE_Y: key.CodeY,
  597. C.AKEYCODE_Z: key.CodeZ,
  598. C.AKEYCODE_COMMA: key.CodeComma,
  599. C.AKEYCODE_PERIOD: key.CodeFullStop,
  600. C.AKEYCODE_ALT_LEFT: key.CodeLeftAlt,
  601. C.AKEYCODE_ALT_RIGHT: key.CodeRightAlt,
  602. C.AKEYCODE_SHIFT_LEFT: key.CodeLeftShift,
  603. C.AKEYCODE_SHIFT_RIGHT: key.CodeRightShift,
  604. C.AKEYCODE_TAB: key.CodeTab,
  605. C.AKEYCODE_SPACE: key.CodeSpacebar,
  606. C.AKEYCODE_ENTER: key.CodeReturnEnter,
  607. C.AKEYCODE_DEL: key.CodeDeleteBackspace,
  608. C.AKEYCODE_GRAVE: key.CodeGraveAccent,
  609. C.AKEYCODE_MINUS: key.CodeHyphenMinus,
  610. C.AKEYCODE_EQUALS: key.CodeEqualSign,
  611. C.AKEYCODE_LEFT_BRACKET: key.CodeLeftSquareBracket,
  612. C.AKEYCODE_RIGHT_BRACKET: key.CodeRightSquareBracket,
  613. C.AKEYCODE_BACKSLASH: key.CodeBackslash,
  614. C.AKEYCODE_SEMICOLON: key.CodeSemicolon,
  615. C.AKEYCODE_APOSTROPHE: key.CodeApostrophe,
  616. C.AKEYCODE_SLASH: key.CodeSlash,
  617. C.AKEYCODE_PAGE_UP: key.CodePageUp,
  618. C.AKEYCODE_PAGE_DOWN: key.CodePageDown,
  619. C.AKEYCODE_ESCAPE: key.CodeEscape,
  620. C.AKEYCODE_FORWARD_DEL: key.CodeDeleteForward,
  621. C.AKEYCODE_CTRL_LEFT: key.CodeLeftControl,
  622. C.AKEYCODE_CTRL_RIGHT: key.CodeRightControl,
  623. C.AKEYCODE_CAPS_LOCK: key.CodeCapsLock,
  624. C.AKEYCODE_META_LEFT: key.CodeLeftGUI,
  625. C.AKEYCODE_META_RIGHT: key.CodeRightGUI,
  626. C.AKEYCODE_INSERT: key.CodeInsert,
  627. C.AKEYCODE_F1: key.CodeF1,
  628. C.AKEYCODE_F2: key.CodeF2,
  629. C.AKEYCODE_F3: key.CodeF3,
  630. C.AKEYCODE_F4: key.CodeF4,
  631. C.AKEYCODE_F5: key.CodeF5,
  632. C.AKEYCODE_F6: key.CodeF6,
  633. C.AKEYCODE_F7: key.CodeF7,
  634. C.AKEYCODE_F8: key.CodeF8,
  635. C.AKEYCODE_F9: key.CodeF9,
  636. C.AKEYCODE_F10: key.CodeF10,
  637. C.AKEYCODE_F11: key.CodeF11,
  638. C.AKEYCODE_F12: key.CodeF12,
  639. C.AKEYCODE_NUM_LOCK: key.CodeKeypadNumLock,
  640. C.AKEYCODE_NUMPAD_0: key.CodeKeypad0,
  641. C.AKEYCODE_NUMPAD_1: key.CodeKeypad1,
  642. C.AKEYCODE_NUMPAD_2: key.CodeKeypad2,
  643. C.AKEYCODE_NUMPAD_3: key.CodeKeypad3,
  644. C.AKEYCODE_NUMPAD_4: key.CodeKeypad4,
  645. C.AKEYCODE_NUMPAD_5: key.CodeKeypad5,
  646. C.AKEYCODE_NUMPAD_6: key.CodeKeypad6,
  647. C.AKEYCODE_NUMPAD_7: key.CodeKeypad7,
  648. C.AKEYCODE_NUMPAD_8: key.CodeKeypad8,
  649. C.AKEYCODE_NUMPAD_9: key.CodeKeypad9,
  650. C.AKEYCODE_NUMPAD_DIVIDE: key.CodeKeypadSlash,
  651. C.AKEYCODE_NUMPAD_MULTIPLY: key.CodeKeypadAsterisk,
  652. C.AKEYCODE_NUMPAD_SUBTRACT: key.CodeKeypadHyphenMinus,
  653. C.AKEYCODE_NUMPAD_ADD: key.CodeKeypadPlusSign,
  654. C.AKEYCODE_NUMPAD_DOT: key.CodeKeypadFullStop,
  655. C.AKEYCODE_NUMPAD_ENTER: key.CodeKeypadEnter,
  656. C.AKEYCODE_NUMPAD_EQUALS: key.CodeKeypadEqualSign,
  657. C.AKEYCODE_VOLUME_MUTE: key.CodeMute,
  658. }
  659. func convAndroidKeyCode(aKeyCode int32) key.Code {
  660. if code, ok := androidKeycoe[aKeyCode]; ok {
  661. return code
  662. }
  663. return key.CodeUnknown
  664. }
  665. /*
  666. Many Android key codes do not map into USB HID codes.
  667. For those, key.CodeUnknown is returned. This switch has all
  668. cases, even the unknown ones, to serve as a documentation
  669. and search aid.
  670. C.AKEYCODE_UNKNOWN
  671. C.AKEYCODE_SOFT_LEFT
  672. C.AKEYCODE_SOFT_RIGHT
  673. C.AKEYCODE_BACK
  674. C.AKEYCODE_CALL
  675. C.AKEYCODE_ENDCALL
  676. C.AKEYCODE_STAR
  677. C.AKEYCODE_POUND
  678. C.AKEYCODE_DPAD_UP
  679. C.AKEYCODE_DPAD_DOWN
  680. C.AKEYCODE_DPAD_LEFT
  681. C.AKEYCODE_DPAD_RIGHT
  682. C.AKEYCODE_DPAD_CENTER
  683. C.AKEYCODE_POWER
  684. C.AKEYCODE_CAMERA
  685. C.AKEYCODE_CLEAR
  686. C.AKEYCODE_SYM
  687. C.AKEYCODE_EXPLORER
  688. C.AKEYCODE_ENVELOPE
  689. C.AKEYCODE_AT
  690. C.AKEYCODE_NUM
  691. C.AKEYCODE_HEADSETHOOK
  692. C.AKEYCODE_FOCUS
  693. C.AKEYCODE_PLUS
  694. C.AKEYCODE_MENU
  695. C.AKEYCODE_NOTIFICATION
  696. C.AKEYCODE_SEARCH
  697. C.AKEYCODE_MEDIA_PLAY_PAUSE
  698. C.AKEYCODE_MEDIA_STOP
  699. C.AKEYCODE_MEDIA_NEXT
  700. C.AKEYCODE_MEDIA_PREVIOUS
  701. C.AKEYCODE_MEDIA_REWIND
  702. C.AKEYCODE_MEDIA_FAST_FORWARD
  703. C.AKEYCODE_MUTE
  704. C.AKEYCODE_PICTSYMBOLS
  705. C.AKEYCODE_SWITCH_CHARSET
  706. C.AKEYCODE_BUTTON_A
  707. C.AKEYCODE_BUTTON_B
  708. C.AKEYCODE_BUTTON_C
  709. C.AKEYCODE_BUTTON_X
  710. C.AKEYCODE_BUTTON_Y
  711. C.AKEYCODE_BUTTON_Z
  712. C.AKEYCODE_BUTTON_L1
  713. C.AKEYCODE_BUTTON_R1
  714. C.AKEYCODE_BUTTON_L2
  715. C.AKEYCODE_BUTTON_R2
  716. C.AKEYCODE_BUTTON_THUMBL
  717. C.AKEYCODE_BUTTON_THUMBR
  718. C.AKEYCODE_BUTTON_START
  719. C.AKEYCODE_BUTTON_SELECT
  720. C.AKEYCODE_BUTTON_MODE
  721. C.AKEYCODE_SCROLL_LOCK
  722. C.AKEYCODE_FUNCTION
  723. C.AKEYCODE_SYSRQ
  724. C.AKEYCODE_BREAK
  725. C.AKEYCODE_MOVE_HOME
  726. C.AKEYCODE_MOVE_END
  727. C.AKEYCODE_FORWARD
  728. C.AKEYCODE_MEDIA_PLAY
  729. C.AKEYCODE_MEDIA_PAUSE
  730. C.AKEYCODE_MEDIA_CLOSE
  731. C.AKEYCODE_MEDIA_EJECT
  732. C.AKEYCODE_MEDIA_RECORD
  733. C.AKEYCODE_NUMPAD_COMMA
  734. C.AKEYCODE_NUMPAD_LEFT_PAREN
  735. C.AKEYCODE_NUMPAD_RIGHT_PAREN
  736. C.AKEYCODE_INFO
  737. C.AKEYCODE_CHANNEL_UP
  738. C.AKEYCODE_CHANNEL_DOWN
  739. C.AKEYCODE_ZOOM_IN
  740. C.AKEYCODE_ZOOM_OUT
  741. C.AKEYCODE_TV
  742. C.AKEYCODE_WINDOW
  743. C.AKEYCODE_GUIDE
  744. C.AKEYCODE_DVR
  745. C.AKEYCODE_BOOKMARK
  746. C.AKEYCODE_CAPTIONS
  747. C.AKEYCODE_SETTINGS
  748. C.AKEYCODE_TV_POWER
  749. C.AKEYCODE_TV_INPUT
  750. C.AKEYCODE_STB_POWER
  751. C.AKEYCODE_STB_INPUT
  752. C.AKEYCODE_AVR_POWER
  753. C.AKEYCODE_AVR_INPUT
  754. C.AKEYCODE_PROG_RED
  755. C.AKEYCODE_PROG_GREEN
  756. C.AKEYCODE_PROG_YELLOW
  757. C.AKEYCODE_PROG_BLUE
  758. C.AKEYCODE_APP_SWITCH
  759. C.AKEYCODE_BUTTON_1
  760. C.AKEYCODE_BUTTON_2
  761. C.AKEYCODE_BUTTON_3
  762. C.AKEYCODE_BUTTON_4
  763. C.AKEYCODE_BUTTON_5
  764. C.AKEYCODE_BUTTON_6
  765. C.AKEYCODE_BUTTON_7
  766. C.AKEYCODE_BUTTON_8
  767. C.AKEYCODE_BUTTON_9
  768. C.AKEYCODE_BUTTON_10
  769. C.AKEYCODE_BUTTON_11
  770. C.AKEYCODE_BUTTON_12
  771. C.AKEYCODE_BUTTON_13
  772. C.AKEYCODE_BUTTON_14
  773. C.AKEYCODE_BUTTON_15
  774. C.AKEYCODE_BUTTON_16
  775. C.AKEYCODE_LANGUAGE_SWITCH
  776. C.AKEYCODE_MANNER_MODE
  777. C.AKEYCODE_3D_MODE
  778. C.AKEYCODE_CONTACTS
  779. C.AKEYCODE_CALENDAR
  780. C.AKEYCODE_MUSIC
  781. C.AKEYCODE_CALCULATOR
  782. Defined in an NDK API version beyond what we use today:
  783. C.AKEYCODE_ASSIST
  784. C.AKEYCODE_BRIGHTNESS_DOWN
  785. C.AKEYCODE_BRIGHTNESS_UP
  786. C.AKEYCODE_RO
  787. C.AKEYCODE_YEN
  788. C.AKEYCODE_ZENKAKU_HANKAKU
  789. */