1 /*
2 Copyright (C) 2014 by Leonhard Oelke <leonhard@in-verted.de>
3 
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include <stdint.h>
19 #include <xcb/xfixes.h>
20 
21 #include <util/bmem.h>
22 #include "xcursor-xcb.h"
23 
24 /*
25  * Create the cursor texture, either by updating if the new cursor has the same
26  * size or by creating a new texture if the size is different
27  */
xcb_xcursor_create(xcb_xcursor_t * data,xcb_xfixes_get_cursor_image_reply_t * xc)28 static void xcb_xcursor_create(xcb_xcursor_t *data,
29 			       xcb_xfixes_get_cursor_image_reply_t *xc)
30 {
31 	uint32_t *pixels = xcb_xfixes_get_cursor_image_cursor_image(xc);
32 	if (!pixels)
33 		return;
34 
35 	if (data->tex && data->last_height == xc->width &&
36 	    data->last_width == xc->height) {
37 		gs_texture_set_image(data->tex, (const uint8_t *)pixels,
38 				     xc->width * sizeof(uint32_t), false);
39 	} else {
40 		if (data->tex)
41 			gs_texture_destroy(data->tex);
42 
43 		data->tex = gs_texture_create(xc->width, xc->height, GS_BGRA, 1,
44 					      (const uint8_t **)&pixels,
45 					      GS_DYNAMIC);
46 	}
47 
48 	data->last_serial = xc->cursor_serial;
49 	data->last_width = xc->width;
50 	data->last_height = xc->height;
51 }
52 
53 /**
54  * We need to check for the xfixes version in order to initialize it ?
55  */
xcb_xcursor_init(xcb_connection_t * xcb)56 xcb_xcursor_t *xcb_xcursor_init(xcb_connection_t *xcb)
57 {
58 	xcb_xcursor_t *data = bzalloc(sizeof(xcb_xcursor_t));
59 
60 	xcb_xfixes_query_version_cookie_t xfix_c;
61 
62 	xfix_c = xcb_xfixes_query_version_unchecked(
63 		xcb, XCB_XFIXES_MAJOR_VERSION, XCB_XFIXES_MINOR_VERSION);
64 	free(xcb_xfixes_query_version_reply(xcb, xfix_c, NULL));
65 
66 	return data;
67 }
68 
xcb_xcursor_destroy(xcb_xcursor_t * data)69 void xcb_xcursor_destroy(xcb_xcursor_t *data)
70 {
71 	if (data->tex)
72 		gs_texture_destroy(data->tex);
73 	bfree(data);
74 }
75 
xcb_xcursor_update(xcb_xcursor_t * data,xcb_xfixes_get_cursor_image_reply_t * xc)76 void xcb_xcursor_update(xcb_xcursor_t *data,
77 			xcb_xfixes_get_cursor_image_reply_t *xc)
78 {
79 	if (!data || !xc)
80 		return;
81 
82 	if (!data->tex || data->last_serial != xc->cursor_serial)
83 		xcb_xcursor_create(data, xc);
84 
85 	data->x = xc->x - data->x_org;
86 	data->y = xc->y - data->y_org;
87 	data->x_render = data->x - xc->xhot;
88 	data->y_render = data->y - xc->yhot;
89 }
90 
xcb_xcursor_render(xcb_xcursor_t * data)91 void xcb_xcursor_render(xcb_xcursor_t *data)
92 {
93 	if (!data->tex)
94 		return;
95 
96 	const bool linear_srgb = gs_get_linear_srgb();
97 
98 	const bool previous = gs_framebuffer_srgb_enabled();
99 	gs_enable_framebuffer_srgb(linear_srgb);
100 
101 	gs_effect_t *effect = gs_get_effect();
102 	gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
103 	if (linear_srgb)
104 		gs_effect_set_texture_srgb(image, data->tex);
105 	else
106 		gs_effect_set_texture(image, data->tex);
107 
108 	gs_blend_state_push();
109 	gs_blend_function(GS_BLEND_SRCALPHA, GS_BLEND_INVSRCALPHA);
110 	gs_enable_color(true, true, true, false);
111 
112 	gs_matrix_push();
113 	gs_matrix_translate3f(data->x_render, data->y_render, 0.0f);
114 	gs_draw_sprite(data->tex, 0, 0, 0);
115 	gs_matrix_pop();
116 
117 	gs_enable_color(true, true, true, true);
118 	gs_blend_state_pop();
119 
120 	gs_enable_framebuffer_srgb(previous);
121 }
122 
xcb_xcursor_offset(xcb_xcursor_t * data,const int x_org,const int y_org)123 void xcb_xcursor_offset(xcb_xcursor_t *data, const int x_org, const int y_org)
124 {
125 	data->x_org = x_org;
126 	data->y_org = y_org;
127 }
128