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", cursor_type,
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: (skip)
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: (skip)
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,
466 g_return_if_fail (GDK_IS_DISPLAY (display));
468 xdisplay = GDK_DISPLAY_XDISPLAY (display);
470 old_theme = XcursorGetTheme (xdisplay);
471 old_size = XcursorGetDefaultSize (xdisplay);
473 if (old_size == size &&
474 (old_theme == theme ||
475 (old_theme && theme && strcmp (old_theme, theme) == 0)))
480 XcursorSetTheme (xdisplay, theme);
482 XcursorSetDefaultSize (xdisplay, size);
484 g_slist_foreach (cursor_cache, update_cursor, NULL);
490 gdk_x11_cursor_get_image (GdkCursor *cursor)
496 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
500 g_return_if_fail (GDK_IS_DISPLAY (display));
504 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
506 g_return_if_fail (cursor != NULL);
514 create_cursor_image (GdkPixbuf *pixbuf,
519 XcursorImage *xcimage;
520 cairo_surface_t *surface;
523 width = gdk_pixbuf_get_width (pixbuf);
524 height = gdk_pixbuf_get_height (pixbuf);
526 xcimage = XcursorImageCreate (width, height);
531 surface = cairo_image_surface_create_for_data ((guchar *) xcimage->pixels,
537 cr = cairo_create (surface);
538 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
539 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
543 cairo_surface_destroy (surface);
549 _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
554 XcursorImage *xcimage;
556 GdkX11Cursor *private;
561 if (x == -1 && (option = gdk_pixbuf_get_option (pixbuf, "x_hot")))
565 value = g_ascii_strtoll (option, &end, 10);
568 value >= 0 && value < G_MAXINT)
571 if (y == -1 && (option = gdk_pixbuf_get_option (pixbuf, "y_hot")))
575 value = g_ascii_strtoll (option, &end, 10);
578 value >= 0 && value < G_MAXINT)
582 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
583 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
585 if (gdk_display_is_closed (display))
591 xcimage = create_cursor_image (pixbuf, x, y);
592 xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
593 XcursorImageDestroy (xcimage);
596 private = g_object_new (GDK_TYPE_X11_CURSOR,
597 "cursor-type", GDK_CURSOR_IS_PIXMAP,
600 private->xcursor = xcursor;
601 private->name = NULL;
602 private->serial = theme_serial;
604 return GDK_CURSOR (private);
608 _gdk_x11_display_get_cursor_for_name (GdkDisplay *display,
613 GdkX11Cursor *private;
615 if (gdk_display_is_closed (display))
621 private = find_in_cache (display, GDK_CURSOR_IS_PIXMAP, name);
625 /* Cache had it, add a ref for this user */
626 g_object_ref (private);
628 return (GdkCursor*) private;
631 xdisplay = GDK_DISPLAY_XDISPLAY (display);
632 xcursor = XcursorLibraryLoadCursor (xdisplay, name);
637 private = g_object_new (GDK_TYPE_X11_CURSOR,
638 "cursor-type", GDK_CURSOR_IS_PIXMAP,
641 private->xcursor = xcursor;
642 private->name = g_strdup (name);
643 private->serial = theme_serial;
645 add_to_cache (private);
647 return GDK_CURSOR (private);
651 _gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
653 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
657 _gdk_x11_display_supports_cursor_color (GdkDisplay *display)
659 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
663 _gdk_x11_display_get_default_cursor_size (GdkDisplay *display,
667 *width = *height = XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
673 gdk_cursor_new_from_pixmap (GdkDisplay *display,
674 Pixmap source_pixmap,
681 GdkX11Cursor *private;
685 g_return_val_if_fail (fg != NULL, NULL);
686 g_return_val_if_fail (bg != NULL, NULL);
688 xfg.red = fg->red * 65535;
689 xfg.blue = fg->blue * 65535;
690 xfg.green = fg->green * 65535;
692 xbg.red = bg->red * 65535;
693 xbg.blue = bg->blue * 65535;
694 xbg.green = bg->green * 65535;
696 if (gdk_display_is_closed (display))
699 xcursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
700 source_pixmap, mask_pixmap, &xfg, &xbg, x, y);
701 private = g_object_new (GDK_TYPE_X11_CURSOR,
702 "cursor-type", GDK_CURSOR_IS_PIXMAP,
705 private->xcursor = xcursor;
706 private->name = NULL;
707 private->serial = theme_serial;
709 return GDK_CURSOR (private);
713 _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
719 cairo_surface_t *pixmap, *mask;
720 guint width, height, n_channels, rowstride, data_stride, i, j;
721 guint8 *data, *mask_data, *pixels;
722 GdkRGBBA fg = { 0, 0, 0, 1 };
723 GdkRGBA bg = { 1, 1, 1, 1 };
725 cairo_surface_t *image;
728 width = gdk_pixbuf_get_width (pixbuf);
729 height = gdk_pixbuf_get_height (pixbuf);
731 g_return_val_if_fail (0 <= x && x < width, NULL);
732 g_return_val_if_fail (0 <= y && y < height, NULL);
734 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
735 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
736 pixels = gdk_pixbuf_get_pixels (pixbuf);
738 data_stride = 4 * ((width + 31) / 32);
739 data = g_new0 (guint8, data_stride * height);
740 mask_data = g_new0 (guint8, data_stride * height);
742 for (j = 0; j < height; j++)
744 guint8 *src = pixels + j * rowstride;
745 guint8 *d = data + data_stride * j;
746 guint8 *md = mask_data + data_stride * j;
748 for (i = 0; i < width; i++)
753 if (n_channels == 3 || src[3] >= 0x80)
765 screen = gdk_display_get_default_screen (display);
767 pixmap = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
769 cr = cairo_create (pixmap);
770 image = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_A1,
771 width, height, data_stride);
772 cairo_set_source_surface (cr, image, 0, 0);
773 cairo_surface_destroy (image);
774 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
778 mask = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
780 cr = cairo_create (mask);
781 image = cairo_image_surface_create_for_data (mask_data, CAIRO_FORMAT_A1,
782 width, height, data_stride);
783 cairo_set_source_surface (cr, image, 0, 0);
784 cairo_surface_destroy (image);
785 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
789 cursor = gdk_cursor_new_from_pixmap (display,
790 cairo_xlib_surface_get_drawable (pixmap),
791 cairo_xlib_surface_get_drawable (mask),
795 cairo_surface_destroy (pixmap);
796 cairo_surface_destroy (mask);
805 _gdk_x11_display_get_cursor_for_name (GdkDisplay *display,
812 _gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
818 _gdk_x11_display_supports_cursor_color (GdkDisplay *display)
824 _gdk_x11_display_get_default_cursor_size (GdkDisplay *display,
828 /* no idea, really */
829 *width = *height = 20;
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);