xcursor-xcb.c 4.2 KB

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