# --- sloposk_core: everything that runs without a compositor; also the "Sloposk" QML module.

qt_add_library(sloposk_core STATIC
    backend/TextBackend.h
    backend/TextContext.h
    engine/InputEngine.h
    engine/DirectEngine.h engine/DirectEngine.cpp
    input/ShiftState.h input/ShiftState.cpp
    input/PointerTracker.h input/PointerTracker.cpp
    input/AlternatesPicker.h input/AlternatesPicker.cpp
    input/KeyRepeater.h input/KeyRepeater.cpp
    input/TextRules.h input/TextRules.cpp
    input/KeyboardController.h input/KeyboardController.cpp
    input/KeyboardSurface.h input/KeyboardSurface.cpp
    layout/Layout.h
    layout/LayoutLoader.h layout/LayoutLoader.cpp
    layout/LayoutRepository.h layout/LayoutRepository.cpp
    layout/KeyGeometry.h layout/KeyGeometry.cpp
    layout/LayoutModel.h layout/LayoutModel.cpp
    window/VisibilityController.h window/VisibilityController.cpp
    window/WindowSettings.h window/WindowSettings.cpp
)

# QML lives in the top-level qml/ dir; alias each file so the module sees it at its root.
set(SLOPOSK_QML_FILES
    Main.qml
    Keyboard.qml
    Key.qml
    AlternatesPopup.qml
    DragHandle.qml
    ResizeHandle.qml
    Theme.qml
)
list(TRANSFORM SLOPOSK_QML_FILES PREPEND ${PROJECT_SOURCE_DIR}/qml/ OUTPUT_VARIABLE sloposk_qml_paths)
foreach(file IN LISTS SLOPOSK_QML_FILES)
    set_source_files_properties(${PROJECT_SOURCE_DIR}/qml/${file} PROPERTIES QT_RESOURCE_ALIAS ${file})
endforeach()
set_source_files_properties(${PROJECT_SOURCE_DIR}/qml/Theme.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)

qt_add_qml_module(sloposk_core
    URI Sloposk
    OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/qml/Sloposk
    QML_FILES ${sloposk_qml_paths}
)

file(GLOB layout_files CONFIGURE_DEPENDS ${PROJECT_SOURCE_DIR}/layouts/*.json)
qt_add_resources(sloposk_core "layouts"
    PREFIX /layouts
    BASE ${PROJECT_SOURCE_DIR}/layouts
    FILES ${layout_files}
)

target_include_directories(sloposk_core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
# The generated QML type registration includes QML_ELEMENT headers by bare name.
target_include_directories(sloposk_core PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/input
    ${CMAKE_CURRENT_SOURCE_DIR}/layout
)
target_link_libraries(sloposk_core PUBLIC Qt6::Quick PkgConfig::XKB)

# --- sloposk: the app, with the compositor-specific parts.

qt_add_executable(sloposk
    main.cpp
    app/DevHooks.h app/DevHooks.cpp
    app/DBusControl.h app/DBusControl.cpp
    backend/InputMethodProtocol.h
    backend/KWinInputMethod.h backend/KWinInputMethod.cpp
    backend/KWinInputContext.h backend/KWinInputContext.cpp
    window/FloatingWindow.h window/FloatingWindow.cpp
)

qt6_generate_wayland_protocol_client_sources(sloposk PRIVATE_CODE FILES
    ${WAYLAND_PROTOCOLS_DIR}/unstable/input-method/input-method-unstable-v1.xml
)
# Generated code lacks the core-protocol include (see backend/InputMethodProtocol.h).
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/qwayland-input-method-unstable-v1.cpp
    PROPERTIES COMPILE_OPTIONS "-include;wayland-client-protocol.h")

target_link_libraries(sloposk PRIVATE
    sloposk_core
    sloposk_coreplugin
    Qt6::DBus
    Qt6::WaylandClient
    LayerShellQt::Interface
    PkgConfig::WAYLAND
)

install(TARGETS sloposk)
install(FILES ${PROJECT_SOURCE_DIR}/data/org.sloposk.keyboard.desktop DESTINATION ${CMAKE_INSTALL_DATADIR}/applications)
