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 gdk_cursor_ref ((GdkCursor*) 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;
253 if (gdk_display_is_closed (display))
259 private = find_in_cache (display, cursor_type, NULL);
263 /* Cache had it, add a ref for this user */
264 gdk_cursor_ref ((GdkCursor*) private);
266 return (GdkCursor*) private;
270 if (cursor_type != GDK_BLANK_CURSOR)
271 xcursor = XCreateFontCursor (GDK_DISPLAY_XDISPLAY (display),
274 xcursor = get_blank_cursor (display);
278 private = g_object_new (GDK_TYPE_X11_CURSOR, NULL);
279 private->display = display;
280 private->xcursor = xcursor;
281 private->name = NULL;
282 private->serial = theme_serial;
284 cursor = (GdkCursor *) private;
285 cursor->type = cursor_type;
288 add_to_cache (private);
294 * gdk_x11_cursor_get_xdisplay:
295 * @cursor: a #GdkCursor.
297 * Returns the display of a #GdkCursor.
299 * Return value: an Xlib <type>Display*</type>.
302 gdk_x11_cursor_get_xdisplay (GdkCursor *cursor)
304 g_return_val_if_fail (cursor != NULL, NULL);
306 return GDK_DISPLAY_XDISPLAY(((GdkX11Cursor *)cursor)->display);
310 * gdk_x11_cursor_get_xcursor:
311 * @cursor: a #GdkCursor.
313 * Returns the X cursor belonging to a #GdkCursor.
315 * Return value: an Xlib <type>Cursor</type>.
318 gdk_x11_cursor_get_xcursor (GdkCursor *cursor)
320 g_return_val_if_fail (cursor != NULL, None);
322 return ((GdkX11Cursor *)cursor)->xcursor;
326 * gdk_cursor_get_display:
327 * @cursor: a #GdkCursor.
329 * Returns the display on which the #GdkCursor is defined.
331 * Returns: (transfer none): the #GdkDisplay associated to @cursor
337 gdk_cursor_get_display (GdkCursor *cursor)
339 g_return_val_if_fail (cursor != NULL, NULL);
341 return ((GdkX11Cursor *)cursor)->display;
344 #if defined(HAVE_XCURSOR) && defined(HAVE_XFIXES) && XFIXES_MAJOR >= 2
347 * gdk_cursor_get_image:
348 * @cursor: a #GdkCursor
350 * Returns a #GdkPixbuf with the image used to display the cursor.
352 * Note that depending on the capabilities of the windowing system and
353 * on the cursor, GDK may not be able to obtain the image data. In this
354 * case, %NULL is returned.
356 * Returns: (transfer full): a #GdkPixbuf representing @cursor, or %NULL
361 gdk_cursor_get_image (GdkCursor *cursor)
364 GdkX11Cursor *private;
365 XcursorImages *images = NULL;
369 guchar *data, *p, tmp;
373 g_return_val_if_fail (cursor != NULL, NULL);
375 private = (GdkX11Cursor *) cursor;
377 xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
379 size = XcursorGetDefaultSize (xdisplay);
380 theme = XcursorGetTheme (xdisplay);
382 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
385 images = XcursorLibraryLoadImages (private->name, theme, size);
388 images = XcursorShapeLoadImages (cursor->type, theme, size);
393 image = images->images[0];
395 data = g_malloc (4 * image->width * image->height);
396 memcpy (data, image->pixels, 4 * image->width * image->height);
398 for (p = data; p < data + (4 * image->width * image->height); p += 4)
405 pixbuf = gdk_pixbuf_new_from_data (data, GDK_COLORSPACE_RGB, TRUE,
406 8, image->width, image->height,
408 (GdkPixbufDestroyNotify)g_free, NULL);
411 gdk_pixbuf_set_option (pixbuf, "name", private->name);
412 g_snprintf (buf, 32, "%d", image->xhot);
413 gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
414 g_snprintf (buf, 32, "%d", image->yhot);
415 gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
417 XcursorImagesDestroy (images);
423 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
426 GdkX11Cursor *private;
427 Cursor new_cursor = None;
428 GdkDisplayX11 *display_x11;
430 private = (GdkX11Cursor *) cursor;
431 xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
432 display_x11 = GDK_DISPLAY_X11 (private->display);
434 if (!display_x11->have_xfixes)
437 if (private->serial == theme_serial)
440 private->serial = theme_serial;
442 if (private->xcursor != None)
444 if (cursor->type == GDK_BLANK_CURSOR)
447 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
450 new_cursor = XcursorLibraryLoadCursor (xdisplay, private->name);
453 new_cursor = XcursorShapeLoadCursor (xdisplay, cursor->type);
455 if (new_cursor != None)
457 XFixesChangeCursor (xdisplay, new_cursor, private->xcursor);
458 private->xcursor = new_cursor;
464 update_cursor (gpointer data,
469 cursor = (GdkCursor*)(data);
474 _gdk_x11_cursor_update_theme (cursor);
478 * gdk_x11_display_set_cursor_theme:
479 * @display: a #GdkDisplay
480 * @theme: the name of the cursor theme to use, or %NULL to unset
481 * a previously set value
482 * @size: the cursor size to use, or 0 to keep the previous size
484 * Sets the cursor theme from which the images for cursor
487 * If the windowing system supports it, existing cursors created
488 * with gdk_cursor_new(), gdk_cursor_new_for_display() and
489 * gdk_cursor_new_for_name() are updated to reflect the theme
490 * change. Custom cursors constructed with
491 * gdk_cursor_new_from_pixbuf() will have to be handled
492 * by the application (GTK+ applications can learn about
493 * cursor theme changes by listening for change notification
494 * for the corresponding #GtkSetting).
499 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
503 GdkDisplayX11 *display_x11;
508 g_return_if_fail (GDK_IS_DISPLAY (display));
510 display_x11 = GDK_DISPLAY_X11 (display);
511 xdisplay = GDK_DISPLAY_XDISPLAY (display);
513 old_theme = XcursorGetTheme (xdisplay);
514 old_size = XcursorGetDefaultSize (xdisplay);
516 if (old_size == size &&
517 (old_theme == theme ||
518 (old_theme && theme && strcmp (old_theme, theme) == 0)))
523 XcursorSetTheme (xdisplay, theme);
525 XcursorSetDefaultSize (xdisplay, size);
527 g_slist_foreach (cursor_cache, update_cursor, NULL);
533 gdk_cursor_get_image (GdkCursor *cursor)
535 g_return_val_if_fail (cursor != NULL, NULL);
541 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
545 g_return_if_fail (GDK_IS_DISPLAY (display));
549 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
551 g_return_if_fail (cursor != NULL);
559 create_cursor_image (GdkPixbuf *pixbuf,
564 XcursorImage *xcimage;
565 cairo_surface_t *surface;
568 width = gdk_pixbuf_get_width (pixbuf);
569 height = gdk_pixbuf_get_height (pixbuf);
571 xcimage = XcursorImageCreate (width, height);
576 surface = cairo_image_surface_create_for_data ((guchar *) xcimage->pixels,
582 cr = cairo_create (surface);
583 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
584 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
588 cairo_surface_destroy (surface);
594 _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
599 XcursorImage *xcimage;
601 GdkX11Cursor *private;
607 if (x == -1 && (option = gdk_pixbuf_get_option (pixbuf, "x_hot")))
611 value = g_ascii_strtoll (option, &end, 10);
614 value >= 0 && value < G_MAXINT)
617 if (y == -1 && (option = gdk_pixbuf_get_option (pixbuf, "y_hot")))
621 value = g_ascii_strtoll (option, &end, 10);
624 value >= 0 && value < G_MAXINT)
628 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
629 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
631 if (gdk_display_is_closed (display))
637 xcimage = create_cursor_image (pixbuf, x, y);
638 xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
639 XcursorImageDestroy (xcimage);
642 private = g_object_new (GDK_TYPE_X11_CURSOR, NULL);
643 private->display = display;
644 private->xcursor = xcursor;
645 private->name = NULL;
646 private->serial = theme_serial;
648 cursor = (GdkCursor *) private;
649 cursor->type = GDK_CURSOR_IS_PIXMAP;
655 _gdk_x11_display_get_cursor_for_name (GdkDisplay *display,
660 GdkX11Cursor *private;
663 if (gdk_display_is_closed (display))
669 private = find_in_cache (display, GDK_CURSOR_IS_PIXMAP, name);
673 /* Cache had it, add a ref for this user */
674 gdk_cursor_ref ((GdkCursor*) private);
676 return (GdkCursor*) private;
679 xdisplay = GDK_DISPLAY_XDISPLAY (display);
680 xcursor = XcursorLibraryLoadCursor (xdisplay, name);
685 private = g_object_new (GDK_TYPE_X11_CURSOR, NULL);
686 private->display = display;
687 private->xcursor = xcursor;
688 private->name = g_strdup (name);
689 private->serial = theme_serial;
691 cursor = (GdkCursor *) private;
692 cursor->type = GDK_CURSOR_IS_PIXMAP;
693 add_to_cache (private);
699 _gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
701 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
705 _gdk_x11_display_supports_cursor_color (GdkDisplay *display)
707 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
711 _gdk_x11_display_get_default_cursor_size (GdkDisplay *display,
715 *width = *height = XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
721 gdk_cursor_new_from_pixmap (GdkDisplay *display,
722 Pixmap source_pixmap,
729 GdkX11Cursor *private;
734 g_return_val_if_fail (fg != NULL, NULL);
735 g_return_val_if_fail (bg != NULL, NULL);
737 xfg.pixel = fg->pixel;
740 xfg.green = fg->green;
741 xbg.pixel = bg->pixel;
744 xbg.green = bg->green;
746 if (gdk_display_is_closed (display->closed))
749 xcursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
750 source_pixmap, mask_pixmap, &xfg, &xbg, x, y);
751 private = g_object_new (GDK_TYPE_X11_CURSOR, NULL);
752 private->display = display;
753 private->xcursor = xcursor;
754 private->name = NULL;
755 private->serial = theme_serial;
757 cursor = (GdkCursor *) private;
758 cursor->type = GDK_CURSOR_IS_PIXMAP;
764 _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
770 cairo_surface_t *pixmap, *mask;
771 guint width, height, n_channels, rowstride, data_stride, i, j;
772 guint8 *data, *mask_data, *pixels;
773 GdkColor fg = { 0, 0, 0, 0 };
774 GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
776 cairo_surface_t *image;
779 width = gdk_pixbuf_get_width (pixbuf);
780 height = gdk_pixbuf_get_height (pixbuf);
782 g_return_val_if_fail (0 <= x && x < width, NULL);
783 g_return_val_if_fail (0 <= y && y < height, NULL);
785 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
786 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
787 pixels = gdk_pixbuf_get_pixels (pixbuf);
789 data_stride = 4 * ((width + 31) / 32);
790 data = g_new0 (guint8, data_stride * height);
791 mask_data = g_new0 (guint8, data_stride * height);
793 for (j = 0; j < height; j++)
795 guint8 *src = pixels + j * rowstride;
796 guint8 *d = data + data_stride * j;
797 guint8 *md = mask_data + data_stride * j;
799 for (i = 0; i < width; i++)
804 if (n_channels == 3 || src[3] >= 0x80)
816 screen = gdk_display_get_default_screen (display);
818 pixmap = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
820 cr = cairo_create (pixmap);
821 image = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_A1,
822 width, height, data_stride);
823 cairo_set_source_surface (cr, image, 0, 0);
824 cairo_surface_destroy (image);
825 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
829 mask = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
831 cr = cairo_create (mask);
832 image = cairo_image_surface_create_for_data (mask_data, CAIRO_FORMAT_A1,
833 width, height, data_stride);
834 cairo_set_source_surface (cr, image, 0, 0);
835 cairo_surface_destroy (image);
836 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
840 cursor = gdk_cursor_new_from_pixmap (display,
841 cairo_xlib_surface_get_drawable (pixmap),
842 cairo_xlib_surface_get_drawable (mask),
846 cairo_surface_destroy (pixmap);
847 cairo_surface_destroy (mask);
856 _gdk_x11_display_get_cursor_for_name (GdkDisplay *display,
863 _gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
869 _gdk_x11_display_supports_cursor_color (GdkDisplay *display)
875 _gdk_x11_display_get_default_cursor_size (GdkDisplay *display)
877 /* no idea, really */
885 * gdk_display_get_maximal_cursor_size:
886 * @display: a #GdkDisplay
887 * @width: (out): the return location for the maximal cursor width
888 * @height: (out): the return location for the maximal cursor height
890 * Gets the maximal size to use for cursors on @display.
895 _gdk_x11_display_get_maximal_cursor_size (GdkDisplay *display,
902 g_return_if_fail (GDK_IS_DISPLAY (display));
904 screen = gdk_display_get_default_screen (display);
905 window = gdk_screen_get_root_window (screen);
906 XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display),
907 GDK_WINDOW_XID (window),
908 128, 128, width, height);