/*
 * Copyright 2025 The Android Open Source Project
 *
 * 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.
 */

#include <input/InputFlags.h>

#include <android-base/logging.h>
#include <com_android_input_flags.h>
#include <com_android_window_flags.h>
#include <cutils/properties.h>

#include <string>

namespace android {

namespace {

// Returns the cached dev option value if available.
// This check is only required for connected-displays related features.
std::optional<bool> getConnectedDisplaysDevOptionValue() {
    if (!com::android::window::flags::show_desktop_experience_dev_option()) {
        return std::nullopt;
    }
    static std::optional<bool> cachedDevOption;
    if (!cachedDevOption.has_value()) {
        char value[PROPERTY_VALUE_MAX];
        constexpr static auto sysprop_name = "persist.wm.debug.desktop_experience_devopts";
        const int devOptionEnabled =
                property_get(sysprop_name, value, nullptr) > 0 ? std::atoi(value) : 0;
        cachedDevOption = devOptionEnabled == 1;
    }
    return cachedDevOption;
}

} // namespace

bool InputFlags::connectedDisplaysCursorEnabled() {
    if (getConnectedDisplaysDevOptionValue().value_or(false)) {
        return true;
    }
    return com::android::input::flags::connected_displays_cursor();
}

bool InputFlags::connectedDisplaysCursorAndAssociatedDisplayCursorBugfixEnabled() {
    if (getConnectedDisplaysDevOptionValue().value_or(false)) {
        return true;
    }
    return connectedDisplaysCursorEnabled() &&
            com::android::input::flags::connected_displays_associated_display_cursor_bugfix();
}

bool InputFlags::scaleCursorSpeedWithDisplayDensity() {
    if (getConnectedDisplaysDevOptionValue().value_or(false)) {
        return true;
    }
    return com::android::input::flags::scale_cursor_speed_with_dpi();
}

} // namespace android
