Debian Codegraph @

src/byzanzlayercursor.c

raw symbols SCIP
1 /* desktop session recorder
2 * Copyright (C) 2009 Benjamin Otte <[email protected]>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 3 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include "byzanzlayercursor.h"
23
24 #include <gdk/gdkx.h>
25
26 G_DEFINE_TYPE (ByzanzLayerCursor, byzanz_layer_cursor, BYZANZ_TYPE_LAYER)
27
28 static cairo_surface_t *
29 create_surface_for_cursor (XFixesCursorImage *cursor)
30 {
31 cairo_surface_t *surface;
32 gulong *cursor_data;
33 guint32 *surface_data;
34 guint x, y, stride;
35
36 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
37 cursor->width,
38 cursor->height);
39
40 surface_data = (guint32 *) cairo_image_surface_get_data (surface);
41 cursor_data = cursor->pixels;
42 stride = cairo_image_surface_get_stride (surface) / sizeof (guint32);
43
44 for (y = 0; y < cursor->height; y++)
45 {
46 for (x = 0; x < cursor->width; x++)
47 {
48 surface_data[x] = *cursor_data;
49 cursor_data++;
50 }
51 surface_data += stride;
52 }
53
54 cairo_surface_mark_dirty (surface);
55 cairo_surface_set_device_offset (surface, cursor->xhot, cursor->yhot);
56
57 return surface;
58 }
59
60 static cairo_surface_t *
61 byzanz_layer_cursor_read_cursor (ByzanzLayerCursor *clayer)
62 {
63 Display *dpy = GDK_DISPLAY_XDISPLAY (gdk_window_get_display (BYZANZ_LAYER (clayer)->recorder->window));
64 XFixesCursorImage *cursor;
65
66 cursor = XFixesGetCursorImage (dpy);
67 if (cursor) {
68 cairo_surface_t *surface = create_surface_for_cursor (cursor);
69 g_hash_table_insert (clayer->cursors, cursor, surface);
70 return surface;
71 } else {
72 return NULL;
73 }
74 }
75
76 static gboolean
77 byzanz_layer_cursor_event (ByzanzLayer * layer,
78 GdkXEvent * gdkxevent)
79 {
80 ByzanzLayerCursor *clayer = BYZANZ_LAYER_CURSOR (layer);
81 XFixesCursorNotifyEvent *event = gdkxevent;
82
83 if (event->type == layer->recorder->fixes_event_base + XFixesCursorNotify) {
84 XFixesCursorImage hack;
85
86 hack.cursor_serial = event->cursor_serial;
87 clayer->cursor_next = g_hash_table_lookup (clayer->cursors, &hack);
88 if (clayer->cursor_next == NULL)
89 clayer->cursor_next = byzanz_layer_cursor_read_cursor (clayer);
90 if (clayer->cursor_next != clayer->cursor)
91 byzanz_layer_invalidate (layer);
92 return TRUE;
93 }
94
95 return FALSE;
96 }
97
98 static gboolean
99 byzanz_layer_cursor_poll (gpointer data)
100 {
101 ByzanzLayerCursor *clayer = data;
102 int x, y;
103 GdkDevice *device;
104 GdkDeviceManager *device_manager;
105 GdkDisplay *display;
106 GdkWindow *window;
107
108 window = BYZANZ_LAYER (clayer)->recorder->window;
109 display = gdk_window_get_display (window);
110 device_manager = gdk_display_get_device_manager (display);
111 device = gdk_device_manager_get_client_pointer (device_manager);
112 gdk_window_get_device_position (window, device, &x, &y, NULL);
113 if (x == clayer->cursor_x &&
114 y == clayer->cursor_y)
115 return TRUE;
116
117 clayer->poll_source = 0;
118 byzanz_layer_invalidate (BYZANZ_LAYER (clayer));
119 return FALSE;
120 }
121
122 static void
123 byzanz_layer_cursor_setup_poll (ByzanzLayerCursor *clayer)
124 {
125 if (clayer->poll_source != 0)
126 return;
127
128 /* FIXME: Is 10ms ok or is it too much? */
129 clayer->poll_source = g_timeout_add (10, byzanz_layer_cursor_poll, clayer);
130 }
131
132 static void
133 byzanz_layer_cursor_invalidate_cursor (cairo_region_t *region, cairo_surface_t *surface, int x, int y)
134 {
135 cairo_rectangle_int_t cursor_rect;
136 double xhot, yhot;
137
138 if (surface == NULL)
139 return;
140
141 cairo_surface_get_device_offset (surface, &xhot, &yhot);
142
143 cursor_rect.x = x - xhot;
144 cursor_rect.y = y - yhot;
145 cursor_rect.width = cairo_image_surface_get_width (surface);
146 cursor_rect.height = cairo_image_surface_get_height (surface);
147
148 cairo_region_union_rectangle (region, &cursor_rect);
149 }
150
151 static cairo_region_t *
152 byzanz_layer_cursor_snapshot (ByzanzLayer *layer)
153 {
154 ByzanzLayerCursor *clayer = BYZANZ_LAYER_CURSOR (layer);
155 cairo_region_t *region, *area;
156 int x, y;
157 GdkDevice *device;
158 GdkDeviceManager *device_manager;
159 GdkDisplay *display;
160 GdkWindow *window;
161
162 window = layer->recorder->window;
163 display = gdk_window_get_display (window);
164 device_manager = gdk_display_get_device_manager (display);
165 device = gdk_device_manager_get_client_pointer (device_manager);
166 gdk_window_get_device_position (window, device, &x, &y, NULL);
167 if (x == clayer->cursor_x &&
168 y == clayer->cursor_y &&
169 clayer->cursor_next == clayer->cursor)
170 return NULL;
171
172 region = cairo_region_create ();
173 byzanz_layer_cursor_invalidate_cursor (region, clayer->cursor, clayer->cursor_x, clayer->cursor_y);
174 byzanz_layer_cursor_invalidate_cursor (region, clayer->cursor_next, x, y);
175 area = cairo_region_create_rectangle (&layer->recorder->area);
176 cairo_region_intersect (region, area);
177 cairo_region_destroy (area);
178
179 clayer->cursor = clayer->cursor_next;
180 clayer->cursor_x = x;
181 clayer->cursor_y = y;
182 byzanz_layer_cursor_setup_poll (clayer);
183
184 return region;
185 }
186
187 static void
188 byzanz_layer_cursor_render (ByzanzLayer *layer,
189 cairo_t * cr)
190 {
191 ByzanzLayerCursor *clayer = BYZANZ_LAYER_CURSOR (layer);
192 cairo_surface_t *cursor_surface;
193
194 if (clayer->cursor == NULL)
195 return;
196
197 cursor_surface = clayer->cursor;
198
199 cairo_save (cr);
200
201 cairo_translate (cr, clayer->cursor_x, clayer->cursor_y);
202 cairo_set_source_surface (cr, cursor_surface, 0, 0);
203 cairo_paint (cr);
204 cairo_restore (cr);
205 }
206
207 static void
208 byzanz_layer_cursor_finalize (GObject *object)
209 {
210 ByzanzLayerCursor *clayer = BYZANZ_LAYER_CURSOR (object);
211 GdkWindow *window = BYZANZ_LAYER (object)->recorder->window;
212 Display *dpy = GDK_DISPLAY_XDISPLAY (gdk_window_get_display (window));
213
214 XFixesSelectCursorInput (dpy, GDK_WINDOW_XID (window), 0);
215
216 g_hash_table_destroy (clayer->cursors);
217
218 if (clayer->poll_source != 0) {
219 g_source_remove (clayer->poll_source);
220 }
221
222 G_OBJECT_CLASS (byzanz_layer_cursor_parent_class)->finalize (object);
223 }
224
225 static void
226 byzanz_layer_cursor_constructed (GObject *object)
227 {
228 ByzanzLayerCursor *clayer = BYZANZ_LAYER_CURSOR (object);
229 GdkWindow *window = BYZANZ_LAYER (object)->recorder->window;
230 Display *dpy = GDK_DISPLAY_XDISPLAY (gdk_window_get_display (window));
231
232 XFixesSelectCursorInput (dpy, GDK_WINDOW_XID (window), XFixesDisplayCursorNotifyMask);
233 byzanz_layer_cursor_read_cursor (clayer);
234 byzanz_layer_cursor_setup_poll (clayer);
235
236 G_OBJECT_CLASS (byzanz_layer_cursor_parent_class)->constructed (object);
237 }
238
239 static void
240 byzanz_layer_cursor_class_init (ByzanzLayerCursorClass *klass)
241 {
242 GObjectClass *object_class = G_OBJECT_CLASS (klass);
243 ByzanzLayerClass *layer_class = BYZANZ_LAYER_CLASS (klass);
244
245 object_class->constructed = byzanz_layer_cursor_constructed;
246 object_class->finalize = byzanz_layer_cursor_finalize;
247
248 layer_class->event = byzanz_layer_cursor_event;
249 layer_class->snapshot = byzanz_layer_cursor_snapshot;
250 layer_class->render = byzanz_layer_cursor_render;
251 }
252
253 static guint
254 byzanz_cursor_hash (gconstpointer key)
255 {
256 return (guint) ((const XFixesCursorImage *) key)->cursor_serial;
257 }
258
259 static gboolean
260 byzanz_cursor_equal (gconstpointer c1, gconstpointer c2)
261 {
262 return ((const XFixesCursorImage *) c1)->cursor_serial ==
263 ((const XFixesCursorImage *) c2)->cursor_serial;
264 }
265
266 static void
267 byzanz_layer_cursor_init (ByzanzLayerCursor *clayer)
268 {
269 clayer->cursors = g_hash_table_new_full (byzanz_cursor_hash,
270 byzanz_cursor_equal, NULL, (GDestroyNotify) cairo_surface_destroy);
271 }
272