xcursor.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. Copyright (C) 2014 by Leonhard Oelke <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdint.h>
  15. #include <X11/extensions/Xfixes.h>
  16. #include <util/bmem.h>
  17. #include "xcursor.h"
  18. /*
  19. * Get pixel data for the cursor
  20. *
  21. * XFixes has the data defined as unsigned long, so we can not use memcpy.
  22. * Theres a lot of talk about this in other implementation and they tend to
  23. * be really complicated, but this naive approach seems to work fine ...
  24. */
  25. static uint32_t *xcursor_pixels(XFixesCursorImage *xc)
  26. {
  27. uint_fast32_t size = xc->width * xc->height;
  28. uint32_t *pixels = bmalloc(size * sizeof(uint32_t));
  29. for (uint_fast32_t i = 0; i < size; ++i)
  30. pixels[i] = (uint32_t)xc->pixels[i];
  31. return pixels;
  32. }
  33. /*
  34. * Create the cursor texture, either by updating if the new cursor has the same
  35. * size or by creating a new texture if the size is different
  36. */
  37. static void xcursor_create(xcursor_t *data, XFixesCursorImage *xc)
  38. {
  39. uint32_t *pixels = xcursor_pixels(xc);
  40. if (!pixels)
  41. return;
  42. if (data->tex && data->last_height == xc->width &&
  43. data->last_width == xc->height) {
  44. gs_texture_set_image(data->tex, (const uint8_t *)pixels,
  45. xc->width * sizeof(uint32_t), False);
  46. } else {
  47. if (data->tex)
  48. gs_texture_destroy(data->tex);
  49. data->tex = gs_texture_create(xc->width, xc->height, GS_BGRA, 1,
  50. (const uint8_t **)&pixels,
  51. GS_DYNAMIC);
  52. }
  53. bfree(pixels);
  54. data->last_serial = xc->cursor_serial;
  55. data->last_width = xc->width;
  56. data->last_height = xc->height;
  57. }
  58. xcursor_t *xcursor_init(Display *dpy)
  59. {
  60. xcursor_t *data = bzalloc(sizeof(xcursor_t));
  61. data->dpy = dpy;
  62. xcursor_tick(data);
  63. return data;
  64. }
  65. void xcursor_destroy(xcursor_t *data)
  66. {
  67. if (data->tex)
  68. gs_texture_destroy(data->tex);
  69. bfree(data);
  70. }
  71. void xcursor_tick(xcursor_t *data)
  72. {
  73. XFixesCursorImage *xc = XFixesGetCursorImage(data->dpy);
  74. if (!xc)
  75. return;
  76. if (!data->tex || data->last_serial != xc->cursor_serial)
  77. xcursor_create(data, xc);
  78. data->x = (int_fast32_t)xc->x - (int_fast32_t)data->x_org;
  79. data->y = (int_fast32_t)xc->y - (int_fast32_t)data->y_org;
  80. data->render_x = xc->x - xc->xhot - data->x_org;
  81. data->render_y = xc->y - xc->yhot - data->y_org;
  82. XFree(xc);
  83. }
  84. void xcursor_render(xcursor_t *data, int x_offset, int y_offset)
  85. {
  86. if (!data->tex)
  87. return;
  88. const bool linear_srgb = gs_get_linear_srgb();
  89. const bool previous = gs_framebuffer_srgb_enabled();
  90. gs_enable_framebuffer_srgb(linear_srgb);
  91. gs_effect_t *effect = gs_get_effect();
  92. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  93. if (linear_srgb)
  94. gs_effect_set_texture_srgb(image, data->tex);
  95. else
  96. gs_effect_set_texture(image, data->tex);
  97. gs_blend_state_push();
  98. gs_blend_function(GS_BLEND_SRCALPHA, GS_BLEND_INVSRCALPHA);
  99. gs_enable_color(true, true, true, false);
  100. gs_matrix_push();
  101. gs_matrix_translate3f(data->render_x + x_offset,
  102. data->render_y + y_offset, 0.0f);
  103. gs_draw_sprite(data->tex, 0, 0, 0);
  104. gs_matrix_pop();
  105. gs_enable_color(true, true, true, true);
  106. gs_blend_state_pop();
  107. gs_enable_framebuffer_srgb(previous);
  108. }
  109. void xcursor_offset(xcursor_t *data, int_fast32_t x_org, int_fast32_t y_org)
  110. {
  111. data->x_org = x_org;
  112. data->y_org = y_org;
  113. }