# The KDE frontend: plain Qt6 Widgets.
#
# Deliberately NOT KDE Frameworks. It picks up Breeze through the platform
# theme anyway, and staying framework-free keeps this frontend usable outside
# a KDE session -- and keeps the door open to reusing it elsewhere.
#
# Found-or-skipped, like the GNOME one: a machine without Qt6 development
# packages still builds the core, the tests and the other frontend.

# Scoped to this directory: the project is C, and this frontend is the only
# C++ in it. It has to come BEFORE find_package(Qt6), not after -- Qt's
# config runs check_cxx_source_compiles (for WrapAtomic among others), which
# fails outright if no C++ compiler has been enabled yet.
enable_language(CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)

# Qt 6.7 for QRhiWidget (the GPU display); ShaderTools bakes its shaders.
find_package(Qt6 6.7 COMPONENTS Widgets GuiPrivate ShaderTools QUIET)
find_package(Qt6 6.7 COMPONENTS Test QUIET)
if(NOT Qt6_FOUND)
  message(STATUS "kde frontend: Qt6 Widgets not found -- skipping")
  return()
endif()

add_executable(fujinet-go-coleco-kde
  main.cpp
  MainWindow.cpp
  DisplayWidget.cpp
  SoftDisplayWidget.cpp
  FrameFeed.cpp
  controllers/ControllersWindow.cpp
  KeyForward.cpp
  SettingsDialog.cpp
  FujiNetLogWindow.cpp
  help/HelpWindow.cpp
  debugger/DebuggerWindow.cpp
  debugger/VideoTab.cpp
)
target_link_libraries(fujinet-go-coleco-kde PRIVATE coleco_session Qt6::Widgets Qt6::GuiPrivate)
target_include_directories(fujinet-go-coleco-kde PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
# The display's shaders, baked for every backend and embedded as resources.
qt_add_shaders(fujinet-go-coleco-kde "display_shaders"
  PREFIX "/"
  FILES shaders/display.vert shaders/display.frag)
target_compile_options(fujinet-go-coleco-kde PRIVATE -Wall -Wextra)

# The Controllers window, offscreen, on an SDL virtual gamepad (no BIOS, no
# machine): needs the Qt Test module, so it is found-or-skipped too.
if(Qt6Test_FOUND)
  add_executable(controllers_test_kde
    controllers_test.cpp
    controllers/ControllersWindow.cpp
    KeyForward.cpp
    help/HelpWindow.cpp
    debugger/DebuggerWindow.cpp
    debugger/VideoTab.cpp
  )
  target_link_libraries(controllers_test_kde PRIVATE coleco_session Qt6::Widgets Qt6::Test Qt6::GuiPrivate)
  target_include_directories(controllers_test_kde PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
  add_test(NAME controllers_kde COMMAND controllers_test_kde)
  set_tests_properties(controllers_kde PROPERTIES
    SKIP_RETURN_CODE 77 TIMEOUT 60
    ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
endif()

# The Debugger window on the offscreen platform: the tabs and what needs no
# machine always, and with a machine (and the ColecoVision BIOS, which is not
# shippable: found through COLECO_BIOS_DIR or tools/roms) a NOP-padded program
# stepped through the window. Skips (77) only where Qt has no offscreen platform.
if(Qt6Test_FOUND)
  add_executable(debugger_test_kde
    debugger_test.cpp
    debugger/DebuggerWindow.cpp
    debugger/VideoTab.cpp
    help/HelpWindow.cpp
  )
  target_link_libraries(debugger_test_kde PRIVATE coleco_session Qt6::Widgets Qt6::Test Qt6::GuiPrivate)
  target_include_directories(debugger_test_kde PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
  target_compile_options(debugger_test_kde PRIVATE -Wall -Wextra)
  target_compile_definitions(debugger_test_kde PRIVATE
    COLECO_TEST_ROMS="${CMAKE_SOURCE_DIR}/tools/roms")
  add_test(NAME debugger_kde COMMAND debugger_test_kde)
  set_tests_properties(debugger_kde PROPERTIES
    SKIP_RETURN_CODE 77 TIMEOUT 240
    ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
endif()

install(TARGETS fujinet-go-coleco-kde RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

configure_file(data/online.fujinet.go.coleco.kde.desktop.in
               ${CMAKE_CURRENT_BINARY_DIR}/online.fujinet.go.coleco.kde.desktop
               @ONLY)
configure_file(data/online.fujinet.go.coleco.kde.metainfo.xml.in
               ${CMAKE_CURRENT_BINARY_DIR}/online.fujinet.go.coleco.kde.metainfo.xml
               @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/online.fujinet.go.coleco.kde.desktop
        DESTINATION ${CMAKE_INSTALL_DATADIR}/applications)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/online.fujinet.go.coleco.kde.metainfo.xml
        DESTINATION ${CMAKE_INSTALL_DATADIR}/metainfo)

find_program(DESKTOP_FILE_VALIDATE desktop-file-validate)
if(DESKTOP_FILE_VALIDATE)
  add_test(NAME desktop_file_kde
           COMMAND ${DESKTOP_FILE_VALIDATE}
                   ${CMAKE_CURRENT_BINARY_DIR}/online.fujinet.go.coleco.kde.desktop)
endif()
find_program(APPSTREAMCLI appstreamcli)
if(APPSTREAMCLI)
  add_test(NAME metainfo_kde
           COMMAND ${APPSTREAMCLI} validate --no-net --pedantic
                   ${CMAKE_CURRENT_BINARY_DIR}/online.fujinet.go.coleco.kde.metainfo.xml)
endif()
