xcursor-xcb.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <xcb/xcb.h>
  17. #include <util/bmem.h>
  18. #include "xcursor-xcb.h"
  19. /*
  20. * Create the cursor texture, either by updating if the new cursor has the same
  21. * size or by creating a new texture if the size is different
  22. */
  23. static void xcb_xcursor_create(xcb_xcursor_t *data, 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 && data->last_width == xc->height) {
  29. gs_texture_set_image(data->tex, (const uint8_t *)pixels, xc->width * sizeof(uint32_t), false);
  30. } else {
  31. if (data->tex)
  32. gs_texture_destroy(data->tex);
  33. data->tex = gs_texture_create(xc->width, xc->height, GS_BGRA, 1, (const uint8_t **)&pixels, GS_DYNAMIC);
  34. }
  35. data->last_serial = xc->cursor_serial;
  36. data->last_width = xc->width;
  37. data->last_height = xc->height;
  38. }
  39. /**
  40. * We need to check for the xfixes version in order to initialize it ?
  41. */
  42. xcb_xcursor_t *xcb_xcursor_init(xcb_connection_t *xcb)
  43. {
  44. xcb_xcursor_t *data = bzalloc(sizeof(xcb_xcursor_t));
  45. xcb_xfixes_query_version_cookie_t xfix_c;
  46. xfix_c = xcb_xfixes_query_version_unchecked(xcb, XCB_XFIXES_MAJOR_VERSION, XCB_XFIXES_MINOR_VERSION);
  47. free(xcb_xfixes_query_version_reply(xcb, xfix_c, NULL));
  48. return data;
  49. }
  50. void xcb_xcursor_destroy(xcb_xcursor_t *data)
  51. {
  52. if (data->tex)
  53. gs_texture_destroy(data->tex);
  54. bfree(data);
  55. }
  56. void xcb_xcursor_update(xcb_connection_t *xcb, xcb_xcursor_t *data)
  57. {
  58. xcb_xfixes_get_cursor_image_cookie_t xc_c = xcb_xfixes_get_cursor_image_unchecked(xcb);
  59. xcb_xfixes_get_cursor_image_reply_t *xc = xcb_xfixes_get_cursor_image_reply(xcb, xc_c, NULL);
  60. if (!data || !xc)
  61. return;
  62. if (!data->tex || data->last_serial != xc->cursor_serial)
  63. xcb_xcursor_create(data, xc);
  64. data->x = xc->x - data->x_org;
  65. data->y = xc->y - data->y_org;
  66. data->x_render = data->x - xc->xhot;
  67. data->y_render = data->y - xc->yhot;
  68. free(xc);
  69. }
  70. void xcb_xcursor_render(xcb_xcursor_t *data)
  71. {
  72. if (!data->tex)
  73. return;
  74. const bool linear_srgb = gs_get_linear_srgb();
  75. const bool previous = gs_framebuffer_srgb_enabled();
  76. gs_enable_framebuffer_srgb(linear_srgb);
  77. gs_effect_t *effect = gs_get_effect();
  78. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  79. if (linear_srgb)
  80. gs_effect_set_texture_srgb(image, data->tex);
  81. else
  82. gs_effect_set_texture(image, data->tex);
  83. gs_blend_state_push();
  84. gs_blend_function(GS_BLEND_SRCALPHA, GS_BLEND_INVSRCALPHA);
  85. gs_enable_color(true, true, true, false);
  86. gs_matrix_push();
  87. gs_matrix_translate3f(data->x_render, data->y_render, 0.0f);
  88. gs_draw_sprite(data->tex, 0, 0, 0);
  89. gs_matrix_pop();
  90. gs_enable_color(true, true, true, true);
  91. gs_blend_state_pop();
  92. gs_enable_framebuffer_srgb(previous);
  93. }
  94. void xcb_xcursor_offset(xcb_xcursor_t *data, const int x_org, const int y_org)
  95. {
  96. data->x_org = x_org;
  97. data->y_org = y_org;
  98. }
  99. void xcb_xcursor_offset_win(xcb_connection_t *xcb, xcb_xcursor_t *data, xcb_window_t win)
  100. {
  101. if (!win)
  102. return;
  103. xcb_generic_error_t *err = NULL;
  104. xcb_get_geometry_cookie_t geom_cookie = xcb_get_geometry(xcb, win);
  105. xcb_get_geometry_reply_t *geom = xcb_get_geometry_reply(xcb, geom_cookie, &err);
  106. if (err) {
  107. free(geom);
  108. return;
  109. }
  110. xcb_translate_coordinates_cookie_t coords_cookie = xcb_translate_coordinates(xcb, win, geom->root, 0, 0);
  111. xcb_translate_coordinates_reply_t *coords = xcb_translate_coordinates_reply(xcb, coords_cookie, &err);
  112. if (!err)
  113. xcb_xcursor_offset(data, coords->dst_x, coords->dst_y);
  114. free(coords);
  115. free(geom);
  116. }