From 953f2fb118a671eecc5a9c9edeec4ce886ddf947 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Thu, 7 Mar 2024 15:10:45 +0000 Subject: [PATCH] tests: add C tests and fixup api --- CMakeLists.txt | 7 +++++- include/hyprcursor.h | 39 +++++++++++++++++++++++++++++++--- include/hyprcursor.hpp | 4 +++- libhyprcursor/hyprcursor_c.cpp | 23 +++++++++++++++++++- tests/test.c | 33 ++++++++++++++++++++++++++++ tests/test.cpp | 4 +++- 6 files changed, 103 insertions(+), 7 deletions(-) create mode 100644 tests/test.c diff --git a/CMakeLists.txt b/CMakeLists.txt index d661b89..ac15a97 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,9 +61,14 @@ add_custom_target(tests) add_executable(hyprcursor_test "tests/test.cpp") target_link_libraries(hyprcursor_test PRIVATE hyprcursor) -add_test(NAME "Test libhyprcursor" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests COMMAND hyprcursor_test) +add_test(NAME "Test libhyprcursor in C++" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests COMMAND hyprcursor_test) add_dependencies(tests hyprcursor_test) +add_executable(hyprcursor_test_c "tests/test.c") +target_link_libraries(hyprcursor_test_c PRIVATE hyprcursor) +add_test(NAME "Test libhyprcursor in C" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests COMMAND hyprcursor_test_c) +add_dependencies(tests hyprcursor_test_c) + # Installation install(TARGETS hyprcursor PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} diff --git a/include/hyprcursor.h b/include/hyprcursor.h index 1efaec1..7c9e5b7 100644 --- a/include/hyprcursor.h +++ b/include/hyprcursor.h @@ -12,8 +12,21 @@ #endif +#include + struct hyprcursor_manager_t; +/*! + Simple struct for styles +*/ +struct hyprcursor_cursor_style_info { + /*! + Shape size. + 0 means "any" or "unspecified". + */ + unsigned int size; +}; + /*! Basic Hyprcursor manager. @@ -28,17 +41,37 @@ struct hyprcursor_manager_t; The caller gets the ownership, call hyprcursor_manager_free to free this object. */ -CAPI hyprcursor_manager_t* hyprcursor_manager_create(const char* theme_name); +CAPI struct hyprcursor_manager_t* hyprcursor_manager_create(const char* theme_name); /*! Free a hyprcursor_manager_t* */ -CAPI void hyprcursor_manager_free(hyprcursor_manager_t* manager); +CAPI void hyprcursor_manager_free(struct hyprcursor_manager_t* manager); /*! Returns true if the theme was successfully loaded, i.e. everything is A-OK and nothing should fail. */ -CAPI bool hyprcursor_manager_valid(hyprcursor_manager_t* manager); +CAPI int hyprcursor_manager_valid(struct hyprcursor_manager_t* manager); + +/*! + Loads a theme at a given style, synchronously. + + Returns whether it succeeded. +*/ +CAPI int hyprcursor_load_theme_style(struct hyprcursor_manager_t* manager, struct hyprcursor_cursor_style_info info); + +/*! + Returns a cairo_surface_t for a given cursor + shape and size. + + Once done with a size, call hyprcursor_style_done() +*/ +CAPI cairo_surface_t* hyprcursor_get_surface_for(struct hyprcursor_manager_t* manager, const char* shape, struct hyprcursor_cursor_style_info info); + +/*! + Marks a certain style as done, allowing it to be potentially freed +*/ +CAPI void hyprcursor_style_done(struct hyprcursor_manager_t* manager, struct hyprcursor_cursor_style_info info); #endif \ No newline at end of file diff --git a/include/hyprcursor.hpp b/include/hyprcursor.hpp index b3652e4..87d6525 100644 --- a/include/hyprcursor.hpp +++ b/include/hyprcursor.hpp @@ -10,7 +10,7 @@ namespace Hyprcursor { Simple struct for styles */ struct SCursorStyleInfo { - /* + /*! Shape size. 0 means "any" or "unspecified". @@ -43,6 +43,8 @@ namespace Hyprcursor { /*! Loads this theme at a given style, synchronously. + + Returns whether it succeeded. */ bool loadThemeStyle(const SCursorStyleInfo& info); diff --git a/libhyprcursor/hyprcursor_c.cpp b/libhyprcursor/hyprcursor_c.cpp index a5ba8f8..9b00348 100644 --- a/libhyprcursor/hyprcursor_c.cpp +++ b/libhyprcursor/hyprcursor_c.cpp @@ -11,7 +11,28 @@ void hyprcursor_manager_free(hyprcursor_manager_t* manager) { delete (CHyprcursorManager*)manager; } -bool hyprcursor_manager_valid(hyprcursor_manager_t* manager) { +int hyprcursor_manager_valid(hyprcursor_manager_t* manager) { const auto MGR = (CHyprcursorManager*)manager; return MGR->valid(); +} + +int hyprcursor_load_theme_style(hyprcursor_manager_t* manager, hyprcursor_cursor_style_info info_) { + const auto MGR = (CHyprcursorManager*)manager; + SCursorStyleInfo info; + info.size = info_.size; + return MGR->loadThemeStyle(info); +} + +cairo_surface_t* hyprcursor_get_surface_for(hyprcursor_manager_t* manager, const char* shape, hyprcursor_cursor_style_info info_) { + const auto MGR = (CHyprcursorManager*)manager; + SCursorStyleInfo info; + info.size = info_.size; + return MGR->getSurfaceFor(shape, info); +} + +void hyprcursor_style_done(hyprcursor_manager_t* manager, hyprcursor_cursor_style_info info_) { + const auto MGR = (CHyprcursorManager*)manager; + SCursorStyleInfo info; + info.size = info_.size; + return MGR->cursorSurfaceStyleDone(info); } \ No newline at end of file diff --git a/tests/test.c b/tests/test.c new file mode 100644 index 0000000..3f75704 --- /dev/null +++ b/tests/test.c @@ -0,0 +1,33 @@ +#include +#include +#include + +int main(int argc, char** argv) { + struct hyprcursor_manager_t* mgr = hyprcursor_manager_create(NULL); + + if (!mgr) { + printf("mgr null\n"); + return 1; + } + + struct hyprcursor_cursor_style_info info = {.size = 48}; + if (!hyprcursor_load_theme_style(mgr, info)) { + printf("load failed\n"); + return 1; + } + + cairo_surface_t* surf = hyprcursor_get_surface_for(mgr, "left_ptr", info); + if (surf == NULL) { + printf("surf failed\n"); + return 1; + } + + int ret = cairo_surface_write_to_png(surf, "/tmp/arrowC.png"); + + if (ret) { + printf("cairo failed\n"); + return 1; + } + + return 0; +} \ No newline at end of file diff --git a/tests/test.cpp b/tests/test.cpp index 3d1372d..84cee19 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -1,6 +1,5 @@ #include #include -#include int main(int argc, char** argv) { Hyprcursor::CHyprcursorManager mgr(nullptr); @@ -19,5 +18,8 @@ int main(int argc, char** argv) { std::cout << "Cairo returned for write: " << RET << "\n"; + if (RET) + return 1; + return !mgr.valid(); } \ No newline at end of file