tests: add C tests and fixup api

This commit is contained in:
Vaxry 2024-03-07 15:10:45 +00:00
parent 6023672759
commit 953f2fb118
6 changed files with 103 additions and 7 deletions

View file

@ -61,9 +61,14 @@ add_custom_target(tests)
add_executable(hyprcursor_test "tests/test.cpp") add_executable(hyprcursor_test "tests/test.cpp")
target_link_libraries(hyprcursor_test PRIVATE hyprcursor) 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_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 # Installation
install(TARGETS hyprcursor install(TARGETS hyprcursor
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}

View file

@ -12,8 +12,21 @@
#endif #endif
#include <cairo/cairo.h>
struct hyprcursor_manager_t; 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. Basic Hyprcursor manager.
@ -28,17 +41,37 @@ struct hyprcursor_manager_t;
The caller gets the ownership, call hyprcursor_manager_free to free this object. 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* 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, Returns true if the theme was successfully loaded,
i.e. everything is A-OK and nothing should fail. 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 #endif

View file

@ -10,7 +10,7 @@ namespace Hyprcursor {
Simple struct for styles Simple struct for styles
*/ */
struct SCursorStyleInfo { struct SCursorStyleInfo {
/* /*!
Shape size. Shape size.
0 means "any" or "unspecified". 0 means "any" or "unspecified".
@ -43,6 +43,8 @@ namespace Hyprcursor {
/*! /*!
Loads this theme at a given style, synchronously. Loads this theme at a given style, synchronously.
Returns whether it succeeded.
*/ */
bool loadThemeStyle(const SCursorStyleInfo& info); bool loadThemeStyle(const SCursorStyleInfo& info);

View file

@ -11,7 +11,28 @@ void hyprcursor_manager_free(hyprcursor_manager_t* manager) {
delete (CHyprcursorManager*)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; const auto MGR = (CHyprcursorManager*)manager;
return MGR->valid(); 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);
}

33
tests/test.c Normal file
View file

@ -0,0 +1,33 @@
#include <hyprcursor.h>
#include <stdio.h>
#include <stdlib.h>
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;
}

View file

@ -1,6 +1,5 @@
#include <iostream> #include <iostream>
#include <hyprcursor.hpp> #include <hyprcursor.hpp>
#include <cairo/cairo.h>
int main(int argc, char** argv) { int main(int argc, char** argv) {
Hyprcursor::CHyprcursorManager mgr(nullptr); Hyprcursor::CHyprcursorManager mgr(nullptr);
@ -19,5 +18,8 @@ int main(int argc, char** argv) {
std::cout << "Cairo returned for write: " << RET << "\n"; std::cout << "Cairo returned for write: " << RET << "\n";
if (RET)
return 1;
return !mgr.valid(); return !mgr.valid();
} }