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"
35 #include "gdkprivate-x11.h"
36 #include "gdkdisplay-x11.h"
40 #include <X11/cursorfont.h>
42 #include <X11/Xcursor/Xcursor.h>
45 #include <X11/extensions/Xfixes.h>
51 static guint theme_serial = 0;
53 /* cursor_cache holds a cache of non-pixmap cursors to avoid expensive
54 * libXcursor searches, cursors are added to it but only removed when
55 * their display is closed. We make the assumption that since there are
56 * a small number of display's and a small number of cursor's that this
57 * list will stay small enough not to be a problem.
59 static GSList* cursor_cache = NULL;
61 struct cursor_cache_key
68 /* Caller should check if there is already a match first.
69 * Cursor MUST be either a typed cursor or a pixmap with
73 add_to_cache (GdkCursorPrivate* cursor)
75 cursor_cache = g_slist_prepend (cursor_cache, cursor);
77 /* Take a ref so that if the caller frees it we still have it */
78 gdk_cursor_ref ((GdkCursor*) cursor);
81 /* Returns 0 on a match
84 cache_compare_func (gconstpointer listelem,
87 GdkCursorPrivate* cursor = (GdkCursorPrivate*)listelem;
88 struct cursor_cache_key* key = (struct cursor_cache_key*)target;
90 if ((cursor->cursor.type != key->type) ||
91 (cursor->display != key->display))
92 return 1; /* No match */
94 /* Elements marked as pixmap must be named cursors
95 * (since we don't store normal pixmap cursors
97 if (key->type == GDK_CURSOR_IS_PIXMAP)
98 return strcmp (key->name, cursor->name);
100 return 0; /* Match */
103 /* Returns the cursor if there is a match, NULL if not
104 * For named cursors type shall be GDK_CURSOR_IS_PIXMAP
105 * For unnamed, typed cursors, name shall be NULL
107 static GdkCursorPrivate*
108 find_in_cache (GdkDisplay *display,
113 struct cursor_cache_key key;
115 key.display = display;
119 res = g_slist_find_custom (cursor_cache, &key, cache_compare_func);
122 return (GdkCursorPrivate *) res->data;
127 /* Called by gdk_display_x11_finalize to flush any cached cursors
128 * for a dead display.
131 _gdk_x11_cursor_display_finalize (GdkDisplay *display)
134 GSList** itemp; /* Pointer to the thing to fix when we delete an item */
136 itemp = &cursor_cache;
139 GdkCursorPrivate* cursor = (GdkCursorPrivate*)(item->data);
140 if (cursor->display == display)
143 gdk_cursor_unref ((GdkCursor*) cursor);
144 /* Remove this item from the list */
145 *(itemp) = item->next;
147 item = g_slist_next (item);
148 g_slist_free_1 (olditem);
152 itemp = &(item->next);
153 item = g_slist_next (item);
159 get_blank_cursor (GdkDisplay *display)
165 cairo_surface_t *surface;
168 screen = gdk_display_get_default_screen (display);
169 surface = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen), 1, 1);
171 cr = cairo_create (surface);
172 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
176 pixmap = cairo_xlib_surface_get_drawable (surface);
179 color.red = color.blue = color.green = 0;
181 if (gdk_display_is_closed (display))
184 cursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
186 &color, &color, 1, 1);
187 cairo_surface_destroy (surface);
193 _gdk_x11_display_get_cursor_for_type (GdkDisplay *display,
194 GdkCursorType cursor_type)
196 GdkCursorPrivate *private;
200 if (gdk_display_is_closed (display))
206 private = find_in_cache (display, cursor_type, NULL);
210 /* Cache had it, add a ref for this user */
211 gdk_cursor_ref ((GdkCursor*) private);
213 return (GdkCursor*) private;
217 if (cursor_type != GDK_BLANK_CURSOR)
218 xcursor = XCreateFontCursor (GDK_DISPLAY_XDISPLAY (display),
221 xcursor = get_blank_cursor (display);
225 private = g_new (GdkCursorPrivate, 1);
226 private->display = display;
227 private->xcursor = xcursor;
228 private->name = NULL;
229 private->serial = theme_serial;
231 cursor = (GdkCursor *) private;
232 cursor->type = cursor_type;
233 cursor->ref_count = 1;
236 add_to_cache (private);
242 _gdk_cursor_destroy (GdkCursor *cursor)
244 GdkCursorPrivate *private;
246 g_return_if_fail (cursor != NULL);
247 g_return_if_fail (cursor->ref_count == 0);
249 private = (GdkCursorPrivate *) cursor;
250 if (private->xcursor && !gdk_display_is_closed (private->display))
251 XFreeCursor (GDK_DISPLAY_XDISPLAY (private->display), private->xcursor);
253 g_free (private->name);
258 * gdk_x11_cursor_get_xdisplay:
259 * @cursor: a #GdkCursor.
261 * Returns the display of a #GdkCursor.
263 * Return value: an Xlib <type>Display*</type>.
266 gdk_x11_cursor_get_xdisplay (GdkCursor *cursor)
268 g_return_val_if_fail (cursor != NULL, NULL);
270 return GDK_DISPLAY_XDISPLAY(((GdkCursorPrivate *)cursor)->display);
274 * gdk_x11_cursor_get_xcursor:
275 * @cursor: a #GdkCursor.
277 * Returns the X cursor belonging to a #GdkCursor.
279 * Return value: an Xlib <type>Cursor</type>.
282 gdk_x11_cursor_get_xcursor (GdkCursor *cursor)
284 g_return_val_if_fail (cursor != NULL, None);
286 return ((GdkCursorPrivate *)cursor)->xcursor;
290 * gdk_cursor_get_display:
291 * @cursor: a #GdkCursor.
293 * Returns the display on which the #GdkCursor is defined.
295 * Returns: (transfer none): the #GdkDisplay associated to @cursor
301 gdk_cursor_get_display (GdkCursor *cursor)
303 g_return_val_if_fail (cursor != NULL, NULL);
305 return ((GdkCursorPrivate *)cursor)->display;
308 #if defined(HAVE_XCURSOR) && defined(HAVE_XFIXES) && XFIXES_MAJOR >= 2
311 * gdk_cursor_get_image:
312 * @cursor: a #GdkCursor
314 * Returns a #GdkPixbuf with the image used to display the cursor.
316 * Note that depending on the capabilities of the windowing system and
317 * on the cursor, GDK may not be able to obtain the image data. In this
318 * case, %NULL is returned.
320 * Returns: (transfer full): a #GdkPixbuf representing @cursor, or %NULL
325 gdk_cursor_get_image (GdkCursor *cursor)
328 GdkCursorPrivate *private;
329 XcursorImages *images = NULL;
333 guchar *data, *p, tmp;
337 g_return_val_if_fail (cursor != NULL, NULL);
339 private = (GdkCursorPrivate *) cursor;
341 xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
343 size = XcursorGetDefaultSize (xdisplay);
344 theme = XcursorGetTheme (xdisplay);
346 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
349 images = XcursorLibraryLoadImages (private->name, theme, size);
352 images = XcursorShapeLoadImages (cursor->type, theme, size);
357 image = images->images[0];
359 data = g_malloc (4 * image->width * image->height);
360 memcpy (data, image->pixels, 4 * image->width * image->height);
362 for (p = data; p < data + (4 * image->width * image->height); p += 4)
369 pixbuf = gdk_pixbuf_new_from_data (data, GDK_COLORSPACE_RGB, TRUE,
370 8, image->width, image->height,
372 (GdkPixbufDestroyNotify)g_free, NULL);
375 gdk_pixbuf_set_option (pixbuf, "name", private->name);
376 g_snprintf (buf, 32, "%d", image->xhot);
377 gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
378 g_snprintf (buf, 32, "%d", image->yhot);
379 gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
381 XcursorImagesDestroy (images);
387 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
390 GdkCursorPrivate *private;
391 Cursor new_cursor = None;
392 GdkDisplayX11 *display_x11;
394 private = (GdkCursorPrivate *) cursor;
395 xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
396 display_x11 = GDK_DISPLAY_X11 (private->display);
398 if (!display_x11->have_xfixes)
401 if (private->serial == theme_serial)
404 private->serial = theme_serial;
406 if (private->xcursor != None)
408 if (cursor->type == GDK_BLANK_CURSOR)
411 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
414 new_cursor = XcursorLibraryLoadCursor (xdisplay, private->name);
417 new_cursor = XcursorShapeLoadCursor (xdisplay, cursor->type);
419 if (new_cursor != None)
421 XFixesChangeCursor (xdisplay, new_cursor, private->xcursor);
422 private->xcursor = new_cursor;
428 update_cursor (gpointer data,
433 cursor = (GdkCursor*)(data);
438 _gdk_x11_cursor_update_theme (cursor);
442 * gdk_x11_display_set_cursor_theme:
443 * @display: a #GdkDisplay
444 * @theme: the name of the cursor theme to use, or %NULL to unset
445 * a previously set value
446 * @size: the cursor size to use, or 0 to keep the previous size
448 * Sets the cursor theme from which the images for cursor
451 * If the windowing system supports it, existing cursors created
452 * with gdk_cursor_new(), gdk_cursor_new_for_display() and
453 * gdk_cursor_new_for_name() are updated to reflect the theme
454 * change. Custom cursors constructed with
455 * gdk_cursor_new_from_pixbuf() will have to be handled
456 * by the application (GTK+ applications can learn about
457 * cursor theme changes by listening for change notification
458 * for the corresponding #GtkSetting).
463 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
467 GdkDisplayX11 *display_x11;
472 g_return_if_fail (GDK_IS_DISPLAY (display));
474 display_x11 = GDK_DISPLAY_X11 (display);
475 xdisplay = GDK_DISPLAY_XDISPLAY (display);
477 old_theme = XcursorGetTheme (xdisplay);
478 old_size = XcursorGetDefaultSize (xdisplay);
480 if (old_size == size &&
481 (old_theme == theme ||
482 (old_theme && theme && strcmp (old_theme, theme) == 0)))
487 XcursorSetTheme (xdisplay, theme);
489 XcursorSetDefaultSize (xdisplay, size);
491 g_slist_foreach (cursor_cache, update_cursor, NULL);
497 gdk_cursor_get_image (GdkCursor *cursor)
499 g_return_val_if_fail (cursor != NULL, NULL);
505 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
509 g_return_if_fail (GDK_IS_DISPLAY (display));
513 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
515 g_return_if_fail (cursor != NULL);
523 create_cursor_image (GdkPixbuf *pixbuf,
528 XcursorImage *xcimage;
529 cairo_surface_t *surface;
532 width = gdk_pixbuf_get_width (pixbuf);
533 height = gdk_pixbuf_get_height (pixbuf);
535 xcimage = XcursorImageCreate (width, height);
540 surface = cairo_image_surface_create_for_data ((guchar *) xcimage->pixels,
546 cr = cairo_create (surface);
547 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
548 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
552 cairo_surface_destroy (surface);
558 _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
563 XcursorImage *xcimage;
565 GdkCursorPrivate *private;
571 if (x == -1 && (option = gdk_pixbuf_get_option (pixbuf, "x_hot")))
575 value = g_ascii_strtoll (option, &end, 10);
578 value >= 0 && value < G_MAXINT)
581 if (y == -1 && (option = gdk_pixbuf_get_option (pixbuf, "y_hot")))
585 value = g_ascii_strtoll (option, &end, 10);
588 value >= 0 && value < G_MAXINT)
592 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
593 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
595 if (gdk_display_is_closed (display))
601 xcimage = create_cursor_image (pixbuf, x, y);
602 xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
603 XcursorImageDestroy (xcimage);
606 private = g_new (GdkCursorPrivate, 1);
607 private->display = display;
608 private->xcursor = xcursor;
609 private->name = NULL;
610 private->serial = theme_serial;
612 cursor = (GdkCursor *) private;
613 cursor->type = GDK_CURSOR_IS_PIXMAP;
614 cursor->ref_count = 1;
620 _gdk_x11_display_get_cursor_for_name (GdkDisplay *display,
625 GdkCursorPrivate *private;
628 if (gdk_display_is_closed (display))
634 private = find_in_cache (display, GDK_CURSOR_IS_PIXMAP, name);
638 /* Cache had it, add a ref for this user */
639 gdk_cursor_ref ((GdkCursor*) private);
641 return (GdkCursor*) private;
644 xdisplay = GDK_DISPLAY_XDISPLAY (display);
645 xcursor = XcursorLibraryLoadCursor (xdisplay, name);
650 private = g_new (GdkCursorPrivate, 1);
651 private->display = display;
652 private->xcursor = xcursor;
653 private->name = g_strdup (name);
654 private->serial = theme_serial;
656 cursor = (GdkCursor *) private;
657 cursor->type = GDK_CURSOR_IS_PIXMAP;
658 cursor->ref_count = 1;
659 add_to_cache (private);
665 _gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
667 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
671 _gdk_x11_display_supports_cursor_color (GdkDisplay *display)
673 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
677 _gdk_x11_display_get_default_cursor_size (GdkDisplay *display,
681 *width = *height = XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
687 gdk_cursor_new_from_pixmap (GdkDisplay *display,
688 Pixmap source_pixmap,
695 GdkCursorPrivate *private;
700 g_return_val_if_fail (fg != NULL, NULL);
701 g_return_val_if_fail (bg != NULL, NULL);
703 xfg.pixel = fg->pixel;
706 xfg.green = fg->green;
707 xbg.pixel = bg->pixel;
710 xbg.green = bg->green;
712 if (gdk_display_is_closed (display->closed))
715 xcursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
716 source_pixmap, mask_pixmap, &xfg, &xbg, x, y);
717 private = g_new (GdkCursorPrivate, 1);
718 private->display = display;
719 private->xcursor = xcursor;
720 private->name = NULL;
721 private->serial = theme_serial;
723 cursor = (GdkCursor *) private;
724 cursor->type = GDK_CURSOR_IS_PIXMAP;
725 cursor->ref_count = 1;
731 _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
737 cairo_surface_t *pixmap, *mask;
738 guint width, height, n_channels, rowstride, data_stride, i, j;
739 guint8 *data, *mask_data, *pixels;
740 GdkColor fg = { 0, 0, 0, 0 };
741 GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
743 cairo_surface_t *image;
746 width = gdk_pixbuf_get_width (pixbuf);
747 height = gdk_pixbuf_get_height (pixbuf);
749 g_return_val_if_fail (0 <= x && x < width, NULL);
750 g_return_val_if_fail (0 <= y && y < height, NULL);
752 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
753 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
754 pixels = gdk_pixbuf_get_pixels (pixbuf);
756 data_stride = 4 * ((width + 31) / 32);
757 data = g_new0 (guint8, data_stride * height);
758 mask_data = g_new0 (guint8, data_stride * height);
760 for (j = 0; j < height; j++)
762 guint8 *src = pixels + j * rowstride;
763 guint8 *d = data + data_stride * j;
764 guint8 *md = mask_data + data_stride * j;
766 for (i = 0; i < width; i++)
771 if (n_channels == 3 || src[3] >= 0x80)
783 screen = gdk_display_get_default_screen (display);
785 pixmap = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
787 cr = cairo_create (pixmap);
788 image = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_A1,
789 width, height, data_stride);
790 cairo_set_source_surface (cr, image, 0, 0);
791 cairo_surface_destroy (image);
792 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
796 mask = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
798 cr = cairo_create (mask);
799 image = cairo_image_surface_create_for_data (mask_data, CAIRO_FORMAT_A1,
800 width, height, data_stride);
801 cairo_set_source_surface (cr, image, 0, 0);
802 cairo_surface_destroy (image);
803 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
807 cursor = gdk_cursor_new_from_pixmap (display,
808 cairo_xlib_surface_get_drawable (pixmap),
809 cairo_xlib_surface_get_drawable (mask),
813 cairo_surface_destroy (pixmap);
814 cairo_surface_destroy (mask);
823 _gdk_x11_display_get_cursor_for_name (GdkDisplay *display,
830 _gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
836 _gdk_x11_display_supports_cursor_color (GdkDisplay *display)
842 _gdk_x11_display_get_default_cursor_size (GdkDisplay *display)
844 /* no idea, really */
852 * gdk_display_get_maximal_cursor_size:
853 * @display: a #GdkDisplay
854 * @width: (out): the return location for the maximal cursor width
855 * @height: (out): the return location for the maximal cursor height
857 * Gets the maximal size to use for cursors on @display.
862 _gdk_x11_display_get_maximal_cursor_size (GdkDisplay *display,
869 g_return_if_fail (GDK_IS_DISPLAY (display));
871 screen = gdk_display_get_default_screen (display);
872 window = gdk_screen_get_root_window (screen);
873 XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display),
874 GDK_WINDOW_XID (window),
875 128, 128, width, height);