1
0

xcursor-xcb.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 <xcb/xfixes.h>
  16. #include <util/bmem.h>
  17. #include "xcursor-xcb.h"
  18. /*
  19. * Create the cursor texture, either by updating if the new cursor has the same
  20. * size or by creating a new texture if the size is different
  21. */
  22. static void xcb_xcursor_create(xcb_xcursor_t *data,
  23. xcb_xfixes_get_cursor_image_reply_t *xc)
  24. {
  25. uint32_t *pixels = xcb_xfixes_get_cursor_image_cursor_image(xc);
  26. if (!pixels)
  27. return;
  28. if (data->tex && data->last_height == xc->width &&
  29. data->last_width == xc->height) {
  30. gs_texture_set_image(data->tex, (const uint8_t *)pixels,
  31. xc->width * sizeof(uint32_t), false);
  32. } else {
  33. if (data->tex)
  34. gs_texture_destroy(data->tex);
  35. data->tex = gs_texture_create(xc->width, xc->height, GS_BGRA, 1,
  36. (const uint8_t **)&pixels,
  37. GS_DYNAMIC);
  38. }
  39. data->last_serial = xc->cursor_serial;
  40. data->last_width = xc->width;
  41. data->last_height = xc->height;
  42. }
  43. /**
  44. * We need to check for the xfixes version in order to initialize it ?
  45. */
  46. xcb_xcursor_t *xcb_xcursor_init(xcb_connection_t *xcb)
  47. {
  48. xcb_xcursor_t *data = bzalloc(sizeof(xcb_xcursor_t));
  49. xcb_xfixes_query_version_cookie_t xfix_c;
  50. xfix_c = xcb_xfixes_query_version_unchecked(
  51. xcb, XCB_XFIXES_MAJOR_VERSION, XCB_XFIXES_MINOR_VERSION);
  52. free(xcb_xfixes_query_version_reply(xcb, xfix_c, NULL));
  53. return data;
  54. }
  55. void xcb_xcursor_destroy(xcb_xcursor_t *data)
  56. {
  57. if (data->tex)
  58. gs_texture_destroy(data->tex);
  59. bfree(data);
  60. }
  61. void xcb_xcursor_update(xcb_xcursor_t *data,
  62. xcb_xfixes_get_cursor_image_reply_t *xc)
  63. {
  64. if (!data || !xc)
  65. return;
  66. if (!data->tex || data->last_serial != xc->cursor_serial)
  67. xcb_xcursor_create(data, xc);
  68. data->x = xc->x - data->x_org;
  69. data->y = xc->y - data->y_org;
  70. data->x_render = data->x - xc->xhot;
  71. data->y_render = data->y - xc->yhot;
  72. }
  73. void xcb_xcursor_render(xcb_xcursor_t *data)
  74. {
  75. if (!data->tex)
  76. return;
  77. gs_effect_t *effect = gs_get_effect();
  78. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  79. gs_effect_set_texture(image, data->tex);
  80. gs_blend_state_push();
  81. gs_blend_function(GS_BLEND_SRCALPHA, GS_BLEND_INVSRCALPHA);
  82. gs_enable_color(true, true, true, false);
  83. gs_matrix_push();
  84. gs_matrix_translate3f(data->x_render, data->y_render, 0.0f);
  85. gs_draw_sprite(data->tex, 0, 0, 0);
  86. gs_matrix_pop();
  87. gs_enable_color(true, true, true, true);
  88. gs_blend_state_pop();
  89. }
  90. void xcb_xcursor_offset(xcb_xcursor_t *data, const int x_org, const int y_org)
  91. {
  92. data->x_org = x_org;
  93. data->y_org = y_org;
  94. }