1/* //device/apps/Quake/quake/src/QW/client/main.c
2**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include <nativehelper/jni.h>
19#include <stdio.h>
20#include <assert.h>
21#include <dlfcn.h>
22
23#define LOG_TAG "QuakeMaster"
24#include <utils/Log.h>
25
26int AndroidInit();
27int AndroidEvent2(int type, int value);
28int AndroidMotionEvent(unsigned long long eventTime, int action,
29        float x, float y, float pressure, float size, int deviceId);
30int AndroidTrackballEvent(unsigned long long eventTime, int action,
31        float x, float y);
32int AndroidStep(int width, int height);
33void AndroidQuit();
34
35jboolean
36qinit(JNIEnv *env, jobject thiz) {
37    ALOGI("qinit");
38    return AndroidInit() ? JNI_TRUE : JNI_FALSE;
39 }
40
41jboolean
42qevent(JNIEnv *env, jobject thiz, jint type, jint value) {
43    return AndroidEvent2(type, value) ? JNI_TRUE : JNI_FALSE;
44}
45
46jboolean
47qmotionevent(JNIEnv *env, jobject thiz, jlong eventTime, jint action,
48        jfloat x, jfloat y, jfloat pressure, jfloat size, jint deviceId) {
49    return AndroidMotionEvent((unsigned long long) eventTime,
50            action, x, y, pressure, size,
51            deviceId) ? JNI_TRUE : JNI_FALSE;
52}
53
54jboolean
55qtrackballevent(JNIEnv *env, jobject thiz, jlong eventTime, jint action,
56        jfloat x, jfloat y) {
57    return AndroidTrackballEvent((unsigned long long) eventTime,
58            action, x, y) ? JNI_TRUE : JNI_FALSE;
59}
60
61jboolean
62qstep(JNIEnv *env, jobject thiz, jint width, jint height) {
63    return AndroidStep(width, height)  ? JNI_TRUE : JNI_FALSE;
64}
65
66void
67qquit(JNIEnv *env, jobject thiz) {
68    ALOGI("qquit");
69    AndroidQuit();
70 }
71
72static const char *classPathName = "com/android/quake/QuakeLib";
73
74static JNINativeMethod methods[] = {
75  {"init", "()Z", (void*)qinit },
76  {"event", "(II)Z", (void*)qevent },
77  {"motionEvent", "(JIFFFFI)Z", (void*) qmotionevent },
78  {"trackballEvent", "(JIFF)Z", (void*) qtrackballevent },
79  {"step", "(II)Z", (void*)qstep },
80  {"quit", "()V", (void*)qquit },
81};
82
83/*
84 * Register several native methods for one class.
85 */
86static int registerNativeMethods(JNIEnv* env, const char* className,
87    JNINativeMethod* gMethods, int numMethods)
88{
89    jclass clazz;
90
91    clazz = env->FindClass(className);
92    if (clazz == NULL) {
93        fprintf(stderr,
94            "Native registration unable to find class '%s'\n", className);
95        return JNI_FALSE;
96    }
97    if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
98        fprintf(stderr, "RegisterNatives failed for '%s'\n", className);
99        return JNI_FALSE;
100    }
101
102    return JNI_TRUE;
103}
104
105/*
106 * Register native methods for all classes we know about.
107 */
108static int registerNatives(JNIEnv* env)
109{
110  if (!registerNativeMethods(env, classPathName,
111                 methods, sizeof(methods) / sizeof(methods[0]))) {
112    return JNI_FALSE;
113  }
114
115  return JNI_TRUE;
116}
117
118/*
119 * Set some test stuff up.
120 *
121 * Returns the JNI version on success, -1 on failure.
122 */
123
124typedef union {
125    JNIEnv* env;
126    void* venv;
127} UnionJNIEnvToVoid;
128
129jint JNI_OnLoad(JavaVM* vm, void* reserved)
130{
131    UnionJNIEnvToVoid uenv;
132    uenv.venv = NULL;
133    jint result = -1;
134    JNIEnv* env = NULL;
135
136    if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_4) != JNI_OK) {
137        fprintf(stderr, "ERROR: GetEnv failed\n");
138        goto bail;
139    }
140    env = uenv.env;
141
142    assert(env != NULL);
143
144    printf("In mgmain JNI_OnLoad\n");
145
146    if (!registerNatives(env)) {
147        fprintf(stderr, "ERROR: quakemaster native registration failed\n");
148        goto bail;
149    }
150
151    /* success -- return valid version number */
152    result = JNI_VERSION_1_4;
153
154bail:
155    return result;
156}
157