CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / widget / src / android / AndroidJavaWrappers.h
blob4acfddad0ce81c6067ead14a30aa34729f6af355
1 /* -*- Mode: c++; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Mozilla Android code.
17 * The Initial Developer of the Original Code is Mozilla Foundation.
18 * Portions created by the Initial Developer are Copyright (C) 2010
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Vladimir Vukicevic <vladimir@pobox.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef AndroidJavaWrappers_h__
39 #define AndroidJavaWrappers_h__
41 #include <jni.h>
42 #include <android/log.h>
44 #include "nsGeoPosition.h"
45 #include "nsPoint.h"
46 #include "nsRect.h"
47 #include "nsString.h"
49 //#define FORCE_ALOG 1
51 #ifndef ALOG
52 #if defined(DEBUG) || defined(FORCE_ALOG)
53 #define ALOG(args...) __android_log_print(ANDROID_LOG_INFO, "Gecko" , ## args)
54 #else
55 #define ALOG(args...)
56 #endif
57 #endif
59 namespace mozilla {
61 void InitAndroidJavaWrappers(JNIEnv *jEnv);
64 * Note: do not store global refs to any WrappedJavaObject;
65 * these are live only during a particular JNI method, as
66 * NewGlobalRef is -not- called on the jobject.
68 * If this is needed, WrappedJavaObject can be extended to
69 * handle it.
72 class WrappedJavaObject {
73 public:
74 WrappedJavaObject() :
75 wrapped_obj(0)
76 { }
78 WrappedJavaObject(jobject jobj) {
79 Init(jobj);
82 void Init(jobject jobj) {
83 wrapped_obj = jobj;
86 bool isNull() const {
87 return wrapped_obj == 0;
90 jobject wrappedObject() const {
91 return wrapped_obj;
94 protected:
95 jobject wrapped_obj;
98 class AndroidPoint : public WrappedJavaObject
100 public:
101 static void InitPointClass(JNIEnv *jEnv);
103 AndroidPoint() { }
104 AndroidPoint(JNIEnv *jenv, jobject jobj) {
105 Init(jenv, jobj);
108 void Init(JNIEnv *jenv, jobject jobj);
110 int X() { return mX; }
111 int Y() { return mY; }
113 protected:
114 int mX;
115 int mY;
117 static jclass jPointClass;
118 static jfieldID jXField;
119 static jfieldID jYField;
122 class AndroidRect : public WrappedJavaObject
124 public:
125 static void InitRectClass(JNIEnv *jEnv);
127 AndroidRect() { }
128 AndroidRect(JNIEnv *jenv, jobject jobj) {
129 Init(jenv, jobj);
132 void Init(JNIEnv *jenv, jobject jobj);
134 int Bottom() { return mBottom; }
135 int Left() { return mLeft; }
136 int Right() { return mRight; }
137 int Top() { return mTop; }
139 protected:
140 int mBottom;
141 int mLeft;
142 int mRight;
143 int mTop;
145 static jclass jRectClass;
146 static jfieldID jBottomField;
147 static jfieldID jLeftField;
148 static jfieldID jRightField;
149 static jfieldID jTopField;
152 class AndroidGeckoSurfaceView : public WrappedJavaObject
154 public:
155 static void InitGeckoSurfaceViewClass(JNIEnv *jEnv);
157 AndroidGeckoSurfaceView() { }
158 AndroidGeckoSurfaceView(jobject jobj) {
159 Init(jobj);
162 void Init(jobject jobj);
164 enum {
165 DRAW_ERROR = 0,
166 DRAW_GLES_2 = 1
169 int BeginDrawing();
170 jobject GetSoftwareDrawBuffer();
171 void EndDrawing();
172 void Draw2D(jobject buffer, int stride);
174 // must have a JNI local frame when calling this,
175 // and you'd better know what you're doing
176 jobject GetSurfaceHolder();
178 protected:
179 static jclass jGeckoSurfaceViewClass;
180 static jmethodID jBeginDrawingMethod;
181 static jmethodID jEndDrawingMethod;
182 static jmethodID jDraw2DMethod;
183 static jmethodID jGetSoftwareDrawBufferMethod;
184 static jmethodID jGetHolderMethod;
187 class AndroidKeyEvent
189 public:
190 enum {
191 KEYCODE_UNKNOWN = 0,
192 KEYCODE_SOFT_LEFT = 1,
193 KEYCODE_SOFT_RIGHT = 2,
194 KEYCODE_HOME = 3,
195 KEYCODE_BACK = 4,
196 KEYCODE_CALL = 5,
197 KEYCODE_ENDCALL = 6,
198 KEYCODE_0 = 7,
199 KEYCODE_1 = 8,
200 KEYCODE_2 = 9,
201 KEYCODE_3 = 10,
202 KEYCODE_4 = 11,
203 KEYCODE_5 = 12,
204 KEYCODE_6 = 13,
205 KEYCODE_7 = 14,
206 KEYCODE_8 = 15,
207 KEYCODE_9 = 16,
208 KEYCODE_STAR = 17,
209 KEYCODE_POUND = 18,
210 KEYCODE_DPAD_UP = 19,
211 KEYCODE_DPAD_DOWN = 20,
212 KEYCODE_DPAD_LEFT = 21,
213 KEYCODE_DPAD_RIGHT = 22,
214 KEYCODE_DPAD_CENTER = 23,
215 KEYCODE_VOLUME_UP = 24,
216 KEYCODE_VOLUME_DOWN = 25,
217 KEYCODE_POWER = 26,
218 KEYCODE_CAMERA = 27,
219 KEYCODE_CLEAR = 28,
220 KEYCODE_A = 29,
221 KEYCODE_B = 30,
222 KEYCODE_C = 31,
223 KEYCODE_D = 32,
224 KEYCODE_E = 33,
225 KEYCODE_F = 34,
226 KEYCODE_G = 35,
227 KEYCODE_H = 36,
228 KEYCODE_I = 37,
229 KEYCODE_J = 38,
230 KEYCODE_K = 39,
231 KEYCODE_L = 40,
232 KEYCODE_M = 41,
233 KEYCODE_N = 42,
234 KEYCODE_O = 43,
235 KEYCODE_P = 44,
236 KEYCODE_Q = 45,
237 KEYCODE_R = 46,
238 KEYCODE_S = 47,
239 KEYCODE_T = 48,
240 KEYCODE_U = 49,
241 KEYCODE_V = 50,
242 KEYCODE_W = 51,
243 KEYCODE_X = 52,
244 KEYCODE_Y = 53,
245 KEYCODE_Z = 54,
246 KEYCODE_COMMA = 55,
247 KEYCODE_PERIOD = 56,
248 KEYCODE_ALT_LEFT = 57,
249 KEYCODE_ALT_RIGHT = 58,
250 KEYCODE_SHIFT_LEFT = 59,
251 KEYCODE_SHIFT_RIGHT = 60,
252 KEYCODE_TAB = 61,
253 KEYCODE_SPACE = 62,
254 KEYCODE_SYM = 63,
255 KEYCODE_EXPLORER = 64,
256 KEYCODE_ENVELOPE = 65,
257 KEYCODE_ENTER = 66,
258 KEYCODE_DEL = 67,
259 KEYCODE_GRAVE = 68,
260 KEYCODE_MINUS = 69,
261 KEYCODE_EQUALS = 70,
262 KEYCODE_LEFT_BRACKET = 71,
263 KEYCODE_RIGHT_BRACKET = 72,
264 KEYCODE_BACKSLASH = 73,
265 KEYCODE_SEMICOLON = 74,
266 KEYCODE_APOSTROPHE = 75,
267 KEYCODE_SLASH = 76,
268 KEYCODE_AT = 77,
269 KEYCODE_NUM = 78,
270 KEYCODE_HEADSETHOOK = 79,
271 KEYCODE_FOCUS = 80,
272 KEYCODE_PLUS = 81,
273 KEYCODE_MENU = 82,
274 KEYCODE_NOTIFICATION = 83,
275 KEYCODE_SEARCH = 84,
276 KEYCODE_MEDIA_PLAY_PAUSE = 85,
277 KEYCODE_MEDIA_STOP = 86,
278 KEYCODE_MEDIA_NEXT = 87,
279 KEYCODE_MEDIA_PREVIOUS = 88,
280 KEYCODE_MEDIA_REWIND = 89,
281 KEYCODE_MEDIA_FAST_FORWARD = 90,
282 KEYCODE_MUTE = 91,
283 ACTION_DOWN = 0,
284 ACTION_UP = 1,
285 ACTION_MULTIPLE = 2,
286 META_ALT_ON = 0x00000002,
287 META_ALT_LEFT_ON = 0x00000010,
288 META_ALT_RIGHT_ON = 0x00000020,
289 META_SHIFT_ON = 0x00000001,
290 META_SHIFT_LEFT_ON = 0x00000040,
291 META_SHIFT_RIGHT_ON = 0x00000080,
292 META_SYM_ON = 0x00000004,
293 FLAG_WOKE_HERE = 0x00000001,
294 FLAG_SOFT_KEYBOARD = 0x00000002,
295 FLAG_KEEP_TOUCH_MODE = 0x00000004,
296 FLAG_FROM_SYSTEM = 0x00000008,
297 FLAG_EDITOR_ACTION = 0x00000010,
298 FLAG_CANCELED = 0x00000020,
299 FLAG_VIRTUAL_HARD_KEY = 0x00000040,
300 FLAG_LONG_PRESS = 0x00000080,
301 FLAG_CANCELED_LONG_PRESS = 0x00000100,
302 FLAG_TRACKING = 0x00000200,
303 FLAG_START_TRACKING = 0x40000000,
304 dummy_java_enum_list_end
308 class AndroidMotionEvent
310 public:
311 enum {
312 ACTION_MASK = 0xff,
313 ACTION_DOWN = 0,
314 ACTION_UP = 1,
315 ACTION_MOVE = 2,
316 ACTION_CANCEL = 3,
317 ACTION_OUTSIDE = 4,
318 ACTION_POINTER_DOWN = 5,
319 ACTION_POINTER_UP = 6,
320 ACTION_POINTER_ID_MASK = 0xff00,
321 ACTION_POINTER_ID_SHIFT = 8,
322 EDGE_TOP = 0x00000001,
323 EDGE_BOTTOM = 0x00000002,
324 EDGE_LEFT = 0x00000004,
325 EDGE_RIGHT = 0x00000008,
326 SAMPLE_X = 0,
327 SAMPLE_Y = 1,
328 SAMPLE_PRESSURE = 2,
329 SAMPLE_SIZE = 3,
330 NUM_SAMPLE_DATA = 4,
331 dummy_java_enum_list_end
335 class AndroidLocation : public WrappedJavaObject
337 public:
338 static void InitLocationClass(JNIEnv *jEnv);
339 static nsGeoPosition* CreateGeoPosition(JNIEnv *jenv, jobject jobj);
340 static jclass jLocationClass;
341 static jmethodID jGetLatitudeMethod;
342 static jmethodID jGetLongitudeMethod;
343 static jmethodID jGetAltitudeMethod;
344 static jmethodID jGetAccuracyMethod;
345 static jmethodID jGetBearingMethod;
346 static jmethodID jGetSpeedMethod;
347 static jmethodID jGetTimeMethod;
350 class AndroidGeckoEvent : public WrappedJavaObject
352 public:
353 static void InitGeckoEventClass(JNIEnv *jEnv);
355 AndroidGeckoEvent() { }
356 AndroidGeckoEvent(int aType) {
357 Init(aType);
359 AndroidGeckoEvent(int x1, int y1, int x2, int y2) {
360 Init(x1, y1, x2, y2);
362 AndroidGeckoEvent(JNIEnv *jenv, jobject jobj) {
363 Init(jenv, jobj);
366 void Init(JNIEnv *jenv, jobject jobj);
367 void Init(int aType);
368 void Init(int x1, int y1, int x2, int y2);
370 int Action() { return mAction; }
371 int Type() { return mType; }
372 int64_t Time() { return mTime; }
373 const nsIntPoint& P0() { return mP0; }
374 const nsIntPoint& P1() { return mP1; }
375 float X() { return mX; }
376 float Y() { return mY; }
377 float Z() { return mZ; }
378 const nsIntRect& Rect() { return mRect; }
379 nsAString& Characters() { return mCharacters; }
380 int KeyCode() { return mKeyCode; }
381 int MetaState() { return mMetaState; }
382 int Flags() { return mFlags; }
383 int UnicodeChar() { return mUnicodeChar; }
384 int Offset() { return mOffset; }
385 int Count() { return mCount; }
386 int RangeType() { return mRangeType; }
387 int RangeStyles() { return mRangeStyles; }
388 int RangeForeColor() { return mRangeForeColor; }
389 int RangeBackColor() { return mRangeBackColor; }
390 nsGeoPosition* GeoPosition() { return mGeoPosition; }
392 protected:
393 int mAction;
394 int mType;
395 int64_t mTime;
396 nsIntPoint mP0;
397 nsIntPoint mP1;
398 nsIntRect mRect;
399 int mFlags, mMetaState;
400 int mKeyCode, mUnicodeChar;
401 int mOffset, mCount;
402 int mRangeType, mRangeStyles;
403 int mRangeForeColor, mRangeBackColor;
404 float mX, mY, mZ;
405 nsString mCharacters;
406 nsRefPtr<nsGeoPosition> mGeoPosition;
408 void ReadP0Field(JNIEnv *jenv);
409 void ReadP1Field(JNIEnv *jenv);
410 void ReadRectField(JNIEnv *jenv);
411 void ReadCharactersField(JNIEnv *jenv);
413 static jclass jGeckoEventClass;
414 static jfieldID jActionField;
415 static jfieldID jTypeField;
416 static jfieldID jTimeField;
417 static jfieldID jP0Field;
418 static jfieldID jP1Field;
419 static jfieldID jXField;
420 static jfieldID jYField;
421 static jfieldID jZField;
422 static jfieldID jRectField;
423 static jfieldID jNativeWindowField;
425 static jfieldID jCharactersField;
426 static jfieldID jKeyCodeField;
427 static jfieldID jMetaStateField;
428 static jfieldID jFlagsField;
429 static jfieldID jOffsetField;
430 static jfieldID jCountField;
431 static jfieldID jUnicodeCharField;
432 static jfieldID jRangeTypeField;
433 static jfieldID jRangeStylesField;
434 static jfieldID jRangeForeColorField;
435 static jfieldID jRangeBackColorField;
436 static jfieldID jLocationField;
438 public:
439 enum {
440 NATIVE_POKE = 0,
441 KEY_EVENT = 1,
442 MOTION_EVENT = 2,
443 SENSOR_EVENT = 3,
444 LOCATION_EVENT = 4,
445 IME_EVENT = 5,
446 DRAW = 6,
447 SIZE_CHANGED = 7,
448 ACTIVITY_STOPPING = 8,
449 ACTIVITY_PAUSING = 9,
450 ACTIVITY_SHUTDOWN = 10,
451 LOAD_URI = 11,
452 SURFACE_CREATED = 12,
453 SURFACE_DESTROYED = 13,
454 dummy_java_enum_list_end
457 enum {
458 IME_COMPOSITION_END = 0,
459 IME_COMPOSITION_BEGIN = 1,
460 IME_SET_TEXT = 2,
461 IME_GET_TEXT = 3,
462 IME_DELETE_TEXT = 4,
463 IME_SET_SELECTION = 5,
464 IME_GET_SELECTION = 6,
465 IME_ADD_RANGE = 7
469 class nsJNIString : public nsString
471 public:
472 nsJNIString(jstring jstr, JNIEnv *jenv = NULL);
477 #endif