<commit_before>/*
 * Copyright 2016 MovingBlocks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.terasology.rendering.openvrprovider;

import com.sun.jna.Memory;
import com.sun.jna.NativeLibrary;
import com.sun.jna.Pointer;
import jopenvr.HmdMatrix34_t;
import jopenvr.HmdMatrix44_t;
import jopenvr.JOpenVRLibrary;
import jopenvr.JOpenVRLibrary.EVREventType;
import jopenvr.Texture_t;
import jopenvr.TrackedDevicePose_t;
import jopenvr.VRControllerState_t;
import jopenvr.VRTextureBounds_t;
import jopenvr.VR_IVRCompositor_FnTable;
import jopenvr.VR_IVROverlay_FnTable;
import jopenvr.VR_IVRSettings_FnTable;
import jopenvr.VR_IVRSystem_FnTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.utilities.NativeHelper;

import java.nio.IntBuffer;

import static org.terasology.rendering.openvrprovider.ControllerListener.LEFT_CONTROLLER;
import static org.terasology.rendering.openvrprovider.ControllerListener.RIGHT_CONTROLLER;

/** This class is designed to make all API calls to OpenVR, thereby insulating it from the user. If you're looking to get
 * some information from the headset/controllers you should probably look at OpenVRStereoRenderer, ControllerListener,
 * or OpenVRState
 */
public class OpenVRProvider {
    public static Texture_t[] texType = new Texture_t[2];

    private static boolean initialized;
    private static final Logger logger = LoggerFactory.getLogger(OpenVRProvider.class);
    private static VR_IVRSystem_FnTable vrSystem;
    private static VR_IVRCompositor_FnTable vrCompositor;
    private static VR_IVROverlay_FnTable vrOverlay;
    private static VR_IVRSettings_FnTable vrSettings;
    private static int[] controllerDeviceIndex = new int[2];
    private static VRControllerState_t.ByReference[] inputStateRefernceArray = new VRControllerState_t.ByReference[2];
    private static VRControllerState_t[] controllerStateReference = new VRControllerState_t[2];
    private static IntBuffer hmdErrorStore;
    private static TrackedDevicePose_t.ByReference hmdTrackedDevicePoseReference;
    private static TrackedDevicePose_t[] hmdTrackedDevicePoses;
    private static boolean[] controllerTracking = new boolean[2];

    //keyboard
    private static boolean keyboardShowing;
    private static boolean headIsTracking;

    public OpenVRState vrState = new OpenVRState();

    // TextureIDs of framebuffers for each eye
    private final VRTextureBounds_t texBounds = new VRTextureBounds_t();
    private float nearClip = 0.5f;
    private float farClip = 500.0f;
    private static OpenVRProvider instance = null;

    // Get a singleton instance.
    /*
    * As a general rule, we should use this class as a singleton, because multiple instantiation
    * will likely cause problems in the upstream native library. This provides a convenient method
    * of using OpenVRProvider as a singleton.
     */
    public static OpenVRProvider getInstance() {
        if (instance == null)
            instance = new OpenVRProvider();
        return instance;
    }

    public OpenVRProvider() {
        for (int handIndex = 0; handIndex < 2; handIndex++) {
            controllerDeviceIndex[handIndex] = -1;
            controllerStateReference[handIndex] = new VRControllerState_t();
            inputStateRefernceArray[handIndex] = new VRControllerState_t.ByReference();
            inputStateRefernceArray[handIndex].setAutoRead(false);
            inputStateRefernceArray[handIndex].setAutoWrite(false);
            inputStateRefernceArray[handIndex].setAutoSynch(false);
            texType[handIndex] = new Texture_t();
        }
    }

    public boolean init() {
        if (!initializeOpenVRLibrary()) {
            logger.warn("JOpenVR library loading failed.");
            return false;
        }
        if (!initializeJOpenVR()) {
            logger.warn("JOpenVR initialization failed.");
            return false;
        }
        int initAttempts = 0;
        boolean initSuccess = false;

        // OpenVR has a race condition here - it is necessary<commit_msg>Checkstyle, made some changes to improve adherence to classic Java singleton pattern.<commit_after>/*
 * Copyright 2016 MovingBlocks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.terasology.rendering.openvrprovider;

import com.sun.jna.Memory;
import com.sun.jna.NativeLibrary;
import com.sun.jna.Pointer;
import jopenvr.HmdMatrix34_t;
import jopenvr.HmdMatrix44_t;
import jopenvr.JOpenVRLibrary;
import jopenvr.JOpenVRLibrary.EVREventType;
import jopenvr.Texture_t;
import jopenvr.TrackedDevicePose_t;
import jopenvr.VRControllerState_t;
import jopenvr.VRTextureBounds_t;
import jopenvr.VR_IVRCompositor_FnTable;
import jopenvr.VR_IVROverlay_FnTable;
import jopenvr.VR_IVRSettings_FnTable;
import jopenvr.VR_IVRSystem_FnTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.utilities.NativeHelper;

import java.nio.IntBuffer;

import static org.terasology.rendering.openvrprovider.ControllerListener.LEFT_CONTROLLER;
import static org.terasology.rendering.openvrprovider.ControllerListener.RIGHT_CONTROLLER;

/** This class is designed to make all API calls to OpenVR, thereby insulating it from the user. If you're looking to get
 * some information from the headset/controllers you should probably look at OpenVRStereoRenderer, ControllerListener,
 * or OpenVRState
 */
public final class OpenVRProvider {
    public static Texture_t[] texType = new Texture_t[2];

    private static boolean initialized;
    private static final Logger logger = LoggerFactory.getLogger(OpenVRProvider.class);
    private static VR_IVRSystem_FnTable vrSystem;
    private static VR_IVRCompositor_FnTable vrCompositor;
    private static VR_IVROverlay_FnTable vrOverlay;
    private static VR_IVRSettings_FnTable vrSettings;
    private static int[] controllerDeviceIndex = new int[2];
    private static VRControllerState_t.ByReference[] inputStateRefernceArray = new VRControllerState_t.ByReference[2];
    private static VRControllerState_t[] controllerStateReference = new VRControllerState_t[2];
    private static IntBuffer hmdErrorStore;
    private static TrackedDevicePose_t.ByReference hmdTrackedDevicePoseReference;
    private static TrackedDevicePose_t[] hmdTrackedDevicePoses;
    private static boolean[] controllerTracking = new boolean[2];

    //keyboard
    private static boolean keyboardShowing;
    private static boolean headIsTracking;
    private static OpenVRProvider instance;

    public OpenVRState vrState = new OpenVRState();

    // TextureIDs of framebuffers for each eye
    private final VRTextureBounds_t texBounds = new VRTextureBounds_t();
    private float nearClip = 0.5f;
    private float farClip = 500.0f;

    // Call getInstance() to instantiate
    private OpenVRProvider() {
    }

    // Get a singleton instance.
    /*
    * As a general rule, we should use this class as a singleton, because multiple instantiation
    * will likely cause problems in the upstream native library. This provides a convenient method
    * of using OpenVRProvider as a singleton.
     */
    public static OpenVRProvider getInstance() {
        if (instance == null) {
            instance = new OpenVRProvider();
        }
        return instance;
    }

    public boolean init() {
        for (int handIndex = 0; handIndex < 2; handIndex++) {
            controllerDeviceIndex[handIndex] = -1;
            controllerStateReference[handIndex] = new VRControllerState_t();
            inputStateRefernceArray[handIndex] = new VRControllerState_t.ByReference();
            inputStateRefernceArray[handIndex].setAutoRead(false);
            inputStateRefernceArray[handIndex].setAutoWrite(false);
            inputStateRefernceArray[handIndex].setAutoSynch(false);
            texType[handIndex] = new Texture_t();
        }
        if (!initializeOpenVRLibrary()) {
            logger.warn("JOpenVR library loading failed.");
            return false;
        }
        if (!initializeJOpenVR()) {
            logger.warn("JOpenVR initialization failed.");
            return false;
        }
        int initAttempts = 0;
        boolean initSuccess = false;

        // OpenVR has a race condition here - it is necessary
{'fraction_non_alphanumeric': 0.06932614555256064, 'fraction_numerical': 0.006253369272237197, 'mean_word_length': 3.874408828166054, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7253075', 'n_tokens_mistral': 2621, 'n_tokens_neox': 2530, 'n_words': 776}