From b868c2e11d02ff5bf6bf0122db8864440a885768 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Sat, 6 Apr 2024 21:15:43 +0100 Subject: [PATCH] tests: better stuff --- CMakeLists.txt | 15 +++-- tests/{test.c => c_test.c} | 12 ++-- tests/{test.cpp => full_rendering.cpp} | 52 +++++++++++------ tests/only_metadata.cpp | 80 ++++++++++++++++++++++++++ 4 files changed, 131 insertions(+), 28 deletions(-) rename tests/{test.c => c_test.c} (97%) rename tests/{test.cpp => full_rendering.cpp} (53%) create mode 100644 tests/only_metadata.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2508d5f..f34b5d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,12 +65,17 @@ target_link_libraries(hyprcursor-util PkgConfig::deps hyprcursor) # tests add_custom_target(tests) -add_executable(hyprcursor_test "tests/test.cpp") -target_link_libraries(hyprcursor_test PRIVATE hyprcursor) -add_test(NAME "Test libhyprcursor in C++" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests COMMAND hyprcursor_test) -add_dependencies(tests hyprcursor_test) +add_executable(hyprcursor_test1 "tests/full_rendering.cpp") +target_link_libraries(hyprcursor_test1 PRIVATE hyprcursor) +add_test(NAME "Test libhyprcursor in C++ (full rendering)" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests COMMAND hyprcursor_test1) +add_dependencies(tests hyprcursor_test1) -add_executable(hyprcursor_test_c "tests/test.c") +add_executable(hyprcursor_test2 "tests/only_metadata.cpp") +target_link_libraries(hyprcursor_test2 PRIVATE hyprcursor) +add_test(NAME "Test libhyprcursor in C++ (only metadata)" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests COMMAND hyprcursor_test2) +add_dependencies(tests hyprcursor_test2) + +add_executable(hyprcursor_test_c "tests/c_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) diff --git a/tests/test.c b/tests/c_test.c similarity index 97% rename from tests/test.c rename to tests/c_test.c index 4d1fdb2..7561fd0 100644 --- a/tests/test.c +++ b/tests/c_test.c @@ -1,3 +1,10 @@ +/* + hyprlang-test in C. + Renders a cursor shape to /tmp at 48px + + For better explanations, see the cpp tests. +*/ + #include #include #include @@ -6,11 +13,6 @@ void logFunction(enum eHyprcursorLogLevel level, char* message) { printf("[hc] %s\n", message); } -/* - hyprlang-test in C. - Renders a cursor shape to /tmp at 48px -*/ - int main(int argc, char** argv) { struct hyprcursor_manager_t* mgr = hyprcursor_manager_create_with_logger(NULL, logFunction); diff --git a/tests/test.cpp b/tests/full_rendering.cpp similarity index 53% rename from tests/test.cpp rename to tests/full_rendering.cpp index 694c0ab..b784ea1 100644 --- a/tests/test.cpp +++ b/tests/full_rendering.cpp @@ -1,3 +1,11 @@ + +/* + full_rendering.cpp + + This example shows probably what you want to do. + Hyprcursor will render a left_ptr shape at 48x48px to a file called /tmp/arrow.png +*/ + #include #include @@ -5,40 +13,43 @@ void logFunction(enum eHyprcursorLogLevel level, char* message) { std::cout << "[hc] " << message << "\n"; } -/* - hyprlang-test in C++. - Renders a cursor shape to /tmp at 48px -*/ - int main(int argc, char** argv) { + /* + Create a manager. You can optionally pass a logger function. + */ Hyprcursor::CHyprcursorManager mgr(nullptr, logFunction); + /* + Manager could be invalid if no themes were found, or + a specified theme was invalid. + */ if (!mgr.valid()) { std::cout << "mgr is invalid\n"; return 1; } - // test raw data - const auto RAWDATA = mgr.getRawShapeData("left_ptr"); - if (RAWDATA.images.empty()) { - std::cout << "failed querying left_ptr\n"; - return 1; - } - - std::cout << "left_ptr images: " << RAWDATA.images.size() << "\n"; - for (auto& i : RAWDATA.images) - std::cout << "left_ptr data size: " << i.data.size() << "\n"; + /* + Style describes what pixel size you want your cursor + images to be. + Remember to free styles once you're done with them + (e.g. the user requested to change the cursor size to something else) + */ Hyprcursor::SCursorStyleInfo style{.size = 48}; - // preload size 48 for testing if (!mgr.loadThemeStyle(style)) { std::cout << "failed loading style\n"; return 1; } - // get cursor for left_ptr + /* + Get a shape. This will return the data about available image(s), + their delay, hotspot, etc. + */ const auto SHAPEDATA = mgr.getShape("left_ptr", style); + /* + If the size doesn't exist, images will be empty. + */ if (SHAPEDATA.images.empty()) { std::cout << "no images\n"; return 1; @@ -46,11 +57,16 @@ int main(int argc, char** argv) { std::cout << "hyprcursor returned " << SHAPEDATA.images.size() << " images\n"; - // save to disk + /* + Save to disk with cairo + */ const auto RET = cairo_surface_write_to_png(SHAPEDATA.images[0].surface, "/tmp/arrow.png"); std::cout << "Cairo returned for write: " << RET << "\n"; + /* + As mentioned before, clean up by releasing the style. + */ mgr.cursorSurfaceStyleDone(style); if (RET) diff --git a/tests/only_metadata.cpp b/tests/only_metadata.cpp new file mode 100644 index 0000000..2f73c77 --- /dev/null +++ b/tests/only_metadata.cpp @@ -0,0 +1,80 @@ + +/* + only_metadata.cpp + + This is a mode in which you probably do NOT want to operate, + but major DEs might want their own renderer for + cursor shapes. + + Prefer full_rendering.cpp for consistency and simplicity. +*/ + +#include +#include + +void logFunction(enum eHyprcursorLogLevel level, char* message) { + std::cout << "[hc] " << message << "\n"; +} + +int main(int argc, char** argv) { + /* + Create a manager. You can optionally pass a logger function. + */ + Hyprcursor::CHyprcursorManager mgr(nullptr, logFunction); + + /* + Manager could be invalid if no themes were found, or + a specified theme was invalid. + */ + if (!mgr.valid()) { + std::cout << "mgr is invalid\n"; + return 1; + } + + /* + If you are planning on using your own renderer, + you do not want to load in any styles, as those + are rendered once you make your call. + + Instead, let's request left_ptr's metadata + */ + auto RAWDATA = mgr.getRawShapeData("left_ptr"); + + /* + if images are empty, check overridenBy + */ + if (RAWDATA.images.empty()) { + + /* + if overridenBy is empty, the current theme doesn't have this shape. + */ + if (RAWDATA.overridenBy.empty()) + return false; + + /* + load what it's overriden by. + */ + RAWDATA = mgr.getRawShapeData(RAWDATA.overridenBy.c_str()); + } + + /* + If we still have no images, the theme seems broken. + */ + if (RAWDATA.images.empty()) { + std::cout << "failed querying left_ptr\n"; + return 1; + } + + /* + You can query the images (animation frames) + or their properties. + + Every image has .data and .type for you to handle. + */ + std::cout << "left_ptr images: " << RAWDATA.images.size() << "\n"; + for (auto& i : RAWDATA.images) + std::cout << "left_ptr data size: " << i.data.size() << "\n"; + + + return 0; +} \ No newline at end of file