cursor-capture.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include <windows.h>
  2. #include <obs.h>
  3. #include "cursor-capture.h"
  4. static uint8_t *get_bitmap_data(HBITMAP hbmp, BITMAP *bmp)
  5. {
  6. if (GetObject(hbmp, sizeof(*bmp), bmp) != 0) {
  7. uint8_t *output;
  8. unsigned int size = bmp->bmHeight * bmp->bmWidthBytes;
  9. output = bmalloc(size);
  10. GetBitmapBits(hbmp, size, output);
  11. return output;
  12. }
  13. return NULL;
  14. }
  15. static inline uint8_t bit_to_alpha(uint8_t *data, long pixel, bool invert)
  16. {
  17. uint8_t pix_byte = data[pixel / 8];
  18. bool alpha = (pix_byte >> (7 - pixel % 8) & 1) != 0;
  19. if (invert) {
  20. return alpha ? 0xFF : 0;
  21. } else {
  22. return alpha ? 0 : 0xFF;
  23. }
  24. }
  25. static inline bool bitmap_has_alpha(uint8_t *data, long num_pixels)
  26. {
  27. for (long i = 0; i < num_pixels; i++) {
  28. if (data[i * 4 + 3] != 0) {
  29. return true;
  30. }
  31. }
  32. return false;
  33. }
  34. static inline void apply_mask(uint8_t *color, uint8_t *mask, BITMAP *bmp_mask)
  35. {
  36. long mask_pix_offs;
  37. for (long y = 0; y < bmp_mask->bmHeight; y++) {
  38. for (long x = 0; x < bmp_mask->bmWidth; x++) {
  39. mask_pix_offs = y * (bmp_mask->bmWidthBytes * 8) + x;
  40. color[(y * bmp_mask->bmWidth + x) * 4 + 3] =
  41. bit_to_alpha(mask, mask_pix_offs, false);
  42. }
  43. }
  44. }
  45. static inline uint8_t *copy_from_color(ICONINFO *ii, uint32_t *width,
  46. uint32_t *height)
  47. {
  48. BITMAP bmp_color;
  49. BITMAP bmp_mask;
  50. uint8_t *color;
  51. uint8_t *mask;
  52. color = get_bitmap_data(ii->hbmColor, &bmp_color);
  53. if (!color) {
  54. return NULL;
  55. }
  56. if (bmp_color.bmBitsPixel < 32) {
  57. bfree(color);
  58. return NULL;
  59. }
  60. mask = get_bitmap_data(ii->hbmMask, &bmp_mask);
  61. if (mask) {
  62. long pixels = bmp_color.bmHeight * bmp_color.bmWidth;
  63. if (!bitmap_has_alpha(color, pixels))
  64. apply_mask(color, mask, &bmp_mask);
  65. bfree(mask);
  66. }
  67. *width = bmp_color.bmWidth;
  68. *height = bmp_color.bmHeight;
  69. return color;
  70. }
  71. static inline uint8_t *copy_from_mask(ICONINFO *ii, uint32_t *width,
  72. uint32_t *height)
  73. {
  74. uint8_t *output;
  75. uint8_t *mask;
  76. long pixels;
  77. long bottom;
  78. BITMAP bmp;
  79. mask = get_bitmap_data(ii->hbmMask, &bmp);
  80. if (!mask) {
  81. return NULL;
  82. }
  83. bmp.bmHeight /= 2;
  84. pixels = bmp.bmHeight * bmp.bmWidth;
  85. output = bzalloc(pixels * 4);
  86. bottom = bmp.bmWidthBytes * bmp.bmHeight;
  87. for (long i = 0; i < pixels; i++) {
  88. uint8_t alpha = bit_to_alpha(mask, i, false);
  89. uint8_t color = bit_to_alpha(mask + bottom, i, true);
  90. if (!alpha) {
  91. output[i * 4 + 3] = color;
  92. } else {
  93. *(uint32_t *)&output[i * 4] = !!color ? 0xFFFFFFFF
  94. : 0xFF000000;
  95. }
  96. }
  97. bfree(mask);
  98. *width = bmp.bmWidth;
  99. *height = bmp.bmHeight;
  100. return output;
  101. }
  102. static inline uint8_t *cursor_capture_icon_bitmap(ICONINFO *ii, uint32_t *width,
  103. uint32_t *height)
  104. {
  105. uint8_t *output;
  106. output = copy_from_color(ii, width, height);
  107. if (!output)
  108. output = copy_from_mask(ii, width, height);
  109. return output;
  110. }
  111. static gs_texture_t *get_cached_texture(struct cursor_data *data, uint32_t cx,
  112. uint32_t cy)
  113. {
  114. struct cached_cursor cc;
  115. for (size_t i = 0; i < data->cached_textures.num; i++) {
  116. struct cached_cursor *pcc = &data->cached_textures.array[i];
  117. if (pcc->cx == cx && pcc->cy == cy)
  118. return pcc->texture;
  119. }
  120. cc.texture = gs_texture_create(cx, cy, GS_BGRA, 1, NULL, GS_DYNAMIC);
  121. cc.cx = cx;
  122. cc.cy = cy;
  123. da_push_back(data->cached_textures, &cc);
  124. return cc.texture;
  125. }
  126. static inline bool cursor_capture_icon(struct cursor_data *data, HICON icon)
  127. {
  128. uint8_t *bitmap;
  129. uint32_t height;
  130. uint32_t width;
  131. ICONINFO ii;
  132. if (!icon) {
  133. return false;
  134. }
  135. if (!GetIconInfo(icon, &ii)) {
  136. return false;
  137. }
  138. bitmap = cursor_capture_icon_bitmap(&ii, &width, &height);
  139. if (bitmap) {
  140. if (data->last_cx != width || data->last_cy != height) {
  141. data->texture = get_cached_texture(data, width, height);
  142. data->last_cx = width;
  143. data->last_cy = height;
  144. }
  145. gs_texture_set_image(data->texture, bitmap, width * 4, false);
  146. bfree(bitmap);
  147. data->x_hotspot = ii.xHotspot;
  148. data->y_hotspot = ii.yHotspot;
  149. }
  150. DeleteObject(ii.hbmColor);
  151. DeleteObject(ii.hbmMask);
  152. return !!data->texture;
  153. }
  154. void cursor_capture(struct cursor_data *data)
  155. {
  156. CURSORINFO ci = {0};
  157. HICON icon;
  158. ci.cbSize = sizeof(ci);
  159. if (!GetCursorInfo(&ci)) {
  160. data->visible = false;
  161. return;
  162. }
  163. memcpy(&data->cursor_pos, &ci.ptScreenPos, sizeof(data->cursor_pos));
  164. if (data->current_cursor == ci.hCursor) {
  165. return;
  166. }
  167. icon = CopyIcon(ci.hCursor);
  168. data->visible = cursor_capture_icon(data, icon);
  169. data->current_cursor = ci.hCursor;
  170. if ((ci.flags & CURSOR_SHOWING) == 0)
  171. data->visible = false;
  172. DestroyIcon(icon);
  173. }
  174. void cursor_draw(struct cursor_data *data, long x_offset, long y_offset,
  175. long width, long height)
  176. {
  177. long x = data->cursor_pos.x + x_offset;
  178. long y = data->cursor_pos.y + y_offset;
  179. long x_draw = x - data->x_hotspot;
  180. long y_draw = y - data->y_hotspot;
  181. if (x < 0 || x > width || y < 0 || y > height)
  182. return;
  183. if (data->visible && !!data->texture) {
  184. gs_blend_state_push();
  185. gs_blend_function(GS_BLEND_SRCALPHA, GS_BLEND_INVSRCALPHA);
  186. gs_enable_color(true, true, true, false);
  187. gs_matrix_push();
  188. obs_source_draw(data->texture, x_draw, y_draw, 0, 0, false);
  189. gs_matrix_pop();
  190. gs_enable_color(true, true, true, true);
  191. gs_blend_state_pop();
  192. }
  193. }
  194. void cursor_data_free(struct cursor_data *data)
  195. {
  196. for (size_t i = 0; i < data->cached_textures.num; i++) {
  197. struct cached_cursor *pcc = &data->cached_textures.array[i];
  198. gs_texture_destroy(pcc->texture);
  199. }
  200. da_free(data->cached_textures);
  201. memset(data, 0, sizeof(*data));
  202. }