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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 /* needs to be first because any header might include gdk-pixbuf.h otherwise */
30 #define GDK_PIXBUF_ENABLE_BACKEND
31 #include <gdk-pixbuf/gdk-pixbuf.h>
33 #include "gdkcursor.h"
34 #include "gdkcursorprivate.h"
35 #include "gdkprivate-x11.h"
36 #include "gdkdisplay-x11.h"
39 #include <X11/cursorfont.h>
41 #include <X11/Xcursor/Xcursor.h>
44 #include <X11/extensions/Xfixes.h>
58 struct _GdkX11CursorClass
60 GdkCursorClass cursor_class;
63 static guint theme_serial = 0;
65 /* cursor_cache holds a cache of non-pixmap cursors to avoid expensive
66 * libXcursor searches, cursors are added to it but only removed when
67 * their display is closed. We make the assumption that since there are
68 * a small number of display's and a small number of cursor's that this
69 * list will stay small enough not to be a problem.
71 static GSList* cursor_cache = NULL;
73 struct cursor_cache_key
80 /* Caller should check if there is already a match first.
81 * Cursor MUST be either a typed cursor or a pixmap with
85 add_to_cache (GdkX11Cursor* cursor)
87 cursor_cache = g_slist_prepend (cursor_cache, cursor);
89 /* Take a ref so that if the caller frees it we still have it */
90 g_object_ref (cursor);
93 /* Returns 0 on a match
96 cache_compare_func (gconstpointer listelem,
99 GdkX11Cursor* cursor = (GdkX11Cursor*)listelem;
100 struct cursor_cache_key* key = (struct cursor_cache_key*)target;
102 if ((cursor->cursor.type != key->type) ||
103 (gdk_cursor_get_display (GDK_CURSOR (cursor)) != key->display))
104 return 1; /* No match */
106 /* Elements marked as pixmap must be named cursors
107 * (since we don't store normal pixmap cursors
109 if (key->type == GDK_CURSOR_IS_PIXMAP)
110 return strcmp (key->name, cursor->name);
112 return 0; /* Match */
115 /* Returns the cursor if there is a match, NULL if not
116 * For named cursors type shall be GDK_CURSOR_IS_PIXMAP
117 * For unnamed, typed cursors, name shall be NULL
120 find_in_cache (GdkDisplay *display,
125 struct cursor_cache_key key;
127 key.display = display;
131 res = g_slist_find_custom (cursor_cache, &key, cache_compare_func);
134 return (GdkX11Cursor *) res->data;
139 /* Called by gdk_x11_display_finalize to flush any cached cursors
140 * for a dead display.
143 _gdk_x11_cursor_display_finalize (GdkDisplay *display)
146 GSList** itemp; /* Pointer to the thing to fix when we delete an item */
148 itemp = &cursor_cache;
151 GdkX11Cursor* cursor = (GdkX11Cursor*)(item->data);
152 if (gdk_cursor_get_display (GDK_CURSOR (cursor)) == display)
155 gdk_cursor_unref ((GdkCursor*) cursor);
156 /* Remove this item from the list */
157 *(itemp) = item->next;
159 item = g_slist_next (item);
160 g_slist_free_1 (olditem);
164 itemp = &(item->next);
165 item = g_slist_next (item);
170 /*** GdkX11Cursor ***/
172 G_DEFINE_TYPE (GdkX11Cursor, gdk_x11_cursor, GDK_TYPE_CURSOR)
174 static GdkPixbuf* gdk_x11_cursor_get_image (GdkCursor *cursor);
177 gdk_x11_cursor_finalize (GObject *object)
179 GdkX11Cursor *private = GDK_X11_CURSOR (object);
182 display = gdk_cursor_get_display (GDK_CURSOR (object));
183 if (private->xcursor && !gdk_display_is_closed (display))
184 XFreeCursor (GDK_DISPLAY_XDISPLAY (display), private->xcursor);
186 g_free (private->name);
188 G_OBJECT_CLASS (gdk_x11_cursor_parent_class)->finalize (object);
192 gdk_x11_cursor_class_init (GdkX11CursorClass *xcursor_class)
194 GdkCursorClass *cursor_class = GDK_CURSOR_CLASS (xcursor_class);
195 GObjectClass *object_class = G_OBJECT_CLASS (xcursor_class);
197 object_class->finalize = gdk_x11_cursor_finalize;
199 cursor_class->get_image = gdk_x11_cursor_get_image;
203 gdk_x11_cursor_init (GdkX11Cursor *cursor)
208 get_blank_cursor (GdkDisplay *display)
214 cairo_surface_t *surface;
217 screen = gdk_display_get_default_screen (display);
218 surface = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen), 1, 1);
220 cr = cairo_create (surface);
221 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
225 pixmap = cairo_xlib_surface_get_drawable (surface);
228 color.red = color.blue = color.green = 0;
230 if (gdk_display_is_closed (display))
233 cursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
235 &color, &color, 1, 1);
236 cairo_surface_destroy (surface);
242 _gdk_x11_display_get_cursor_for_type (GdkDisplay *display,
243 GdkCursorType cursor_type)
245 GdkX11Cursor *private;
248 if (gdk_display_is_closed (display))
254 private = find_in_cache (display, cursor_type, NULL);
258 /* Cache had it, add a ref for this user */
259 g_object_ref (private);
261 return (GdkCursor*) private;
265 if (cursor_type != GDK_BLANK_CURSOR)
266 xcursor = XCreateFontCursor (GDK_DISPLAY_XDISPLAY (display),
269 xcursor = get_blank_cursor (display);
273 private = g_object_new (GDK_TYPE_X11_CURSOR,
274 "cursor-type", GDK_CURSOR_IS_PIXMAP,
277 private->xcursor = xcursor;
278 private->name = NULL;
279 private->serial = theme_serial;
282 add_to_cache (private);
284 return GDK_CURSOR (private);
288 * gdk_x11_cursor_get_xdisplay:
289 * @cursor: a #GdkCursor.
291 * Returns the display of a #GdkCursor.
293 * Return value: an Xlib <type>Display*</type>.
296 gdk_x11_cursor_get_xdisplay (GdkCursor *cursor)
298 g_return_val_if_fail (cursor != NULL, NULL);
300 return GDK_DISPLAY_XDISPLAY (gdk_cursor_get_display (cursor));
304 * gdk_x11_cursor_get_xcursor:
305 * @cursor: a #GdkCursor.
307 * Returns the X cursor belonging to a #GdkCursor.
309 * Return value: an Xlib <type>Cursor</type>.
312 gdk_x11_cursor_get_xcursor (GdkCursor *cursor)
314 g_return_val_if_fail (cursor != NULL, None);
316 return ((GdkX11Cursor *)cursor)->xcursor;
319 #if defined(HAVE_XCURSOR) && defined(HAVE_XFIXES) && XFIXES_MAJOR >= 2
322 gdk_x11_cursor_get_image (GdkCursor *cursor)
325 GdkX11Cursor *private;
326 XcursorImages *images = NULL;
330 guchar *data, *p, tmp;
334 private = GDK_X11_CURSOR (cursor);
336 xdisplay = GDK_DISPLAY_XDISPLAY (gdk_cursor_get_display (cursor));
338 size = XcursorGetDefaultSize (xdisplay);
339 theme = XcursorGetTheme (xdisplay);
341 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
344 images = XcursorLibraryLoadImages (private->name, theme, size);
347 images = XcursorShapeLoadImages (cursor->type, theme, size);
352 image = images->images[0];
354 data = g_malloc (4 * image->width * image->height);
355 memcpy (data, image->pixels, 4 * image->width * image->height);
357 for (p = data; p < data + (4 * image->width * image->height); p += 4)
364 pixbuf = gdk_pixbuf_new_from_data (data, GDK_COLORSPACE_RGB, TRUE,
365 8, image->width, image->height,
367 (GdkPixbufDestroyNotify)g_free, NULL);
370 gdk_pixbuf_set_option (pixbuf, "name", private->name);
371 g_snprintf (buf, 32, "%d", image->xhot);
372 gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
373 g_snprintf (buf, 32, "%d", image->yhot);
374 gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
376 XcursorImagesDestroy (images);
382 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
385 GdkX11Cursor *private;
386 Cursor new_cursor = None;
387 GdkX11Display *display_x11;
389 private = (GdkX11Cursor *) cursor;
390 display_x11 = GDK_X11_DISPLAY (gdk_cursor_get_display (cursor));
391 xdisplay = GDK_DISPLAY_XDISPLAY (display_x11);
393 if (!display_x11->have_xfixes)
396 if (private->serial == theme_serial)
399 private->serial = theme_serial;
401 if (private->xcursor != None)
403 if (cursor->type == GDK_BLANK_CURSOR)
406 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
409 new_cursor = XcursorLibraryLoadCursor (xdisplay, private->name);
412 new_cursor = XcursorShapeLoadCursor (xdisplay, cursor->type);
414 if (new_cursor != None)
416 XFixesChangeCursor (xdisplay, new_cursor, private->xcursor);
417 private->xcursor = new_cursor;
423 update_cursor (gpointer data,
428 cursor = (GdkCursor*)(data);
433 _gdk_x11_cursor_update_theme (cursor);
437 * gdk_x11_display_set_cursor_theme:
438 * @display: a #GdkDisplay
439 * @theme: the name of the cursor theme to use, or %NULL to unset
440 * a previously set value
441 * @size: the cursor size to use, or 0 to keep the previous size
443 * Sets the cursor theme from which the images for cursor
446 * If the windowing system supports it, existing cursors created
447 * with gdk_cursor_new(), gdk_cursor_new_for_display() and
448 * gdk_cursor_new_for_name() are updated to reflect the theme
449 * change. Custom cursors constructed with
450 * gdk_cursor_new_from_pixbuf() will have to be handled
451 * by the application (GTK+ applications can learn about
452 * cursor theme changes by listening for change notification
453 * for the corresponding #GtkSetting).
458 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
462 GdkX11Display *display_x11;
467 g_return_if_fail (GDK_IS_DISPLAY (display));
469 display_x11 = GDK_X11_DISPLAY (display);
470 xdisplay = GDK_DISPLAY_XDISPLAY (display);
472 old_theme = XcursorGetTheme (xdisplay);
473 old_size = XcursorGetDefaultSize (xdisplay);
475 if (old_size == size &&
476 (old_theme == theme ||
477 (old_theme && theme && strcmp (old_theme, theme) == 0)))
482 XcursorSetTheme (xdisplay, theme);
484 XcursorSetDefaultSize (xdisplay, size);
486 g_slist_foreach (cursor_cache, update_cursor, NULL);
492 gdk_x11_cursor_get_image (GdkCursor *cursor)
498 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
502 g_return_if_fail (GDK_IS_DISPLAY (display));
506 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
508 g_return_if_fail (cursor != NULL);
516 create_cursor_image (GdkPixbuf *pixbuf,
521 XcursorImage *xcimage;
522 cairo_surface_t *surface;
525 width = gdk_pixbuf_get_width (pixbuf);
526 height = gdk_pixbuf_get_height (pixbuf);
528 xcimage = XcursorImageCreate (width, height);
533 surface = cairo_image_surface_create_for_data ((guchar *) xcimage->pixels,
539 cr = cairo_create (surface);
540 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
541 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
545 cairo_surface_destroy (surface);
551 _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
556 XcursorImage *xcimage;
558 GdkX11Cursor *private;
563 if (x == -1 && (option = gdk_pixbuf_get_option (pixbuf, "x_hot")))
567 value = g_ascii_strtoll (option, &end, 10);
570 value >= 0 && value < G_MAXINT)
573 if (y == -1 && (option = gdk_pixbuf_get_option (pixbuf, "y_hot")))
577 value = g_ascii_strtoll (option, &end, 10);
580 value >= 0 && value < G_MAXINT)
584 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
585 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
587 if (gdk_display_is_closed (display))
593 xcimage = create_cursor_image (pixbuf, x, y);
594 xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
595 XcursorImageDestroy (xcimage);
598 private = g_object_new (GDK_TYPE_X11_CURSOR,
599 "cursor-type", GDK_CURSOR_IS_PIXMAP,
602 private->xcursor = xcursor;
603 private->name = NULL;
604 private->serial = theme_serial;
606 return GDK_CURSOR (private);
610 _gdk_x11_display_get_cursor_for_name (GdkDisplay *display,
615 GdkX11Cursor *private;
617 if (gdk_display_is_closed (display))
623 private = find_in_cache (display, GDK_CURSOR_IS_PIXMAP, name);
627 /* Cache had it, add a ref for this user */
628 g_object_ref (private);
630 return (GdkCursor*) private;
633 xdisplay = GDK_DISPLAY_XDISPLAY (display);
634 xcursor = XcursorLibraryLoadCursor (xdisplay, name);
639 private = g_object_new (GDK_TYPE_X11_CURSOR,
640 "cursor-type", GDK_CURSOR_IS_PIXMAP,
643 private->xcursor = xcursor;
644 private->name = g_strdup (name);
645 private->serial = theme_serial;
647 add_to_cache (private);
649 return GDK_CURSOR (private);
653 _gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
655 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
659 _gdk_x11_display_supports_cursor_color (GdkDisplay *display)
661 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
665 _gdk_x11_display_get_default_cursor_size (GdkDisplay *display,
669 *width = *height = XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
675 gdk_cursor_new_from_pixmap (GdkDisplay *display,
676 Pixmap source_pixmap,
683 GdkX11Cursor *private;
687 g_return_val_if_fail (fg != NULL, NULL);
688 g_return_val_if_fail (bg != NULL, NULL);
690 xfg.pixel = fg->pixel;
693 xfg.green = fg->green;
694 xbg.pixel = bg->pixel;
697 xbg.green = bg->green;
699 if (gdk_display_is_closed (display->closed))
702 xcursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
703 source_pixmap, mask_pixmap, &xfg, &xbg, x, y);
704 private = g_object_new (GDK_TYPE_X11_CURSOR,
705 "cursor-type", GDK_CURSOR_IS_PIXMAP,
708 private->xcursor = xcursor;
709 private->name = NULL;
710 private->serial = theme_serial;
712 return GDK_CURSOR (private);
716 _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
722 cairo_surface_t *pixmap, *mask;
723 guint width, height, n_channels, rowstride, data_stride, i, j;
724 guint8 *data, *mask_data, *pixels;
725 GdkColor fg = { 0, 0, 0, 0 };
726 GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
728 cairo_surface_t *image;
731 width = gdk_pixbuf_get_width (pixbuf);
732 height = gdk_pixbuf_get_height (pixbuf);
734 g_return_val_if_fail (0 <= x && x < width, NULL);
735 g_return_val_if_fail (0 <= y && y < height, NULL);
737 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
738 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
739 pixels = gdk_pixbuf_get_pixels (pixbuf);
741 data_stride = 4 * ((width + 31) / 32);
742 data = g_new0 (guint8, data_stride * height);
743 mask_data = g_new0 (guint8, data_stride * height);
745 for (j = 0; j < height; j++)
747 guint8 *src = pixels + j * rowstride;
748 guint8 *d = data + data_stride * j;
749 guint8 *md = mask_data + data_stride * j;
751 for (i = 0; i < width; i++)
756 if (n_channels == 3 || src[3] >= 0x80)
768 screen = gdk_display_get_default_screen (display);
770 pixmap = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
772 cr = cairo_create (pixmap);
773 image = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_A1,
774 width, height, data_stride);
775 cairo_set_source_surface (cr, image, 0, 0);
776 cairo_surface_destroy (image);
777 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
781 mask = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
783 cr = cairo_create (mask);
784 image = cairo_image_surface_create_for_data (mask_data, CAIRO_FORMAT_A1,
785 width, height, data_stride);
786 cairo_set_source_surface (cr, image, 0, 0);
787 cairo_surface_destroy (image);
788 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
792 cursor = gdk_cursor_new_from_pixmap (display,
793 cairo_xlib_surface_get_drawable (pixmap),
794 cairo_xlib_surface_get_drawable (mask),
798 cairo_surface_destroy (pixmap);
799 cairo_surface_destroy (mask);
808 _gdk_x11_display_get_cursor_for_name (GdkDisplay *display,
815 _gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
821 _gdk_x11_display_supports_cursor_color (GdkDisplay *display)
827 _gdk_x11_display_get_default_cursor_size (GdkDisplay *display)
829 /* no idea, really */
836 _gdk_x11_display_get_maximal_cursor_size (GdkDisplay *display,
843 g_return_if_fail (GDK_IS_DISPLAY (display));
845 screen = gdk_display_get_default_screen (display);
846 window = gdk_screen_get_root_window (screen);
847 XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display),
848 GDK_WINDOW_XID (window),
849 128, 128, width, height);