2014-04-21 18:05:59 -04:00
|
|
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Red Hat
|
|
|
|
*
|
|
|
|
* 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
* 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* Written by:
|
|
|
|
* Jasper St. Pierre <jstpierre@mecheye.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "meta-cursor-renderer.h"
|
|
|
|
|
2014-08-13 19:46:32 -04:00
|
|
|
#include <meta/meta-backend.h>
|
2015-11-09 09:51:02 -05:00
|
|
|
#include <backends/meta-backend-private.h>
|
|
|
|
#include <backends/meta-monitor-manager-private.h>
|
2014-08-13 19:46:32 -04:00
|
|
|
#include <meta/util.h>
|
|
|
|
|
2014-04-21 18:05:59 -04:00
|
|
|
#include <cogl/cogl.h>
|
|
|
|
#include <clutter/clutter.h>
|
|
|
|
|
2014-04-22 15:06:42 -04:00
|
|
|
#include "meta-stage.h"
|
2014-04-21 18:05:59 -04:00
|
|
|
|
|
|
|
struct _MetaCursorRendererPrivate
|
|
|
|
{
|
|
|
|
int current_x, current_y;
|
|
|
|
|
2015-03-09 23:23:00 -04:00
|
|
|
MetaCursorSprite *displayed_cursor;
|
2014-05-17 11:22:05 -04:00
|
|
|
gboolean handled_by_backend;
|
2014-04-21 18:05:59 -04:00
|
|
|
};
|
|
|
|
typedef struct _MetaCursorRendererPrivate MetaCursorRendererPrivate;
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (MetaCursorRenderer, meta_cursor_renderer, G_TYPE_OBJECT);
|
|
|
|
|
|
|
|
static void
|
2015-07-17 11:16:39 -04:00
|
|
|
queue_redraw (MetaCursorRenderer *renderer,
|
|
|
|
MetaCursorSprite *cursor_sprite)
|
2014-04-21 18:05:59 -04:00
|
|
|
{
|
|
|
|
MetaCursorRendererPrivate *priv = meta_cursor_renderer_get_instance_private (renderer);
|
2014-08-13 19:46:32 -04:00
|
|
|
MetaBackend *backend = meta_get_backend ();
|
|
|
|
ClutterActor *stage = meta_backend_get_stage (backend);
|
2014-08-21 15:00:03 -04:00
|
|
|
CoglTexture *texture;
|
2015-07-17 11:16:39 -04:00
|
|
|
MetaRectangle rect = { 0 };
|
|
|
|
|
|
|
|
if (cursor_sprite)
|
|
|
|
rect = meta_cursor_renderer_calculate_rect (renderer, cursor_sprite);
|
2014-04-21 18:05:59 -04:00
|
|
|
|
2014-04-22 14:43:46 -04:00
|
|
|
/* During early initialization, we can have no stage */
|
|
|
|
if (!stage)
|
|
|
|
return;
|
|
|
|
|
2015-07-17 11:16:39 -04:00
|
|
|
if (cursor_sprite && !priv->handled_by_backend)
|
|
|
|
texture = meta_cursor_sprite_get_cogl_texture (cursor_sprite);
|
2014-05-17 11:22:05 -04:00
|
|
|
else
|
2014-08-21 15:00:03 -04:00
|
|
|
texture = NULL;
|
|
|
|
|
2015-07-17 11:16:39 -04:00
|
|
|
meta_stage_set_cursor (META_STAGE (stage), texture, &rect);
|
2014-04-21 18:05:59 -04:00
|
|
|
}
|
|
|
|
|
2014-05-17 11:22:05 -04:00
|
|
|
static gboolean
|
2015-07-17 11:16:39 -04:00
|
|
|
meta_cursor_renderer_real_update_cursor (MetaCursorRenderer *renderer,
|
|
|
|
MetaCursorSprite *cursor_sprite)
|
2014-04-22 15:39:09 -04:00
|
|
|
{
|
2014-05-17 11:22:05 -04:00
|
|
|
return FALSE;
|
2014-04-22 15:15:11 -04:00
|
|
|
}
|
2014-04-22 15:39:09 -04:00
|
|
|
|
2014-04-22 15:15:11 -04:00
|
|
|
static void
|
|
|
|
meta_cursor_renderer_class_init (MetaCursorRendererClass *klass)
|
|
|
|
{
|
|
|
|
klass->update_cursor = meta_cursor_renderer_real_update_cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
meta_cursor_renderer_init (MetaCursorRenderer *renderer)
|
|
|
|
{
|
2014-04-22 15:39:09 -04:00
|
|
|
}
|
|
|
|
|
2015-07-17 11:16:39 -04:00
|
|
|
MetaRectangle
|
|
|
|
meta_cursor_renderer_calculate_rect (MetaCursorRenderer *renderer,
|
|
|
|
MetaCursorSprite *cursor_sprite)
|
|
|
|
{
|
|
|
|
MetaCursorRendererPrivate *priv =
|
|
|
|
meta_cursor_renderer_get_instance_private (renderer);
|
|
|
|
CoglTexture *texture;
|
|
|
|
int hot_x, hot_y;
|
|
|
|
int width, height;
|
|
|
|
float texture_scale;
|
|
|
|
|
|
|
|
texture = meta_cursor_sprite_get_cogl_texture (cursor_sprite);
|
|
|
|
if (!texture)
|
|
|
|
return (MetaRectangle) { 0 };
|
|
|
|
|
|
|
|
meta_cursor_sprite_get_hotspot (cursor_sprite, &hot_x, &hot_y);
|
|
|
|
texture_scale = meta_cursor_sprite_get_texture_scale (cursor_sprite);
|
|
|
|
width = cogl_texture_get_width (texture);
|
|
|
|
height = cogl_texture_get_height (texture);
|
|
|
|
|
|
|
|
return (MetaRectangle) {
|
|
|
|
.x = (int)roundf (priv->current_x - (hot_x * texture_scale)),
|
|
|
|
.y = (int)roundf (priv->current_y - (hot_y * texture_scale)),
|
|
|
|
.width = (int)roundf (width * texture_scale),
|
|
|
|
.height = (int)roundf (height * texture_scale),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-11-09 09:51:02 -05:00
|
|
|
static gboolean
|
|
|
|
is_cursor_in_monitors_area (int x, int y)
|
|
|
|
{
|
|
|
|
MetaMonitorManager *monitor_manager = meta_backend_get_monitor_manager (meta_get_backend ());
|
|
|
|
return meta_monitor_manager_get_monitor_at_point (monitor_manager,
|
|
|
|
(gfloat) x, (gfloat) y) >= 0;
|
|
|
|
}
|
|
|
|
|
2014-04-21 18:05:59 -04:00
|
|
|
static void
|
2015-07-17 11:16:39 -04:00
|
|
|
update_cursor (MetaCursorRenderer *renderer,
|
|
|
|
MetaCursorSprite *cursor_sprite)
|
2014-04-21 18:05:59 -04:00
|
|
|
{
|
|
|
|
MetaCursorRendererPrivate *priv = meta_cursor_renderer_get_instance_private (renderer);
|
2014-05-17 11:22:05 -04:00
|
|
|
gboolean handled_by_backend;
|
|
|
|
gboolean should_redraw = FALSE;
|
2014-04-21 18:05:59 -04:00
|
|
|
|
2015-11-09 09:51:02 -05:00
|
|
|
/* do not render cursor if it is not on any monitor. Such situation
|
|
|
|
* can occur e. g. after monitor hot-plug */
|
|
|
|
if (!is_cursor_in_monitors_area (priv->current_x, priv->current_y))
|
|
|
|
return;
|
|
|
|
|
2015-07-17 11:16:39 -04:00
|
|
|
if (cursor_sprite)
|
|
|
|
meta_cursor_sprite_prepare_at (cursor_sprite,
|
|
|
|
priv->current_x,
|
|
|
|
priv->current_y);
|
2014-04-21 18:05:59 -04:00
|
|
|
|
2015-07-17 11:16:39 -04:00
|
|
|
handled_by_backend =
|
|
|
|
META_CURSOR_RENDERER_GET_CLASS (renderer)->update_cursor (renderer,
|
|
|
|
cursor_sprite);
|
2014-05-17 11:22:05 -04:00
|
|
|
if (handled_by_backend != priv->handled_by_backend)
|
|
|
|
{
|
|
|
|
priv->handled_by_backend = handled_by_backend;
|
|
|
|
should_redraw = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!handled_by_backend)
|
|
|
|
should_redraw = TRUE;
|
|
|
|
|
|
|
|
if (should_redraw)
|
2015-07-17 11:16:39 -04:00
|
|
|
queue_redraw (renderer, cursor_sprite);
|
2014-04-21 18:05:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
MetaCursorRenderer *
|
2014-04-22 14:27:38 -04:00
|
|
|
meta_cursor_renderer_new (void)
|
2014-04-21 18:05:59 -04:00
|
|
|
{
|
2014-04-22 14:27:38 -04:00
|
|
|
return g_object_new (META_TYPE_CURSOR_RENDERER, NULL);
|
2014-04-21 18:05:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-03-09 23:23:00 -04:00
|
|
|
meta_cursor_renderer_set_cursor (MetaCursorRenderer *renderer,
|
|
|
|
MetaCursorSprite *cursor_sprite)
|
2014-04-21 18:05:59 -04:00
|
|
|
{
|
|
|
|
MetaCursorRendererPrivate *priv = meta_cursor_renderer_get_instance_private (renderer);
|
|
|
|
|
2015-03-09 23:23:00 -04:00
|
|
|
if (priv->displayed_cursor == cursor_sprite)
|
2014-04-21 18:05:59 -04:00
|
|
|
return;
|
2015-03-09 23:23:00 -04:00
|
|
|
priv->displayed_cursor = cursor_sprite;
|
2015-07-17 11:16:39 -04:00
|
|
|
|
|
|
|
update_cursor (renderer, cursor_sprite);
|
2014-04-21 18:05:59 -04:00
|
|
|
}
|
|
|
|
|
2015-07-13 14:36:05 -04:00
|
|
|
void
|
|
|
|
meta_cursor_renderer_force_update (MetaCursorRenderer *renderer)
|
|
|
|
{
|
2015-07-17 11:16:39 -04:00
|
|
|
MetaCursorRendererPrivate *priv =
|
|
|
|
meta_cursor_renderer_get_instance_private (renderer);
|
|
|
|
|
|
|
|
update_cursor (renderer, priv->displayed_cursor);
|
2015-07-13 14:36:05 -04:00
|
|
|
}
|
|
|
|
|
2014-04-21 18:05:59 -04:00
|
|
|
void
|
|
|
|
meta_cursor_renderer_set_position (MetaCursorRenderer *renderer,
|
|
|
|
int x, int y)
|
|
|
|
{
|
|
|
|
MetaCursorRendererPrivate *priv = meta_cursor_renderer_get_instance_private (renderer);
|
|
|
|
|
|
|
|
g_assert (meta_is_wayland_compositor ());
|
|
|
|
|
|
|
|
priv->current_x = x;
|
|
|
|
priv->current_y = y;
|
|
|
|
|
2015-07-17 11:16:39 -04:00
|
|
|
update_cursor (renderer, priv->displayed_cursor);
|
2014-04-21 18:05:59 -04:00
|
|
|
}
|
|
|
|
|
2015-03-09 23:23:00 -04:00
|
|
|
MetaCursorSprite *
|
2014-04-22 15:15:11 -04:00
|
|
|
meta_cursor_renderer_get_cursor (MetaCursorRenderer *renderer)
|
2014-04-21 18:05:59 -04:00
|
|
|
{
|
2014-04-22 15:15:11 -04:00
|
|
|
MetaCursorRendererPrivate *priv = meta_cursor_renderer_get_instance_private (renderer);
|
2014-04-21 18:05:59 -04:00
|
|
|
|
2014-04-22 15:15:11 -04:00
|
|
|
return priv->displayed_cursor;
|
2014-04-21 18:05:59 -04:00
|
|
|
}
|
|
|
|
|
2015-03-10 23:16:58 -04:00
|
|
|
#ifdef HAVE_WAYLAND
|
|
|
|
void
|
|
|
|
meta_cursor_renderer_realize_cursor_from_wl_buffer (MetaCursorRenderer *renderer,
|
|
|
|
MetaCursorSprite *cursor_sprite,
|
|
|
|
struct wl_resource *buffer)
|
|
|
|
{
|
|
|
|
|
|
|
|
MetaCursorRendererClass *renderer_class = META_CURSOR_RENDERER_GET_CLASS (renderer);
|
|
|
|
|
|
|
|
if (renderer_class->realize_cursor_from_wl_buffer)
|
|
|
|
renderer_class->realize_cursor_from_wl_buffer (renderer, cursor_sprite, buffer);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
meta_cursor_renderer_realize_cursor_from_xcursor (MetaCursorRenderer *renderer,
|
|
|
|
MetaCursorSprite *cursor_sprite,
|
|
|
|
XcursorImage *xc_image)
|
|
|
|
{
|
|
|
|
MetaCursorRendererClass *renderer_class = META_CURSOR_RENDERER_GET_CLASS (renderer);
|
|
|
|
|
|
|
|
if (renderer_class->realize_cursor_from_xcursor)
|
|
|
|
renderer_class->realize_cursor_from_xcursor (renderer, cursor_sprite, xc_image);
|
|
|
|
}
|