2014-02-13 13:05:23 -08:00
|
|
|
/*
|
|
|
|
Copyright (C) 2014 by Leonhard Oelke <leonhard@in-verted.de>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2014-02-20 10:41:04 -08:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2014-02-09 11:36:52 -08:00
|
|
|
#include <X11/extensions/Xfixes.h>
|
2014-02-20 10:41:04 -08:00
|
|
|
|
|
|
|
#include <util/bmem.h>
|
2014-02-09 11:36:52 -08:00
|
|
|
#include "xcursor.h"
|
|
|
|
|
2014-02-20 10:41:04 -08:00
|
|
|
/*
|
|
|
|
* Get pixel data for the cursor
|
2014-03-11 10:12:54 -07:00
|
|
|
*
|
2014-02-20 10:41:04 -08:00
|
|
|
* XFixes has the data defined as unsigned long, so we can not use memcpy.
|
|
|
|
* Theres a lot of talk about this in other implementation and they tend to
|
|
|
|
* be really complicated, but this naive approach seems to work fine ...
|
|
|
|
*/
|
2019-06-22 22:13:45 -07:00
|
|
|
static uint32_t *xcursor_pixels(XFixesCursorImage *xc)
|
|
|
|
{
|
2014-02-20 10:41:04 -08:00
|
|
|
uint_fast32_t size = xc->width * xc->height;
|
|
|
|
uint32_t *pixels = bmalloc(size * sizeof(uint32_t));
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-20 10:41:04 -08:00
|
|
|
for (uint_fast32_t i = 0; i < size; ++i)
|
2019-06-22 22:13:45 -07:00
|
|
|
pixels[i] = (uint32_t)xc->pixels[i];
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-20 10:41:04 -08:00
|
|
|
return pixels;
|
2014-02-09 11:36:52 -08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:41:04 -08:00
|
|
|
/*
|
|
|
|
* Create the cursor texture, either by updating if the new cursor has the same
|
|
|
|
* size or by creating a new texture if the size is different
|
2014-03-11 10:12:54 -07:00
|
|
|
*/
|
2019-06-22 22:13:45 -07:00
|
|
|
static void xcursor_create(xcursor_t *data, XFixesCursorImage *xc)
|
|
|
|
{
|
2014-02-20 10:41:04 -08:00
|
|
|
uint32_t *pixels = xcursor_pixels(xc);
|
2015-01-13 13:19:47 -08:00
|
|
|
if (!pixels)
|
|
|
|
return;
|
2014-02-20 10:41:04 -08:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
if (data->tex && data->last_height == xc->width &&
|
|
|
|
data->last_width == xc->height) {
|
|
|
|
gs_texture_set_image(data->tex, (const uint8_t *)pixels,
|
|
|
|
xc->width * sizeof(uint32_t), False);
|
2014-02-20 10:41:04 -08:00
|
|
|
} else {
|
|
|
|
if (data->tex)
|
2014-08-07 23:42:07 -07:00
|
|
|
gs_texture_destroy(data->tex);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
data->tex = gs_texture_create(xc->width, xc->height, GS_BGRA, 1,
|
|
|
|
(const uint8_t **)&pixels,
|
|
|
|
GS_DYNAMIC);
|
2014-02-20 10:41:04 -08:00
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-20 10:41:04 -08:00
|
|
|
bfree(pixels);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-20 10:41:04 -08:00
|
|
|
data->last_serial = xc->cursor_serial;
|
|
|
|
data->last_width = xc->width;
|
|
|
|
data->last_height = xc->height;
|
2014-02-09 11:36:52 -08:00
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
xcursor_t *xcursor_init(Display *dpy)
|
|
|
|
{
|
2014-04-22 10:53:06 -07:00
|
|
|
xcursor_t *data = bzalloc(sizeof(xcursor_t));
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-20 10:41:04 -08:00
|
|
|
data->dpy = dpy;
|
|
|
|
xcursor_tick(data);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-20 10:41:04 -08:00
|
|
|
return data;
|
2014-02-09 11:36:52 -08:00
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
void xcursor_destroy(xcursor_t *data)
|
|
|
|
{
|
2014-02-20 10:41:04 -08:00
|
|
|
if (data->tex)
|
2014-08-07 23:42:07 -07:00
|
|
|
gs_texture_destroy(data->tex);
|
2014-02-20 10:41:04 -08:00
|
|
|
bfree(data);
|
2014-02-09 11:36:52 -08:00
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
void xcursor_tick(xcursor_t *data)
|
|
|
|
{
|
2014-02-20 10:41:04 -08:00
|
|
|
XFixesCursorImage *xc = XFixesGetCursorImage(data->dpy);
|
2015-01-13 13:19:47 -08:00
|
|
|
if (!xc)
|
|
|
|
return;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-20 10:41:04 -08:00
|
|
|
if (!data->tex || data->last_serial != xc->cursor_serial)
|
|
|
|
xcursor_create(data, xc);
|
2014-08-29 17:11:30 -07:00
|
|
|
|
|
|
|
data->x = (int_fast32_t)xc->x - (int_fast32_t)data->x_org;
|
|
|
|
data->y = (int_fast32_t)xc->y - (int_fast32_t)data->y_org;
|
2014-08-29 17:09:14 -07:00
|
|
|
data->render_x = xc->x - xc->xhot - data->x_org;
|
|
|
|
data->render_y = xc->y - xc->yhot - data->y_org;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-20 10:41:04 -08:00
|
|
|
XFree(xc);
|
2014-02-10 10:09:44 -08:00
|
|
|
}
|
|
|
|
|
2019-10-28 01:59:19 -07:00
|
|
|
void xcursor_render(xcursor_t *data, int x_offset, int y_offset)
|
2019-06-22 22:13:45 -07:00
|
|
|
{
|
2015-01-13 13:22:39 -08:00
|
|
|
if (!data->tex)
|
|
|
|
return;
|
|
|
|
|
2021-01-20 19:48:48 -08:00
|
|
|
const bool linear_srgb = gs_get_linear_srgb();
|
|
|
|
|
|
|
|
const bool previous = gs_framebuffer_srgb_enabled();
|
|
|
|
gs_enable_framebuffer_srgb(linear_srgb);
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
gs_effect_t *effect = gs_get_effect();
|
2014-09-25 17:44:05 -07:00
|
|
|
gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
|
2021-01-20 19:48:48 -08:00
|
|
|
if (linear_srgb)
|
|
|
|
gs_effect_set_texture_srgb(image, data->tex);
|
|
|
|
else
|
|
|
|
gs_effect_set_texture(image, data->tex);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2015-03-14 00:38:09 -07:00
|
|
|
gs_blend_state_push();
|
|
|
|
gs_blend_function(GS_BLEND_SRCALPHA, GS_BLEND_INVSRCALPHA);
|
|
|
|
gs_enable_color(true, true, true, false);
|
|
|
|
|
2014-02-20 10:41:04 -08:00
|
|
|
gs_matrix_push();
|
2019-10-28 01:59:19 -07:00
|
|
|
gs_matrix_translate3f(data->render_x + x_offset,
|
|
|
|
data->render_y + y_offset, 0.0f);
|
2014-02-20 10:41:04 -08:00
|
|
|
gs_draw_sprite(data->tex, 0, 0, 0);
|
|
|
|
gs_matrix_pop();
|
2015-03-14 00:38:09 -07:00
|
|
|
|
|
|
|
gs_enable_color(true, true, true, true);
|
|
|
|
gs_blend_state_pop();
|
2021-01-20 19:48:48 -08:00
|
|
|
|
|
|
|
gs_enable_framebuffer_srgb(previous);
|
2014-02-14 15:12:06 -08:00
|
|
|
}
|
2014-05-04 09:01:32 -07:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
void xcursor_offset(xcursor_t *data, int_fast32_t x_org, int_fast32_t y_org)
|
2014-05-04 09:01:32 -07:00
|
|
|
{
|
|
|
|
data->x_org = x_org;
|
|
|
|
data->y_org = y_org;
|
|
|
|
}
|