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 "gdkprivate-x11.h"
35 #include "gdkdisplay-x11.h"
38 #include <X11/cursorfont.h>
40 #include <X11/Xcursor/Xcursor.h>
43 #include <X11/extensions/Xfixes.h>
48 typedef struct _GdkCursorPrivate GdkCursorPrivate;
50 struct _GdkCursorPrivate
60 static guint theme_serial = 0;
62 /* cursor_cache holds a cache of non-pixmap cursors to avoid expensive
63 * libXcursor searches, cursors are added to it but only removed when
64 * their display is closed. We make the assumption that since there are
65 * a small number of display's and a small number of cursor's that this
66 * list will stay small enough not to be a problem.
68 static GSList* cursor_cache = NULL;
70 struct cursor_cache_key
77 /* Caller should check if there is already a match first.
78 * Cursor MUST be either a typed cursor or a pixmap with
82 add_to_cache (GdkCursorPrivate* cursor)
84 cursor_cache = g_slist_prepend (cursor_cache, cursor);
86 /* Take a ref so that if the caller frees it we still have it */
87 gdk_cursor_ref ((GdkCursor*) cursor);
90 /* Returns 0 on a match
93 cache_compare_func (gconstpointer listelem,
96 GdkCursorPrivate* cursor = (GdkCursorPrivate*)listelem;
97 struct cursor_cache_key* key = (struct cursor_cache_key*)target;
99 if ((cursor->cursor.type != key->type) ||
100 (cursor->display != key->display))
101 return 1; /* No match */
103 /* Elements marked as pixmap must be named cursors
104 * (since we don't store normal pixmap cursors
106 if (key->type == GDK_CURSOR_IS_PIXMAP)
107 return strcmp (key->name, cursor->name);
109 return 0; /* Match */
112 /* Returns the cursor if there is a match, NULL if not
113 * For named cursors type shall be GDK_CURSOR_IS_PIXMAP
114 * For unnamed, typed cursors, name shall be NULL
116 static GdkCursorPrivate*
117 find_in_cache (GdkDisplay *display,
122 struct cursor_cache_key key;
124 key.display = display;
128 res = g_slist_find_custom (cursor_cache, &key, cache_compare_func);
131 return (GdkCursorPrivate *) res->data;
136 /* Called by gdk_display_x11_finalize to flush any cached cursors
137 * for a dead display.
140 _gdk_x11_cursor_display_finalize (GdkDisplay *display)
143 GSList** itemp; /* Pointer to the thing to fix when we delete an item */
145 itemp = &cursor_cache;
148 GdkCursorPrivate* cursor = (GdkCursorPrivate*)(item->data);
149 if (cursor->display == display)
152 gdk_cursor_unref ((GdkCursor*) cursor);
153 /* Remove this item from the list */
154 *(itemp) = item->next;
156 item = g_slist_next (item);
157 g_slist_free_1 (olditem);
161 itemp = &(item->next);
162 item = g_slist_next (item);
168 get_blank_cursor (GdkDisplay *display)
174 cairo_surface_t *surface;
177 screen = gdk_display_get_default_screen (display);
178 surface = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen), 1, 1);
180 cr = cairo_create (surface);
181 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
185 pixmap = cairo_xlib_surface_get_drawable (surface);
188 color.red = color.blue = color.green = 0;
190 if (gdk_display_is_closed (display))
193 cursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
195 &color, &color, 1, 1);
196 cairo_surface_destroy (surface);
202 _gdk_x11_display_get_cursor_for_type (GdkDisplay *display,
203 GdkCursorType cursor_type)
205 GdkCursorPrivate *private;
209 if (gdk_display_is_closed (display))
215 private = find_in_cache (display, cursor_type, NULL);
219 /* Cache had it, add a ref for this user */
220 gdk_cursor_ref ((GdkCursor*) private);
222 return (GdkCursor*) private;
226 if (cursor_type != GDK_BLANK_CURSOR)
227 xcursor = XCreateFontCursor (GDK_DISPLAY_XDISPLAY (display),
230 xcursor = get_blank_cursor (display);
234 private = g_new (GdkCursorPrivate, 1);
235 private->display = display;
236 private->xcursor = xcursor;
237 private->name = NULL;
238 private->serial = theme_serial;
240 cursor = (GdkCursor *) private;
241 cursor->type = cursor_type;
242 cursor->ref_count = 1;
245 add_to_cache (private);
251 _gdk_cursor_destroy (GdkCursor *cursor)
253 GdkCursorPrivate *private;
255 g_return_if_fail (cursor != NULL);
256 g_return_if_fail (cursor->ref_count == 0);
258 private = (GdkCursorPrivate *) cursor;
259 if (private->xcursor && !gdk_display_is_closed (private->display))
260 XFreeCursor (GDK_DISPLAY_XDISPLAY (private->display), private->xcursor);
262 g_free (private->name);
267 * gdk_x11_cursor_get_xdisplay:
268 * @cursor: a #GdkCursor.
270 * Returns the display of a #GdkCursor.
272 * Return value: an Xlib <type>Display*</type>.
275 gdk_x11_cursor_get_xdisplay (GdkCursor *cursor)
277 g_return_val_if_fail (cursor != NULL, NULL);
279 return GDK_DISPLAY_XDISPLAY(((GdkCursorPrivate *)cursor)->display);
283 * gdk_x11_cursor_get_xcursor:
284 * @cursor: a #GdkCursor.
286 * Returns the X cursor belonging to a #GdkCursor.
288 * Return value: an Xlib <type>Cursor</type>.
291 gdk_x11_cursor_get_xcursor (GdkCursor *cursor)
293 g_return_val_if_fail (cursor != NULL, None);
295 return ((GdkCursorPrivate *)cursor)->xcursor;
299 * gdk_cursor_get_display:
300 * @cursor: a #GdkCursor.
302 * Returns the display on which the #GdkCursor is defined.
304 * Returns: (transfer none): the #GdkDisplay associated to @cursor
310 gdk_cursor_get_display (GdkCursor *cursor)
312 g_return_val_if_fail (cursor != NULL, NULL);
314 return ((GdkCursorPrivate *)cursor)->display;
317 #if defined(HAVE_XCURSOR) && defined(HAVE_XFIXES) && XFIXES_MAJOR >= 2
320 * gdk_cursor_get_image:
321 * @cursor: a #GdkCursor
323 * Returns a #GdkPixbuf with the image used to display the cursor.
325 * Note that depending on the capabilities of the windowing system and
326 * on the cursor, GDK may not be able to obtain the image data. In this
327 * case, %NULL is returned.
329 * Returns: (transfer full): a #GdkPixbuf representing @cursor, or %NULL
334 gdk_cursor_get_image (GdkCursor *cursor)
337 GdkCursorPrivate *private;
338 XcursorImages *images = NULL;
342 guchar *data, *p, tmp;
346 g_return_val_if_fail (cursor != NULL, NULL);
348 private = (GdkCursorPrivate *) cursor;
350 xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
352 size = XcursorGetDefaultSize (xdisplay);
353 theme = XcursorGetTheme (xdisplay);
355 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
358 images = XcursorLibraryLoadImages (private->name, theme, size);
361 images = XcursorShapeLoadImages (cursor->type, theme, size);
366 image = images->images[0];
368 data = g_malloc (4 * image->width * image->height);
369 memcpy (data, image->pixels, 4 * image->width * image->height);
371 for (p = data; p < data + (4 * image->width * image->height); p += 4)
378 pixbuf = gdk_pixbuf_new_from_data (data, GDK_COLORSPACE_RGB, TRUE,
379 8, image->width, image->height,
381 (GdkPixbufDestroyNotify)g_free, NULL);
384 gdk_pixbuf_set_option (pixbuf, "name", private->name);
385 g_snprintf (buf, 32, "%d", image->xhot);
386 gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
387 g_snprintf (buf, 32, "%d", image->yhot);
388 gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
390 XcursorImagesDestroy (images);
396 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
399 GdkCursorPrivate *private;
400 Cursor new_cursor = None;
401 GdkDisplayX11 *display_x11;
403 private = (GdkCursorPrivate *) cursor;
404 xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
405 display_x11 = GDK_DISPLAY_X11 (private->display);
407 if (!display_x11->have_xfixes)
410 if (private->serial == theme_serial)
413 private->serial = theme_serial;
415 if (private->xcursor != None)
417 if (cursor->type == GDK_BLANK_CURSOR)
420 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
423 new_cursor = XcursorLibraryLoadCursor (xdisplay, private->name);
426 new_cursor = XcursorShapeLoadCursor (xdisplay, cursor->type);
428 if (new_cursor != None)
430 XFixesChangeCursor (xdisplay, new_cursor, private->xcursor);
431 private->xcursor = new_cursor;
437 update_cursor (gpointer data,
442 cursor = (GdkCursor*)(data);
447 _gdk_x11_cursor_update_theme (cursor);
451 * gdk_x11_display_set_cursor_theme:
452 * @display: a #GdkDisplay
453 * @theme: the name of the cursor theme to use, or %NULL to unset
454 * a previously set value
455 * @size: the cursor size to use, or 0 to keep the previous size
457 * Sets the cursor theme from which the images for cursor
460 * If the windowing system supports it, existing cursors created
461 * with gdk_cursor_new(), gdk_cursor_new_for_display() and
462 * gdk_cursor_new_for_name() are updated to reflect the theme
463 * change. Custom cursors constructed with
464 * gdk_cursor_new_from_pixbuf() will have to be handled
465 * by the application (GTK+ applications can learn about
466 * cursor theme changes by listening for change notification
467 * for the corresponding #GtkSetting).
472 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
476 GdkDisplayX11 *display_x11;
481 g_return_if_fail (GDK_IS_DISPLAY (display));
483 display_x11 = GDK_DISPLAY_X11 (display);
484 xdisplay = GDK_DISPLAY_XDISPLAY (display);
486 old_theme = XcursorGetTheme (xdisplay);
487 old_size = XcursorGetDefaultSize (xdisplay);
489 if (old_size == size &&
490 (old_theme == theme ||
491 (old_theme && theme && strcmp (old_theme, theme) == 0)))
496 XcursorSetTheme (xdisplay, theme);
498 XcursorSetDefaultSize (xdisplay, size);
500 g_slist_foreach (cursor_cache, update_cursor, NULL);
506 gdk_cursor_get_image (GdkCursor *cursor)
508 g_return_val_if_fail (cursor != NULL, NULL);
514 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
518 g_return_if_fail (GDK_IS_DISPLAY (display));
522 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
524 g_return_if_fail (cursor != NULL);
532 create_cursor_image (GdkPixbuf *pixbuf,
537 XcursorImage *xcimage;
538 cairo_surface_t *surface;
541 width = gdk_pixbuf_get_width (pixbuf);
542 height = gdk_pixbuf_get_height (pixbuf);
544 xcimage = XcursorImageCreate (width, height);
549 surface = cairo_image_surface_create_for_data ((guchar *) xcimage->pixels,
555 cr = cairo_create (surface);
556 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
557 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
561 cairo_surface_destroy (surface);
567 _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
572 XcursorImage *xcimage;
574 GdkCursorPrivate *private;
580 if (x == -1 && (option = gdk_pixbuf_get_option (pixbuf, "x_hot")))
584 value = g_ascii_strtoll (option, &end, 10);
587 value >= 0 && value < G_MAXINT)
590 if (y == -1 && (option = gdk_pixbuf_get_option (pixbuf, "y_hot")))
594 value = g_ascii_strtoll (option, &end, 10);
597 value >= 0 && value < G_MAXINT)
601 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
602 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
604 if (gdk_display_is_closed (display))
610 xcimage = create_cursor_image (pixbuf, x, y);
611 xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
612 XcursorImageDestroy (xcimage);
615 private = g_new (GdkCursorPrivate, 1);
616 private->display = display;
617 private->xcursor = xcursor;
618 private->name = NULL;
619 private->serial = theme_serial;
621 cursor = (GdkCursor *) private;
622 cursor->type = GDK_CURSOR_IS_PIXMAP;
623 cursor->ref_count = 1;
629 _gdk_x11_display_get_cursor_for_name (GdkDisplay *display,
634 GdkCursorPrivate *private;
637 if (gdk_display_is_closed (display))
643 private = find_in_cache (display, GDK_CURSOR_IS_PIXMAP, name);
647 /* Cache had it, add a ref for this user */
648 gdk_cursor_ref ((GdkCursor*) private);
650 return (GdkCursor*) private;
653 xdisplay = GDK_DISPLAY_XDISPLAY (display);
654 xcursor = XcursorLibraryLoadCursor (xdisplay, name);
659 private = g_new (GdkCursorPrivate, 1);
660 private->display = display;
661 private->xcursor = xcursor;
662 private->name = g_strdup (name);
663 private->serial = theme_serial;
665 cursor = (GdkCursor *) private;
666 cursor->type = GDK_CURSOR_IS_PIXMAP;
667 cursor->ref_count = 1;
668 add_to_cache (private);
674 _gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
676 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
680 _gdk_x11_display_supports_cursor_color (GdkDisplay *display)
682 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
686 _gdk_x11_display_get_default_cursor_size (GdkDisplay *display,
690 *width = *height = XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
696 gdk_cursor_new_from_pixmap (GdkDisplay *display,
697 Pixmap source_pixmap,
704 GdkCursorPrivate *private;
709 g_return_val_if_fail (fg != NULL, NULL);
710 g_return_val_if_fail (bg != NULL, NULL);
712 xfg.pixel = fg->pixel;
715 xfg.green = fg->green;
716 xbg.pixel = bg->pixel;
719 xbg.green = bg->green;
721 if (gdk_display_is_closed (display->closed))
724 xcursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
725 source_pixmap, mask_pixmap, &xfg, &xbg, x, y);
726 private = g_new (GdkCursorPrivate, 1);
727 private->display = display;
728 private->xcursor = xcursor;
729 private->name = NULL;
730 private->serial = theme_serial;
732 cursor = (GdkCursor *) private;
733 cursor->type = GDK_CURSOR_IS_PIXMAP;
734 cursor->ref_count = 1;
740 _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
746 cairo_surface_t *pixmap, *mask;
747 guint width, height, n_channels, rowstride, data_stride, i, j;
748 guint8 *data, *mask_data, *pixels;
749 GdkColor fg = { 0, 0, 0, 0 };
750 GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
752 cairo_surface_t *image;
755 width = gdk_pixbuf_get_width (pixbuf);
756 height = gdk_pixbuf_get_height (pixbuf);
758 g_return_val_if_fail (0 <= x && x < width, NULL);
759 g_return_val_if_fail (0 <= y && y < height, NULL);
761 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
762 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
763 pixels = gdk_pixbuf_get_pixels (pixbuf);
765 data_stride = 4 * ((width + 31) / 32);
766 data = g_new0 (guint8, data_stride * height);
767 mask_data = g_new0 (guint8, data_stride * height);
769 for (j = 0; j < height; j++)
771 guint8 *src = pixels + j * rowstride;
772 guint8 *d = data + data_stride * j;
773 guint8 *md = mask_data + data_stride * j;
775 for (i = 0; i < width; i++)
780 if (n_channels == 3 || src[3] >= 0x80)
792 screen = gdk_display_get_default_screen (display);
794 pixmap = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
796 cr = cairo_create (pixmap);
797 image = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_A1,
798 width, height, data_stride);
799 cairo_set_source_surface (cr, image, 0, 0);
800 cairo_surface_destroy (image);
801 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
805 mask = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
807 cr = cairo_create (mask);
808 image = cairo_image_surface_create_for_data (mask_data, CAIRO_FORMAT_A1,
809 width, height, data_stride);
810 cairo_set_source_surface (cr, image, 0, 0);
811 cairo_surface_destroy (image);
812 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
816 cursor = gdk_cursor_new_from_pixmap (display,
817 cairo_xlib_surface_get_drawable (pixmap),
818 cairo_xlib_surface_get_drawable (mask),
822 cairo_surface_destroy (pixmap);
823 cairo_surface_destroy (mask);
832 _gdk_x11_display_get_cursor_for_name (GdkDisplay *display,
839 _gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
845 _gdk_x11_display_supports_cursor_color (GdkDisplay *display)
851 _gdk_x11_display_get_default_cursor_size (GdkDisplay *display)
853 /* no idea, really */
861 * gdk_display_get_maximal_cursor_size:
862 * @display: a #GdkDisplay
863 * @width: (out): the return location for the maximal cursor width
864 * @height: (out): the return location for the maximal cursor height
866 * Gets the maximal size to use for cursors on @display.
871 _gdk_x11_display_get_maximal_cursor_size (GdkDisplay *display,
878 g_return_if_fail (GDK_IS_DISPLAY (display));
880 screen = gdk_display_get_default_screen (display);
881 window = gdk_screen_get_root_window (screen);
882 XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display),
883 GDK_WINDOW_XID (window),
884 128, 128, width, height);