xcursor.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. uint_fast32_t size = xc->width * xc->height;
  27. uint32_t *pixels = bmalloc(size * sizeof(uint32_t));
  28. for (uint_fast32_t i = 0; i < size; ++i)
  29. pixels[i] = (uint32_t) xc->pixels[i];
  30. return pixels;
  31. }
  32. /*
  33. * Create the cursor texture, either by updating if the new cursor has the same
  34. * size or by creating a new texture if the size is different
  35. */
  36. static void xcursor_create(xcursor_t *data, XFixesCursorImage *xc) {
  37. uint32_t *pixels = xcursor_pixels(xc);
  38. if (!pixels)
  39. return;
  40. if (data->tex
  41. && data->last_height == xc->width
  42. && data->last_width == xc->height) {
  43. gs_texture_set_image(data->tex, (const uint8_t *) pixels,
  44. xc->width * sizeof(uint32_t), False);
  45. } else {
  46. if (data->tex)
  47. gs_texture_destroy(data->tex);
  48. data->tex = gs_texture_create(xc->width, xc->height,
  49. GS_BGRA, 1, (const uint8_t **) &pixels, GS_DYNAMIC);
  50. }
  51. bfree(pixels);
  52. data->last_serial = xc->cursor_serial;
  53. data->last_width = xc->width;
  54. data->last_height = xc->height;
  55. }
  56. xcursor_t *xcursor_init(Display *dpy) {
  57. xcursor_t *data = bzalloc(sizeof(xcursor_t));
  58. data->dpy = dpy;
  59. xcursor_tick(data);
  60. return data;
  61. }
  62. void xcursor_destroy(xcursor_t *data) {
  63. if (data->tex)
  64. gs_texture_destroy(data->tex);
  65. bfree(data);
  66. }
  67. void xcursor_tick(xcursor_t *data) {
  68. XFixesCursorImage *xc = XFixesGetCursorImage(data->dpy);
  69. if (!xc)
  70. return;
  71. if (!data->tex || data->last_serial != xc->cursor_serial)
  72. xcursor_create(data, xc);
  73. data->x = (int_fast32_t)xc->x - (int_fast32_t)data->x_org;
  74. data->y = (int_fast32_t)xc->y - (int_fast32_t)data->y_org;
  75. data->render_x = xc->x - xc->xhot - data->x_org;
  76. data->render_y = xc->y - xc->yhot - data->y_org;
  77. XFree(xc);
  78. }
  79. void xcursor_render(xcursor_t *data) {
  80. if (!data->tex)
  81. return;
  82. gs_effect_t *effect = gs_get_effect();
  83. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  84. gs_effect_set_texture(image, data->tex);
  85. gs_blend_state_push();
  86. gs_blend_function(GS_BLEND_SRCALPHA, GS_BLEND_INVSRCALPHA);
  87. gs_enable_color(true, true, true, false);
  88. gs_matrix_push();
  89. gs_matrix_translate3f(data->render_x, data->render_y, 0.0f);
  90. gs_draw_sprite(data->tex, 0, 0, 0);
  91. gs_matrix_pop();
  92. gs_enable_color(true, true, true, true);
  93. gs_blend_state_pop();
  94. }
  95. void xcursor_offset(xcursor_t* data, int_fast32_t x_org, int_fast32_t y_org)
  96. {
  97. data->x_org = x_org;
  98. data->y_org = y_org;
  99. }