1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "VrManagerService"
18
19#include <android_runtime/AndroidRuntime.h>
20#include <jni.h>
21#include <JNIHelp.h>
22
23#include <utils/Errors.h>
24#include <utils/Log.h>
25#include <hardware/hardware.h>
26#include <hardware/vr.h>
27
28namespace android {
29
30static vr_module_t *gVrHardwareModule = NULL;
31
32
33static void init_native(JNIEnv* /* env */, jclass /* clazz */) {
34    if (gVrHardwareModule != NULL) {
35        // This call path should never be hit.
36        ALOGE("%s: May not initialize VR hardware module more than once!", __FUNCTION__);
37        return;
38    }
39
40    int err = hw_get_module(VR_HARDWARE_MODULE_ID, (hw_module_t const**)&gVrHardwareModule);
41    if (err) {
42        ALOGW("%s: Could not open VR hardware module, error %s (%d).", __FUNCTION__,
43                strerror(-err), err);
44        return;
45    }
46
47    // Call init method if implemented.
48    if (gVrHardwareModule->init) {
49        gVrHardwareModule->init(gVrHardwareModule);
50    }
51}
52
53static void setVrMode_native(JNIEnv* /* env */, jclass /* clazz */, jboolean enabled) {
54    if (gVrHardwareModule == NULL) {
55        // There is no VR hardware module implemented, do nothing.
56        return;
57    }
58
59    // Call set_vr_mode method, this must be implemented if the HAL exists.
60    gVrHardwareModule->set_vr_mode(gVrHardwareModule, static_cast<bool>(enabled));
61}
62
63static const JNINativeMethod method_table[] = {
64    { "initializeNative", "()V", (void*)init_native },
65    { "setVrModeNative", "(Z)V", (void*)setVrMode_native },
66};
67
68int register_android_server_vr_VrManagerService(JNIEnv *env)
69{
70    return jniRegisterNativeMethods(env, "com/android/server/vr/VrManagerService",
71            method_table, NELEM(method_table));
72}
73
74}; // namespace android
75