xcursor.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 (data->tex
  39. && data->last_height == xc->width
  40. && data->last_width == xc->height) {
  41. texture_setimage(data->tex, (void **) pixels,
  42. xc->width * sizeof(uint32_t), False);
  43. } else {
  44. if (data->tex)
  45. texture_destroy(data->tex);
  46. data->tex = gs_create_texture(xc->width, xc->height,
  47. GS_RGBA, 1, (const void **) &pixels, GS_DYNAMIC);
  48. }
  49. bfree(pixels);
  50. data->last_serial = xc->cursor_serial;
  51. data->last_width = xc->width;
  52. data->last_height = xc->height;
  53. }
  54. xcursor_t *xcursor_init(Display *dpy) {
  55. xcursor_t *data = bmalloc(sizeof(xcursor_t));
  56. memset(data, 0, sizeof(xcursor_t));
  57. data->dpy = dpy;
  58. xcursor_tick(data);
  59. return data;
  60. }
  61. void xcursor_destroy(xcursor_t *data) {
  62. if (data->tex)
  63. texture_destroy(data->tex);
  64. bfree(data);
  65. }
  66. void xcursor_tick(xcursor_t *data) {
  67. XFixesCursorImage *xc = XFixesGetCursorImage(data->dpy);
  68. if (!data->tex || data->last_serial != xc->cursor_serial)
  69. xcursor_create(data, xc);
  70. data->pos_x = -1.0 * (xc->x - xc->xhot);
  71. data->pos_y = -1.0 * (xc->y - xc->yhot);
  72. XFree(xc);
  73. }
  74. void xcursor_render(xcursor_t *data) {
  75. /* TODO: why do i need effects ? */
  76. effect_t effect = gs_geteffect();
  77. eparam_t image = effect_getparambyname(effect, "image");
  78. effect_settexture(effect, image, data->tex);
  79. gs_matrix_push();
  80. gs_matrix_translate3f(data->pos_x, data->pos_y, 0);
  81. gs_enable_blending(True);
  82. gs_blendfunction(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  83. gs_draw_sprite(data->tex, 0, 0, 0);
  84. gs_enable_blending(False);
  85. gs_matrix_pop();
  86. }