1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 /* needs to be first because any header might include gdk-pixbuf.h otherwise */
28 #define GDK_PIXBUF_ENABLE_BACKEND
29 #include <gdk-pixbuf/gdk-pixbuf.h>
31 #include "gdkcursor.h"
32 #include "gdkcursorprivate.h"
33 #include "gdkprivate-x11.h"
34 #include "gdkdisplay-x11.h"
37 #include <X11/cursorfont.h>
39 #include <X11/Xcursor/Xcursor.h>
42 #include <X11/extensions/Xfixes.h>
56 struct _GdkX11CursorClass
58 GdkCursorClass cursor_class;
61 static guint theme_serial = 0;
63 /* cursor_cache holds a cache of non-pixmap cursors to avoid expensive
64 * libXcursor searches, cursors are added to it but only removed when
65 * their display is closed. We make the assumption that since there are
66 * a small number of display's and a small number of cursor's that this
67 * list will stay small enough not to be a problem.
69 static GSList* cursor_cache = NULL;
71 struct cursor_cache_key
78 /* Caller should check if there is already a match first.
79 * Cursor MUST be either a typed cursor or a pixmap with
83 add_to_cache (GdkX11Cursor* cursor)
85 cursor_cache = g_slist_prepend (cursor_cache, cursor);
87 /* Take a ref so that if the caller frees it we still have it */
88 g_object_ref (cursor);
91 /* Returns 0 on a match
94 cache_compare_func (gconstpointer listelem,
97 GdkX11Cursor* cursor = (GdkX11Cursor*)listelem;
98 struct cursor_cache_key* key = (struct cursor_cache_key*)target;
100 if ((cursor->cursor.type != key->type) ||
101 (gdk_cursor_get_display (GDK_CURSOR (cursor)) != key->display))
102 return 1; /* No match */
104 /* Elements marked as pixmap must be named cursors
105 * (since we don't store normal pixmap cursors
107 if (key->type == GDK_CURSOR_IS_PIXMAP)
108 return strcmp (key->name, cursor->name);
110 return 0; /* Match */
113 /* Returns the cursor if there is a match, NULL if not
114 * For named cursors type shall be GDK_CURSOR_IS_PIXMAP
115 * For unnamed, typed cursors, name shall be NULL
118 find_in_cache (GdkDisplay *display,
123 struct cursor_cache_key key;
125 key.display = display;
129 res = g_slist_find_custom (cursor_cache, &key, cache_compare_func);
132 return (GdkX11Cursor *) res->data;
137 /* Called by gdk_x11_display_finalize to flush any cached cursors
138 * for a dead display.
141 _gdk_x11_cursor_display_finalize (GdkDisplay *display)
144 GSList** itemp; /* Pointer to the thing to fix when we delete an item */
146 itemp = &cursor_cache;
149 GdkX11Cursor* cursor = (GdkX11Cursor*)(item->data);
150 if (gdk_cursor_get_display (GDK_CURSOR (cursor)) == display)
153 g_object_unref ((GdkCursor*) cursor);
154 /* Remove this item from the list */
155 *(itemp) = item->next;
157 item = g_slist_next (item);
158 g_slist_free_1 (olditem);
162 itemp = &(item->next);
163 item = g_slist_next (item);
168 /*** GdkX11Cursor ***/
170 G_DEFINE_TYPE (GdkX11Cursor, gdk_x11_cursor, GDK_TYPE_CURSOR)
172 static GdkPixbuf* gdk_x11_cursor_get_image (GdkCursor *cursor);
175 gdk_x11_cursor_finalize (GObject *object)
177 GdkX11Cursor *private = GDK_X11_CURSOR (object);
180 display = gdk_cursor_get_display (GDK_CURSOR (object));
181 if (private->xcursor && !gdk_display_is_closed (display))
182 XFreeCursor (GDK_DISPLAY_XDISPLAY (display), private->xcursor);
184 g_free (private->name);
186 G_OBJECT_CLASS (gdk_x11_cursor_parent_class)->finalize (object);
190 gdk_x11_cursor_class_init (GdkX11CursorClass *xcursor_class)
192 GdkCursorClass *cursor_class = GDK_CURSOR_CLASS (xcursor_class);
193 GObjectClass *object_class = G_OBJECT_CLASS (xcursor_class);
195 object_class->finalize = gdk_x11_cursor_finalize;
197 cursor_class->get_image = gdk_x11_cursor_get_image;
201 gdk_x11_cursor_init (GdkX11Cursor *cursor)
206 get_blank_cursor (GdkDisplay *display)
212 cairo_surface_t *surface;
215 screen = gdk_display_get_default_screen (display);
216 surface = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen), 1, 1);
218 cr = cairo_create (surface);
219 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
223 pixmap = cairo_xlib_surface_get_drawable (surface);
226 color.red = color.blue = color.green = 0;
228 if (gdk_display_is_closed (display))
231 cursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
233 &color, &color, 1, 1);
234 cairo_surface_destroy (surface);
240 _gdk_x11_display_get_cursor_for_type (GdkDisplay *display,
241 GdkCursorType cursor_type)
243 GdkX11Cursor *private;
246 if (gdk_display_is_closed (display))
252 private = find_in_cache (display, cursor_type, NULL);
256 /* Cache had it, add a ref for this user */
257 g_object_ref (private);
259 return (GdkCursor*) private;
263 if (cursor_type != GDK_BLANK_CURSOR)
264 xcursor = XCreateFontCursor (GDK_DISPLAY_XDISPLAY (display),
267 xcursor = get_blank_cursor (display);
271 private = g_object_new (GDK_TYPE_X11_CURSOR,
272 "cursor-type", cursor_type,
275 private->xcursor = xcursor;
276 private->name = NULL;
277 private->serial = theme_serial;
280 add_to_cache (private);
282 return GDK_CURSOR (private);
286 * gdk_x11_cursor_get_xdisplay:
287 * @cursor: (type GdkX11Cursor): a #GdkCursor.
289 * Returns the display of a #GdkCursor.
291 * Return value: (transfer none): an Xlib <type>Display*</type>.
294 gdk_x11_cursor_get_xdisplay (GdkCursor *cursor)
296 g_return_val_if_fail (cursor != NULL, NULL);
298 return GDK_DISPLAY_XDISPLAY (gdk_cursor_get_display (cursor));
302 * gdk_x11_cursor_get_xcursor:
303 * @cursor: (type GdkX11Cursor): a #GdkCursor.
305 * Returns the X cursor belonging to a #GdkCursor.
307 * Return value: an Xlib <type>Cursor</type>.
310 gdk_x11_cursor_get_xcursor (GdkCursor *cursor)
312 g_return_val_if_fail (cursor != NULL, None);
314 return ((GdkX11Cursor *)cursor)->xcursor;
317 #if defined(HAVE_XCURSOR) && defined(HAVE_XFIXES) && XFIXES_MAJOR >= 2
320 gdk_x11_cursor_get_image (GdkCursor *cursor)
323 GdkX11Cursor *private;
324 XcursorImages *images = NULL;
328 guchar *data, *p, tmp;
332 private = GDK_X11_CURSOR (cursor);
334 xdisplay = GDK_DISPLAY_XDISPLAY (gdk_cursor_get_display (cursor));
336 size = XcursorGetDefaultSize (xdisplay);
337 theme = XcursorGetTheme (xdisplay);
339 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
342 images = XcursorLibraryLoadImages (private->name, theme, size);
345 images = XcursorShapeLoadImages (cursor->type, theme, size);
350 image = images->images[0];
352 data = g_malloc (4 * image->width * image->height);
353 memcpy (data, image->pixels, 4 * image->width * image->height);
355 for (p = data; p < data + (4 * image->width * image->height); p += 4)
362 pixbuf = gdk_pixbuf_new_from_data (data, GDK_COLORSPACE_RGB, TRUE,
363 8, image->width, image->height,
365 (GdkPixbufDestroyNotify)g_free, NULL);
368 gdk_pixbuf_set_option (pixbuf, "name", private->name);
369 g_snprintf (buf, 32, "%d", image->xhot);
370 gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
371 g_snprintf (buf, 32, "%d", image->yhot);
372 gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
374 XcursorImagesDestroy (images);
380 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
383 GdkX11Cursor *private;
384 Cursor new_cursor = None;
385 GdkX11Display *display_x11;
387 private = (GdkX11Cursor *) cursor;
388 display_x11 = GDK_X11_DISPLAY (gdk_cursor_get_display (cursor));
389 xdisplay = GDK_DISPLAY_XDISPLAY (display_x11);
391 if (!display_x11->have_xfixes)
394 if (private->serial == theme_serial)
397 private->serial = theme_serial;
399 if (private->xcursor != None)
401 if (cursor->type == GDK_BLANK_CURSOR)
404 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
407 new_cursor = XcursorLibraryLoadCursor (xdisplay, private->name);
410 new_cursor = XcursorShapeLoadCursor (xdisplay, cursor->type);
412 if (new_cursor != None)
414 XFixesChangeCursor (xdisplay, new_cursor, private->xcursor);
415 private->xcursor = new_cursor;
421 update_cursor (gpointer data,
426 cursor = (GdkCursor*)(data);
431 _gdk_x11_cursor_update_theme (cursor);
435 * gdk_x11_display_set_cursor_theme:
436 * @display: (type GdkX11Display): a #GdkDisplay
437 * @theme: the name of the cursor theme to use, or %NULL to unset
438 * a previously set value
439 * @size: the cursor size to use, or 0 to keep the previous size
441 * Sets the cursor theme from which the images for cursor
444 * If the windowing system supports it, existing cursors created
445 * with gdk_cursor_new(), gdk_cursor_new_for_display() and
446 * gdk_cursor_new_for_name() are updated to reflect the theme
447 * change. Custom cursors constructed with
448 * gdk_cursor_new_from_pixbuf() will have to be handled
449 * by the application (GTK+ applications can learn about
450 * cursor theme changes by listening for change notification
451 * for the corresponding #GtkSetting).
456 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
464 g_return_if_fail (GDK_IS_DISPLAY (display));
466 xdisplay = GDK_DISPLAY_XDISPLAY (display);
468 old_theme = XcursorGetTheme (xdisplay);
469 old_size = XcursorGetDefaultSize (xdisplay);
471 if (old_size == size &&
472 (old_theme == theme ||
473 (old_theme && theme && strcmp (old_theme, theme) == 0)))
478 XcursorSetTheme (xdisplay, theme);
480 XcursorSetDefaultSize (xdisplay, size);
482 g_slist_foreach (cursor_cache, update_cursor, NULL);
488 gdk_x11_cursor_get_image (GdkCursor *cursor)
494 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
498 g_return_if_fail (GDK_IS_DISPLAY (display));
502 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
504 g_return_if_fail (cursor != NULL);
512 create_cursor_image (GdkPixbuf *pixbuf,
517 XcursorImage *xcimage;
518 cairo_surface_t *surface;
521 width = gdk_pixbuf_get_width (pixbuf);
522 height = gdk_pixbuf_get_height (pixbuf);
524 xcimage = XcursorImageCreate (width, height);
529 surface = cairo_image_surface_create_for_data ((guchar *) xcimage->pixels,
535 cr = cairo_create (surface);
536 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
537 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
541 cairo_surface_destroy (surface);
547 _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
552 XcursorImage *xcimage;
554 GdkX11Cursor *private;
559 if (x == -1 && (option = gdk_pixbuf_get_option (pixbuf, "x_hot")))
563 value = g_ascii_strtoll (option, &end, 10);
566 value >= 0 && value < G_MAXINT)
569 if (y == -1 && (option = gdk_pixbuf_get_option (pixbuf, "y_hot")))
573 value = g_ascii_strtoll (option, &end, 10);
576 value >= 0 && value < G_MAXINT)
580 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
581 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
583 if (gdk_display_is_closed (display))
589 xcimage = create_cursor_image (pixbuf, x, y);
590 xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
591 XcursorImageDestroy (xcimage);
594 private = g_object_new (GDK_TYPE_X11_CURSOR,
595 "cursor-type", GDK_CURSOR_IS_PIXMAP,
598 private->xcursor = xcursor;
599 private->name = NULL;
600 private->serial = theme_serial;
602 return GDK_CURSOR (private);
606 _gdk_x11_display_get_cursor_for_name (GdkDisplay *display,
611 GdkX11Cursor *private;
613 if (gdk_display_is_closed (display))
619 private = find_in_cache (display, GDK_CURSOR_IS_PIXMAP, name);
623 /* Cache had it, add a ref for this user */
624 g_object_ref (private);
626 return (GdkCursor*) private;
629 xdisplay = GDK_DISPLAY_XDISPLAY (display);
630 xcursor = XcursorLibraryLoadCursor (xdisplay, name);
635 private = g_object_new (GDK_TYPE_X11_CURSOR,
636 "cursor-type", GDK_CURSOR_IS_PIXMAP,
639 private->xcursor = xcursor;
640 private->name = g_strdup (name);
641 private->serial = theme_serial;
643 add_to_cache (private);
645 return GDK_CURSOR (private);
649 _gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
651 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
655 _gdk_x11_display_supports_cursor_color (GdkDisplay *display)
657 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
661 _gdk_x11_display_get_default_cursor_size (GdkDisplay *display,
665 *width = *height = XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
671 gdk_cursor_new_from_pixmap (GdkDisplay *display,
672 Pixmap source_pixmap,
679 GdkX11Cursor *private;
683 g_return_val_if_fail (fg != NULL, NULL);
684 g_return_val_if_fail (bg != NULL, NULL);
686 xfg.red = fg->red * 65535;
687 xfg.blue = fg->blue * 65535;
688 xfg.green = fg->green * 65535;
690 xbg.red = bg->red * 65535;
691 xbg.blue = bg->blue * 65535;
692 xbg.green = bg->green * 65535;
694 if (gdk_display_is_closed (display))
697 xcursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
698 source_pixmap, mask_pixmap, &xfg, &xbg, x, y);
699 private = g_object_new (GDK_TYPE_X11_CURSOR,
700 "cursor-type", GDK_CURSOR_IS_PIXMAP,
703 private->xcursor = xcursor;
704 private->name = NULL;
705 private->serial = theme_serial;
707 return GDK_CURSOR (private);
711 _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
717 cairo_surface_t *pixmap, *mask;
718 guint width, height, n_channels, rowstride, data_stride, i, j;
719 guint8 *data, *mask_data, *pixels;
720 GdkRGBA fg = { 0, 0, 0, 1 };
721 GdkRGBA bg = { 1, 1, 1, 1 };
723 cairo_surface_t *image;
726 width = gdk_pixbuf_get_width (pixbuf);
727 height = gdk_pixbuf_get_height (pixbuf);
729 g_return_val_if_fail (0 <= x && x < width, NULL);
730 g_return_val_if_fail (0 <= y && y < height, NULL);
732 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
733 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
734 pixels = gdk_pixbuf_get_pixels (pixbuf);
736 data_stride = 4 * ((width + 31) / 32);
737 data = g_new0 (guint8, data_stride * height);
738 mask_data = g_new0 (guint8, data_stride * height);
740 for (j = 0; j < height; j++)
742 guint8 *src = pixels + j * rowstride;
743 guint8 *d = data + data_stride * j;
744 guint8 *md = mask_data + data_stride * j;
746 for (i = 0; i < width; i++)
751 if (n_channels == 3 || src[3] >= 0x80)
763 screen = gdk_display_get_default_screen (display);
765 pixmap = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
767 cr = cairo_create (pixmap);
768 image = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_A1,
769 width, height, data_stride);
770 cairo_set_source_surface (cr, image, 0, 0);
771 cairo_surface_destroy (image);
772 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
776 mask = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
778 cr = cairo_create (mask);
779 image = cairo_image_surface_create_for_data (mask_data, CAIRO_FORMAT_A1,
780 width, height, data_stride);
781 cairo_set_source_surface (cr, image, 0, 0);
782 cairo_surface_destroy (image);
783 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
787 cursor = gdk_cursor_new_from_pixmap (display,
788 cairo_xlib_surface_get_drawable (pixmap),
789 cairo_xlib_surface_get_drawable (mask),
793 cairo_surface_destroy (pixmap);
794 cairo_surface_destroy (mask);
803 _gdk_x11_display_get_cursor_for_name (GdkDisplay *display,
810 _gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
816 _gdk_x11_display_supports_cursor_color (GdkDisplay *display)
822 _gdk_x11_display_get_default_cursor_size (GdkDisplay *display,
826 /* no idea, really */
827 *width = *height = 20;
834 _gdk_x11_display_get_maximal_cursor_size (GdkDisplay *display,
841 g_return_if_fail (GDK_IS_DISPLAY (display));
843 screen = gdk_display_get_default_screen (display);
844 window = gdk_screen_get_root_window (screen);
845 XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display),
846 GDK_WINDOW_XID (window),
847 128, 128, width, height);