xcursor.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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, (const uint8_t *) 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_BGRA, 1, (const uint8_t **) &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 = bzalloc(sizeof(xcursor_t));
  56. data->dpy = dpy;
  57. xcursor_tick(data);
  58. return data;
  59. }
  60. void xcursor_destroy(xcursor_t *data) {
  61. if (data->tex)
  62. texture_destroy(data->tex);
  63. bfree(data);
  64. }
  65. void xcursor_tick(xcursor_t *data) {
  66. XFixesCursorImage *xc = XFixesGetCursorImage(data->dpy);
  67. if (!data->tex || data->last_serial != xc->cursor_serial)
  68. xcursor_create(data, xc);
  69. data->pos_x = -1.0 * (xc->x - xc->xhot - data->x_org);
  70. data->pos_y = -1.0 * (xc->y - xc->yhot - data->y_org);
  71. XFree(xc);
  72. }
  73. void xcursor_render(xcursor_t *data) {
  74. /* TODO: why do i need effects ? */
  75. effect_t effect = gs_geteffect();
  76. eparam_t image = effect_getparambyname(effect, "image");
  77. effect_settexture(image, data->tex);
  78. gs_matrix_push();
  79. gs_matrix_translate3f(-data->pos_x, -data->pos_y, 0);
  80. gs_enable_blending(True);
  81. gs_blendfunction(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  82. gs_draw_sprite(data->tex, 0, 0, 0);
  83. gs_matrix_pop();
  84. }
  85. void xcursor_offset(xcursor_t* data, int_fast32_t x_org, int_fast32_t y_org)
  86. {
  87. data->x_org = x_org;
  88. data->y_org = y_org;
  89. }