android.go 22 KB

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