wl_cursors.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Future devs supporting whatever Wayland protocol stabilizes for cursor selection: see _themeAdd.
  2. #include "internal.h"
  3. #include "linux_desktop_settings.h"
  4. #include <assert.h>
  5. #include <errno.h>
  6. #include <limits.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. static GLFWWLCursorThemes cursor_themes;
  10. static int
  11. pixels_from_scale(int scale) {
  12. int factor;
  13. const char* name;
  14. glfw_current_cursor_theme(&name, &factor);
  15. return factor * scale;
  16. }
  17. struct wl_cursor_theme*
  18. glfw_wlc_theme_for_scale(int scale) {
  19. for (size_t i = 0; i < cursor_themes.count; i++) {
  20. if (cursor_themes.themes[i].scale == scale) return cursor_themes.themes[i].theme;
  21. }
  22. if (cursor_themes.count >= cursor_themes.capacity) {
  23. cursor_themes.themes = realloc(cursor_themes.themes, sizeof(GLFWWLCursorTheme) * (cursor_themes.count + 16));
  24. if (!cursor_themes.themes) {
  25. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: Out of memory allocating space for cursor themes");
  26. return NULL;
  27. }
  28. cursor_themes.capacity = cursor_themes.count + 16;
  29. }
  30. int factor;
  31. const char* name;
  32. glfw_current_cursor_theme(&name, &factor);
  33. struct wl_cursor_theme *ans = wl_cursor_theme_load(name, pixels_from_scale(scale), _glfw.wl.shm);
  34. if (!ans) {
  35. _glfwInputError(
  36. GLFW_PLATFORM_ERROR, "Wayland: wl_cursor_theme_load failed at scale: %d pixels: %d",
  37. scale, pixels_from_scale(scale)
  38. );
  39. return NULL;
  40. }
  41. GLFWWLCursorTheme *theme = cursor_themes.themes + cursor_themes.count++;
  42. theme->scale = scale;
  43. theme->theme = ans;
  44. return ans;
  45. }
  46. void
  47. glfw_wlc_destroy(void) {
  48. for (size_t i = 0; i < cursor_themes.count; i++) {
  49. wl_cursor_theme_destroy(cursor_themes.themes[i].theme);
  50. }
  51. free(cursor_themes.themes);
  52. cursor_themes.themes = NULL; cursor_themes.capacity = 0; cursor_themes.count = 0;
  53. }