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>
49 #define GDK_TYPE_X11_CURSOR (gdk_x11_cursor_get_type ())
50 #define GDK_X11_CURSOR(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_X11_CURSOR, GdkX11Cursor))
51 #define GDK_X11_CURSOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_X11_CURSOR, GdkX11CursorClass))
52 #define GDK_IS_X11_CURSOR(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_X11_CURSOR))
53 #define GDK_IS_X11_CURSOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_X11_CURSOR))
54 #define GDK_X11_CURSOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_X11_CURSOR, GdkX11CursorClass))
56 typedef struct _GdkX11Cursor GdkX11Cursor;
57 typedef struct _GdkX11CursorClass GdkX11CursorClass;
69 struct _GdkX11CursorClass
71 GdkCursorClass cursor_class;
74 static guint theme_serial = 0;
76 /* cursor_cache holds a cache of non-pixmap cursors to avoid expensive
77 * libXcursor searches, cursors are added to it but only removed when
78 * their display is closed. We make the assumption that since there are
79 * a small number of display's and a small number of cursor's that this
80 * list will stay small enough not to be a problem.
82 static GSList* cursor_cache = NULL;
84 struct cursor_cache_key
91 /* Caller should check if there is already a match first.
92 * Cursor MUST be either a typed cursor or a pixmap with
96 add_to_cache (GdkX11Cursor* cursor)
98 cursor_cache = g_slist_prepend (cursor_cache, cursor);
100 /* Take a ref so that if the caller frees it we still have it */
101 g_object_ref (cursor);
104 /* Returns 0 on a match
107 cache_compare_func (gconstpointer listelem,
108 gconstpointer target)
110 GdkX11Cursor* cursor = (GdkX11Cursor*)listelem;
111 struct cursor_cache_key* key = (struct cursor_cache_key*)target;
113 if ((cursor->cursor.type != key->type) ||
114 (cursor->display != key->display))
115 return 1; /* No match */
117 /* Elements marked as pixmap must be named cursors
118 * (since we don't store normal pixmap cursors
120 if (key->type == GDK_CURSOR_IS_PIXMAP)
121 return strcmp (key->name, cursor->name);
123 return 0; /* Match */
126 /* Returns the cursor if there is a match, NULL if not
127 * For named cursors type shall be GDK_CURSOR_IS_PIXMAP
128 * For unnamed, typed cursors, name shall be NULL
131 find_in_cache (GdkDisplay *display,
136 struct cursor_cache_key key;
138 key.display = display;
142 res = g_slist_find_custom (cursor_cache, &key, cache_compare_func);
145 return (GdkX11Cursor *) res->data;
150 /* Called by gdk_display_x11_finalize to flush any cached cursors
151 * for a dead display.
154 _gdk_x11_cursor_display_finalize (GdkDisplay *display)
157 GSList** itemp; /* Pointer to the thing to fix when we delete an item */
159 itemp = &cursor_cache;
162 GdkX11Cursor* cursor = (GdkX11Cursor*)(item->data);
163 if (cursor->display == display)
166 gdk_cursor_unref ((GdkCursor*) cursor);
167 /* Remove this item from the list */
168 *(itemp) = item->next;
170 item = g_slist_next (item);
171 g_slist_free_1 (olditem);
175 itemp = &(item->next);
176 item = g_slist_next (item);
181 /*** GdkX11Cursor ***/
183 G_DEFINE_TYPE (GdkX11Cursor, gdk_x11_cursor, GDK_TYPE_CURSOR)
186 gdk_x11_cursor_finalize (GObject *object)
188 GdkX11Cursor *private = GDK_X11_CURSOR (object);
190 if (private->xcursor && !gdk_display_is_closed (private->display))
191 XFreeCursor (GDK_DISPLAY_XDISPLAY (private->display), private->xcursor);
193 g_free (private->name);
195 G_OBJECT_CLASS (gdk_x11_cursor_parent_class)->finalize (object);
199 gdk_x11_cursor_class_init (GdkX11CursorClass *cursor_class)
201 GObjectClass *object_class = G_OBJECT_CLASS (cursor_class);
203 object_class->finalize = gdk_x11_cursor_finalize;
207 gdk_x11_cursor_init (GdkX11Cursor *cursor)
212 get_blank_cursor (GdkDisplay *display)
218 cairo_surface_t *surface;
221 screen = gdk_display_get_default_screen (display);
222 surface = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen), 1, 1);
224 cr = cairo_create (surface);
225 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
229 pixmap = cairo_xlib_surface_get_drawable (surface);
232 color.red = color.blue = color.green = 0;
234 if (gdk_display_is_closed (display))
237 cursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
239 &color, &color, 1, 1);
240 cairo_surface_destroy (surface);
246 _gdk_x11_display_get_cursor_for_type (GdkDisplay *display,
247 GdkCursorType cursor_type)
249 GdkX11Cursor *private;
252 if (gdk_display_is_closed (display))
258 private = find_in_cache (display, cursor_type, NULL);
262 /* Cache had it, add a ref for this user */
263 g_object_ref (private);
265 return (GdkCursor*) private;
269 if (cursor_type != GDK_BLANK_CURSOR)
270 xcursor = XCreateFontCursor (GDK_DISPLAY_XDISPLAY (display),
273 xcursor = get_blank_cursor (display);
277 private = g_object_new (GDK_TYPE_X11_CURSOR,
278 "cursor-type", GDK_CURSOR_IS_PIXMAP,
281 private->display = display;
282 private->xcursor = xcursor;
283 private->name = NULL;
284 private->serial = theme_serial;
287 add_to_cache (private);
289 return GDK_CURSOR (private);
293 * gdk_x11_cursor_get_xdisplay:
294 * @cursor: a #GdkCursor.
296 * Returns the display of a #GdkCursor.
298 * Return value: an Xlib <type>Display*</type>.
301 gdk_x11_cursor_get_xdisplay (GdkCursor *cursor)
303 g_return_val_if_fail (cursor != NULL, NULL);
305 return GDK_DISPLAY_XDISPLAY(((GdkX11Cursor *)cursor)->display);
309 * gdk_x11_cursor_get_xcursor:
310 * @cursor: a #GdkCursor.
312 * Returns the X cursor belonging to a #GdkCursor.
314 * Return value: an Xlib <type>Cursor</type>.
317 gdk_x11_cursor_get_xcursor (GdkCursor *cursor)
319 g_return_val_if_fail (cursor != NULL, None);
321 return ((GdkX11Cursor *)cursor)->xcursor;
325 * gdk_cursor_get_display:
326 * @cursor: a #GdkCursor.
328 * Returns the display on which the #GdkCursor is defined.
330 * Returns: (transfer none): the #GdkDisplay associated to @cursor
336 gdk_cursor_get_display (GdkCursor *cursor)
338 g_return_val_if_fail (cursor != NULL, NULL);
340 return ((GdkX11Cursor *)cursor)->display;
343 #if defined(HAVE_XCURSOR) && defined(HAVE_XFIXES) && XFIXES_MAJOR >= 2
346 * gdk_cursor_get_image:
347 * @cursor: a #GdkCursor
349 * Returns a #GdkPixbuf with the image used to display the cursor.
351 * Note that depending on the capabilities of the windowing system and
352 * on the cursor, GDK may not be able to obtain the image data. In this
353 * case, %NULL is returned.
355 * Returns: (transfer full): a #GdkPixbuf representing @cursor, or %NULL
360 gdk_cursor_get_image (GdkCursor *cursor)
363 GdkX11Cursor *private;
364 XcursorImages *images = NULL;
368 guchar *data, *p, tmp;
372 g_return_val_if_fail (cursor != NULL, NULL);
374 private = (GdkX11Cursor *) cursor;
376 xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
378 size = XcursorGetDefaultSize (xdisplay);
379 theme = XcursorGetTheme (xdisplay);
381 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
384 images = XcursorLibraryLoadImages (private->name, theme, size);
387 images = XcursorShapeLoadImages (cursor->type, theme, size);
392 image = images->images[0];
394 data = g_malloc (4 * image->width * image->height);
395 memcpy (data, image->pixels, 4 * image->width * image->height);
397 for (p = data; p < data + (4 * image->width * image->height); p += 4)
404 pixbuf = gdk_pixbuf_new_from_data (data, GDK_COLORSPACE_RGB, TRUE,
405 8, image->width, image->height,
407 (GdkPixbufDestroyNotify)g_free, NULL);
410 gdk_pixbuf_set_option (pixbuf, "name", private->name);
411 g_snprintf (buf, 32, "%d", image->xhot);
412 gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
413 g_snprintf (buf, 32, "%d", image->yhot);
414 gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
416 XcursorImagesDestroy (images);
422 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
425 GdkX11Cursor *private;
426 Cursor new_cursor = None;
427 GdkDisplayX11 *display_x11;
429 private = (GdkX11Cursor *) cursor;
430 xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
431 display_x11 = GDK_DISPLAY_X11 (private->display);
433 if (!display_x11->have_xfixes)
436 if (private->serial == theme_serial)
439 private->serial = theme_serial;
441 if (private->xcursor != None)
443 if (cursor->type == GDK_BLANK_CURSOR)
446 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
449 new_cursor = XcursorLibraryLoadCursor (xdisplay, private->name);
452 new_cursor = XcursorShapeLoadCursor (xdisplay, cursor->type);
454 if (new_cursor != None)
456 XFixesChangeCursor (xdisplay, new_cursor, private->xcursor);
457 private->xcursor = new_cursor;
463 update_cursor (gpointer data,
468 cursor = (GdkCursor*)(data);
473 _gdk_x11_cursor_update_theme (cursor);
477 * gdk_x11_display_set_cursor_theme:
478 * @display: a #GdkDisplay
479 * @theme: the name of the cursor theme to use, or %NULL to unset
480 * a previously set value
481 * @size: the cursor size to use, or 0 to keep the previous size
483 * Sets the cursor theme from which the images for cursor
486 * If the windowing system supports it, existing cursors created
487 * with gdk_cursor_new(), gdk_cursor_new_for_display() and
488 * gdk_cursor_new_for_name() are updated to reflect the theme
489 * change. Custom cursors constructed with
490 * gdk_cursor_new_from_pixbuf() will have to be handled
491 * by the application (GTK+ applications can learn about
492 * cursor theme changes by listening for change notification
493 * for the corresponding #GtkSetting).
498 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
502 GdkDisplayX11 *display_x11;
507 g_return_if_fail (GDK_IS_DISPLAY (display));
509 display_x11 = GDK_DISPLAY_X11 (display);
510 xdisplay = GDK_DISPLAY_XDISPLAY (display);
512 old_theme = XcursorGetTheme (xdisplay);
513 old_size = XcursorGetDefaultSize (xdisplay);
515 if (old_size == size &&
516 (old_theme == theme ||
517 (old_theme && theme && strcmp (old_theme, theme) == 0)))
522 XcursorSetTheme (xdisplay, theme);
524 XcursorSetDefaultSize (xdisplay, size);
526 g_slist_foreach (cursor_cache, update_cursor, NULL);
532 gdk_cursor_get_image (GdkCursor *cursor)
534 g_return_val_if_fail (cursor != NULL, NULL);
540 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
544 g_return_if_fail (GDK_IS_DISPLAY (display));
548 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
550 g_return_if_fail (cursor != NULL);
558 create_cursor_image (GdkPixbuf *pixbuf,
563 XcursorImage *xcimage;
564 cairo_surface_t *surface;
567 width = gdk_pixbuf_get_width (pixbuf);
568 height = gdk_pixbuf_get_height (pixbuf);
570 xcimage = XcursorImageCreate (width, height);
575 surface = cairo_image_surface_create_for_data ((guchar *) xcimage->pixels,
581 cr = cairo_create (surface);
582 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
583 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
587 cairo_surface_destroy (surface);
593 _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
598 XcursorImage *xcimage;
600 GdkX11Cursor *private;
605 if (x == -1 && (option = gdk_pixbuf_get_option (pixbuf, "x_hot")))
609 value = g_ascii_strtoll (option, &end, 10);
612 value >= 0 && value < G_MAXINT)
615 if (y == -1 && (option = gdk_pixbuf_get_option (pixbuf, "y_hot")))
619 value = g_ascii_strtoll (option, &end, 10);
622 value >= 0 && value < G_MAXINT)
626 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
627 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
629 if (gdk_display_is_closed (display))
635 xcimage = create_cursor_image (pixbuf, x, y);
636 xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
637 XcursorImageDestroy (xcimage);
640 private = g_object_new (GDK_TYPE_X11_CURSOR,
641 "cursor-type", GDK_CURSOR_IS_PIXMAP,
644 private->display = display;
645 private->xcursor = xcursor;
646 private->name = NULL;
647 private->serial = theme_serial;
649 return GDK_CURSOR (private);
653 _gdk_x11_display_get_cursor_for_name (GdkDisplay *display,
658 GdkX11Cursor *private;
660 if (gdk_display_is_closed (display))
666 private = find_in_cache (display, GDK_CURSOR_IS_PIXMAP, name);
670 /* Cache had it, add a ref for this user */
671 g_object_ref (private);
673 return (GdkCursor*) private;
676 xdisplay = GDK_DISPLAY_XDISPLAY (display);
677 xcursor = XcursorLibraryLoadCursor (xdisplay, name);
682 private = g_object_new (GDK_TYPE_X11_CURSOR,
683 "cursor-type", GDK_CURSOR_IS_PIXMAP,
686 private->display = display;
687 private->xcursor = xcursor;
688 private->name = g_strdup (name);
689 private->serial = theme_serial;
691 add_to_cache (private);
693 return GDK_CURSOR (private);
697 _gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
699 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
703 _gdk_x11_display_supports_cursor_color (GdkDisplay *display)
705 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
709 _gdk_x11_display_get_default_cursor_size (GdkDisplay *display,
713 *width = *height = XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
719 gdk_cursor_new_from_pixmap (GdkDisplay *display,
720 Pixmap source_pixmap,
727 GdkX11Cursor *private;
731 g_return_val_if_fail (fg != NULL, NULL);
732 g_return_val_if_fail (bg != NULL, NULL);
734 xfg.pixel = fg->pixel;
737 xfg.green = fg->green;
738 xbg.pixel = bg->pixel;
741 xbg.green = bg->green;
743 if (gdk_display_is_closed (display->closed))
746 xcursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
747 source_pixmap, mask_pixmap, &xfg, &xbg, x, y);
748 private = g_object_new (GDK_TYPE_X11_CURSOR,
749 "cursor-type", GDK_CURSOR_IS_PIXMAP,
752 private->display = display;
753 private->xcursor = xcursor;
754 private->name = NULL;
755 private->serial = theme_serial;
757 return GDK_CURSOR (private);
761 _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
767 cairo_surface_t *pixmap, *mask;
768 guint width, height, n_channels, rowstride, data_stride, i, j;
769 guint8 *data, *mask_data, *pixels;
770 GdkColor fg = { 0, 0, 0, 0 };
771 GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
773 cairo_surface_t *image;
776 width = gdk_pixbuf_get_width (pixbuf);
777 height = gdk_pixbuf_get_height (pixbuf);
779 g_return_val_if_fail (0 <= x && x < width, NULL);
780 g_return_val_if_fail (0 <= y && y < height, NULL);
782 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
783 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
784 pixels = gdk_pixbuf_get_pixels (pixbuf);
786 data_stride = 4 * ((width + 31) / 32);
787 data = g_new0 (guint8, data_stride * height);
788 mask_data = g_new0 (guint8, data_stride * height);
790 for (j = 0; j < height; j++)
792 guint8 *src = pixels + j * rowstride;
793 guint8 *d = data + data_stride * j;
794 guint8 *md = mask_data + data_stride * j;
796 for (i = 0; i < width; i++)
801 if (n_channels == 3 || src[3] >= 0x80)
813 screen = gdk_display_get_default_screen (display);
815 pixmap = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
817 cr = cairo_create (pixmap);
818 image = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_A1,
819 width, height, data_stride);
820 cairo_set_source_surface (cr, image, 0, 0);
821 cairo_surface_destroy (image);
822 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
826 mask = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
828 cr = cairo_create (mask);
829 image = cairo_image_surface_create_for_data (mask_data, CAIRO_FORMAT_A1,
830 width, height, data_stride);
831 cairo_set_source_surface (cr, image, 0, 0);
832 cairo_surface_destroy (image);
833 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
837 cursor = gdk_cursor_new_from_pixmap (display,
838 cairo_xlib_surface_get_drawable (pixmap),
839 cairo_xlib_surface_get_drawable (mask),
843 cairo_surface_destroy (pixmap);
844 cairo_surface_destroy (mask);
853 _gdk_x11_display_get_cursor_for_name (GdkDisplay *display,
860 _gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
866 _gdk_x11_display_supports_cursor_color (GdkDisplay *display)
872 _gdk_x11_display_get_default_cursor_size (GdkDisplay *display)
874 /* no idea, really */
882 * gdk_display_get_maximal_cursor_size:
883 * @display: a #GdkDisplay
884 * @width: (out): the return location for the maximal cursor width
885 * @height: (out): the return location for the maximal cursor height
887 * Gets the maximal size to use for cursors on @display.
892 _gdk_x11_display_get_maximal_cursor_size (GdkDisplay *display,
899 g_return_if_fail (GDK_IS_DISPLAY (display));
901 screen = gdk_display_get_default_screen (display);
902 window = gdk_screen_get_root_window (screen);
903 XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display),
904 GDK_WINDOW_XID (window),
905 128, 128, width, height);