OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "surface_glue_android.h" | 8 #include "surface_glue_android.h" |
9 | 9 |
10 #include <jni.h> | 10 #include <jni.h> |
11 #include <pthread.h> | 11 #include <pthread.h> |
12 #include <stdio.h> | 12 #include <stdio.h> |
13 #include <unistd.h> | 13 #include <unistd.h> |
14 #include <unordered_map> | 14 #include <unordered_map> |
15 | 15 |
16 #include <android/keycodes.h> | 16 #include <android/keycodes.h> |
17 #include <android/looper.h> | 17 #include <android/looper.h> |
18 #include <android/native_window_jni.h> | 18 #include <android/native_window_jni.h> |
| 19 #include <android/input.h> |
19 | 20 |
20 #include "../Application.h" | 21 #include "../Application.h" |
21 #include "SkTypes.h" | 22 #include "SkTypes.h" |
22 #include "SkUtils.h" | 23 #include "SkUtils.h" |
23 #include "Window_android.h" | 24 #include "Window_android.h" |
24 | 25 |
25 namespace sk_app { | 26 namespace sk_app { |
26 | 27 |
27 static const int LOOPER_ID_MESSAGEPIPE = 1; | 28 static const int LOOPER_ID_MESSAGEPIPE = 1; |
28 | 29 |
29 static const std::unordered_map<int, Window::Key> ANDROID_TO_WINDOW_KEYMAP({ | 30 static const std::unordered_map<int, Window::Key> ANDROID_TO_WINDOW_KEYMAP({ |
30 {AKEYCODE_SOFT_LEFT, Window::Key::kLeft}, | 31 {AKEYCODE_SOFT_LEFT, Window::Key::kLeft}, |
31 {AKEYCODE_SOFT_RIGHT, Window::Key::kRight} | 32 {AKEYCODE_SOFT_RIGHT, Window::Key::kRight} |
32 }); | 33 }); |
33 | 34 |
| 35 static const std::unordered_map<int, Window::InputState> ANDROID_TO_WINDOW_STATE
MAP({ |
| 36 {AMOTION_EVENT_ACTION_DOWN, Window::kDown_InputState}, |
| 37 {AMOTION_EVENT_ACTION_POINTER_DOWN, Window::kDown_InputState}, |
| 38 {AMOTION_EVENT_ACTION_UP, Window::kUp_InputState}, |
| 39 {AMOTION_EVENT_ACTION_POINTER_UP, Window::kUp_InputState}, |
| 40 {AMOTION_EVENT_ACTION_MOVE, Window::kMove_InputState}, |
| 41 {AMOTION_EVENT_ACTION_CANCEL, Window::kUp_InputState}, |
| 42 }); |
| 43 |
34 SkiaAndroidApp::SkiaAndroidApp(JNIEnv* env, jobject androidApp) { | 44 SkiaAndroidApp::SkiaAndroidApp(JNIEnv* env, jobject androidApp) { |
35 env->GetJavaVM(&fJavaVM); | 45 env->GetJavaVM(&fJavaVM); |
36 fAndroidApp = env->NewGlobalRef(androidApp); | 46 fAndroidApp = env->NewGlobalRef(androidApp); |
37 jclass cls = env->GetObjectClass(fAndroidApp); | 47 jclass cls = env->GetObjectClass(fAndroidApp); |
38 fSetTitleMethodID = env->GetMethodID(cls, "setTitle", "(Ljava/lang/String;)V
"); | 48 fSetTitleMethodID = env->GetMethodID(cls, "setTitle", "(Ljava/lang/String;)V
"); |
39 fNativeWindow = nullptr; | 49 fNativeWindow = nullptr; |
40 pthread_create(&fThread, nullptr, pthread_main, this); | 50 pthread_create(&fThread, nullptr, pthread_main, this); |
41 } | 51 } |
42 | 52 |
43 SkiaAndroidApp::~SkiaAndroidApp() { | 53 SkiaAndroidApp::~SkiaAndroidApp() { |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 case kSurfaceDestroyed: { | 124 case kSurfaceDestroyed: { |
115 if (skiaAndroidApp->fNativeWindow) { | 125 if (skiaAndroidApp->fNativeWindow) { |
116 auto window_android = (Window_android*)skiaAndroidApp->fWindow; | 126 auto window_android = (Window_android*)skiaAndroidApp->fWindow; |
117 window_android->onDisplayDestroyed(); | 127 window_android->onDisplayDestroyed(); |
118 ANativeWindow_release(skiaAndroidApp->fNativeWindow); | 128 ANativeWindow_release(skiaAndroidApp->fNativeWindow); |
119 skiaAndroidApp->fNativeWindow = nullptr; | 129 skiaAndroidApp->fNativeWindow = nullptr; |
120 } | 130 } |
121 break; | 131 break; |
122 } | 132 } |
123 case kKeyPressed: { | 133 case kKeyPressed: { |
124 auto it = ANDROID_TO_WINDOW_KEYMAP.find(message.keycode); | 134 auto it = ANDROID_TO_WINDOW_KEYMAP.find(message.fKeycode); |
125 SkASSERT(it != ANDROID_TO_WINDOW_KEYMAP.end()); | 135 SkASSERT(it != ANDROID_TO_WINDOW_KEYMAP.end()); |
126 // No modifier is supported so far | 136 // No modifier is supported so far |
127 skiaAndroidApp->fWindow->onKey(it->second, Window::kDown_InputState,
0); | 137 skiaAndroidApp->fWindow->onKey(it->second, Window::kDown_InputState,
0); |
128 skiaAndroidApp->fWindow->onKey(it->second, Window::kUp_InputState, 0
); | 138 skiaAndroidApp->fWindow->onKey(it->second, Window::kUp_InputState, 0
); |
129 break; | 139 break; |
130 } | 140 } |
| 141 case kTouched: { |
| 142 auto it = ANDROID_TO_WINDOW_STATEMAP.find(message.fTouchState); |
| 143 SkASSERT(it != ANDROID_TO_WINDOW_STATEMAP.end()); |
| 144 skiaAndroidApp->fWindow->onTouch(message.fTouchOwner, it->second, me
ssage.fTouchX, message.fTouchY); |
| 145 break; |
| 146 } |
131 default: { | 147 default: { |
132 // do nothing | 148 // do nothing |
133 } | 149 } |
134 } | 150 } |
135 | 151 |
136 return 1; // continue receiving callbacks | 152 return 1; // continue receiving callbacks |
137 } | 153 } |
138 | 154 |
139 void* SkiaAndroidApp::pthread_main(void* arg) { | 155 void* SkiaAndroidApp::pthread_main(void* arg) { |
140 SkDebugf("pthread_main begins"); | 156 SkDebugf("pthread_main begins"); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 auto skiaAndroidApp = (SkiaAndroidApp*)handle; | 212 auto skiaAndroidApp = (SkiaAndroidApp*)handle; |
197 skiaAndroidApp->postMessage(Message(kSurfaceDestroyed)); | 213 skiaAndroidApp->postMessage(Message(kSurfaceDestroyed)); |
198 } | 214 } |
199 | 215 |
200 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onKeyPress
ed(JNIEnv* env, | 216 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onKeyPress
ed(JNIEnv* env, |
201
jobject activity, | 217
jobject activity, |
202
jlong handle, | 218
jlong handle, |
203
jint keycode) { | 219
jint keycode) { |
204 auto skiaAndroidApp = (SkiaAndroidApp*)handle; | 220 auto skiaAndroidApp = (SkiaAndroidApp*)handle; |
205 Message message(kKeyPressed); | 221 Message message(kKeyPressed); |
206 message.keycode = keycode; | 222 message.fKeycode = keycode; |
207 skiaAndroidApp->postMessage(message); | 223 skiaAndroidApp->postMessage(message); |
208 } | 224 } |
209 | 225 |
| 226 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onTouched(
JNIEnv* env, |
| 227 jobject activity, jlong handle, jint owner, jfloat x, jfloat y, jint state)
{ |
| 228 auto skiaAndroidApp = (SkiaAndroidApp*)handle; |
| 229 Message message(kTouched); |
| 230 message.fTouchOwner = owner; |
| 231 message.fTouchState = state; |
| 232 message.fTouchX = x; |
| 233 message.fTouchY = y; |
| 234 skiaAndroidApp->postMessage(message); |
| 235 } |
| 236 |
210 } // namespace sk_app | 237 } // namespace sk_app |
OLD | NEW |