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;
68 struct _GdkX11CursorClass
70 GdkCursorClass cursor_class;
73 static guint theme_serial = 0;
75 /* cursor_cache holds a cache of non-pixmap cursors to avoid expensive
76 * libXcursor searches, cursors are added to it but only removed when
77 * their display is closed. We make the assumption that since there are
78 * a small number of display's and a small number of cursor's that this
79 * list will stay small enough not to be a problem.
81 static GSList* cursor_cache = NULL;
83 struct cursor_cache_key
90 /* Caller should check if there is already a match first.
91 * Cursor MUST be either a typed cursor or a pixmap with
95 add_to_cache (GdkX11Cursor* cursor)
97 cursor_cache = g_slist_prepend (cursor_cache, cursor);
99 /* Take a ref so that if the caller frees it we still have it */
100 g_object_ref (cursor);
103 /* Returns 0 on a match
106 cache_compare_func (gconstpointer listelem,
107 gconstpointer target)
109 GdkX11Cursor* cursor = (GdkX11Cursor*)listelem;
110 struct cursor_cache_key* key = (struct cursor_cache_key*)target;
112 if ((cursor->cursor.type != key->type) ||
113 (gdk_cursor_get_display (GDK_CURSOR (cursor)) != key->display))
114 return 1; /* No match */
116 /* Elements marked as pixmap must be named cursors
117 * (since we don't store normal pixmap cursors
119 if (key->type == GDK_CURSOR_IS_PIXMAP)
120 return strcmp (key->name, cursor->name);
122 return 0; /* Match */
125 /* Returns the cursor if there is a match, NULL if not
126 * For named cursors type shall be GDK_CURSOR_IS_PIXMAP
127 * For unnamed, typed cursors, name shall be NULL
130 find_in_cache (GdkDisplay *display,
135 struct cursor_cache_key key;
137 key.display = display;
141 res = g_slist_find_custom (cursor_cache, &key, cache_compare_func);
144 return (GdkX11Cursor *) res->data;
149 /* Called by gdk_x11_display_finalize to flush any cached cursors
150 * for a dead display.
153 _gdk_x11_cursor_display_finalize (GdkDisplay *display)
156 GSList** itemp; /* Pointer to the thing to fix when we delete an item */
158 itemp = &cursor_cache;
161 GdkX11Cursor* cursor = (GdkX11Cursor*)(item->data);
162 if (gdk_cursor_get_display (GDK_CURSOR (cursor)) == display)
165 gdk_cursor_unref ((GdkCursor*) cursor);
166 /* Remove this item from the list */
167 *(itemp) = item->next;
169 item = g_slist_next (item);
170 g_slist_free_1 (olditem);
174 itemp = &(item->next);
175 item = g_slist_next (item);
180 /*** GdkX11Cursor ***/
182 G_DEFINE_TYPE (GdkX11Cursor, gdk_x11_cursor, GDK_TYPE_CURSOR)
184 static GdkPixbuf* gdk_x11_cursor_get_image (GdkCursor *cursor);
187 gdk_x11_cursor_finalize (GObject *object)
189 GdkX11Cursor *private = GDK_X11_CURSOR (object);
192 display = gdk_cursor_get_display (GDK_CURSOR (object));
193 if (private->xcursor && !gdk_display_is_closed (display))
194 XFreeCursor (GDK_DISPLAY_XDISPLAY (display), private->xcursor);
196 g_free (private->name);
198 G_OBJECT_CLASS (gdk_x11_cursor_parent_class)->finalize (object);
202 gdk_x11_cursor_class_init (GdkX11CursorClass *xcursor_class)
204 GdkCursorClass *cursor_class = GDK_CURSOR_CLASS (xcursor_class);
205 GObjectClass *object_class = G_OBJECT_CLASS (xcursor_class);
207 object_class->finalize = gdk_x11_cursor_finalize;
209 cursor_class->get_image = gdk_x11_cursor_get_image;
213 gdk_x11_cursor_init (GdkX11Cursor *cursor)
218 get_blank_cursor (GdkDisplay *display)
224 cairo_surface_t *surface;
227 screen = gdk_display_get_default_screen (display);
228 surface = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen), 1, 1);
230 cr = cairo_create (surface);
231 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
235 pixmap = cairo_xlib_surface_get_drawable (surface);
238 color.red = color.blue = color.green = 0;
240 if (gdk_display_is_closed (display))
243 cursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
245 &color, &color, 1, 1);
246 cairo_surface_destroy (surface);
252 _gdk_x11_display_get_cursor_for_type (GdkDisplay *display,
253 GdkCursorType cursor_type)
255 GdkX11Cursor *private;
258 if (gdk_display_is_closed (display))
264 private = find_in_cache (display, cursor_type, NULL);
268 /* Cache had it, add a ref for this user */
269 g_object_ref (private);
271 return (GdkCursor*) private;
275 if (cursor_type != GDK_BLANK_CURSOR)
276 xcursor = XCreateFontCursor (GDK_DISPLAY_XDISPLAY (display),
279 xcursor = get_blank_cursor (display);
283 private = g_object_new (GDK_TYPE_X11_CURSOR,
284 "cursor-type", GDK_CURSOR_IS_PIXMAP,
287 private->xcursor = xcursor;
288 private->name = NULL;
289 private->serial = theme_serial;
292 add_to_cache (private);
294 return GDK_CURSOR (private);
298 * gdk_x11_cursor_get_xdisplay:
299 * @cursor: a #GdkCursor.
301 * Returns the display of a #GdkCursor.
303 * Return value: an Xlib <type>Display*</type>.
306 gdk_x11_cursor_get_xdisplay (GdkCursor *cursor)
308 g_return_val_if_fail (cursor != NULL, NULL);
310 return GDK_DISPLAY_XDISPLAY (gdk_cursor_get_display (cursor));
314 * gdk_x11_cursor_get_xcursor:
315 * @cursor: a #GdkCursor.
317 * Returns the X cursor belonging to a #GdkCursor.
319 * Return value: an Xlib <type>Cursor</type>.
322 gdk_x11_cursor_get_xcursor (GdkCursor *cursor)
324 g_return_val_if_fail (cursor != NULL, None);
326 return ((GdkX11Cursor *)cursor)->xcursor;
329 #if defined(HAVE_XCURSOR) && defined(HAVE_XFIXES) && XFIXES_MAJOR >= 2
332 gdk_x11_cursor_get_image (GdkCursor *cursor)
335 GdkX11Cursor *private;
336 XcursorImages *images = NULL;
340 guchar *data, *p, tmp;
344 private = GDK_X11_CURSOR (cursor);
346 xdisplay = GDK_DISPLAY_XDISPLAY (gdk_cursor_get_display (cursor));
348 size = XcursorGetDefaultSize (xdisplay);
349 theme = XcursorGetTheme (xdisplay);
351 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
354 images = XcursorLibraryLoadImages (private->name, theme, size);
357 images = XcursorShapeLoadImages (cursor->type, theme, size);
362 image = images->images[0];
364 data = g_malloc (4 * image->width * image->height);
365 memcpy (data, image->pixels, 4 * image->width * image->height);
367 for (p = data; p < data + (4 * image->width * image->height); p += 4)
374 pixbuf = gdk_pixbuf_new_from_data (data, GDK_COLORSPACE_RGB, TRUE,
375 8, image->width, image->height,
377 (GdkPixbufDestroyNotify)g_free, NULL);
380 gdk_pixbuf_set_option (pixbuf, "name", private->name);
381 g_snprintf (buf, 32, "%d", image->xhot);
382 gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
383 g_snprintf (buf, 32, "%d", image->yhot);
384 gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
386 XcursorImagesDestroy (images);
392 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
395 GdkX11Cursor *private;
396 Cursor new_cursor = None;
397 GdkX11Display *display_x11;
399 private = (GdkX11Cursor *) cursor;
400 display_x11 = GDK_X11_DISPLAY (gdk_cursor_get_display (cursor));
401 xdisplay = GDK_DISPLAY_XDISPLAY (display_x11);
403 if (!display_x11->have_xfixes)
406 if (private->serial == theme_serial)
409 private->serial = theme_serial;
411 if (private->xcursor != None)
413 if (cursor->type == GDK_BLANK_CURSOR)
416 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
419 new_cursor = XcursorLibraryLoadCursor (xdisplay, private->name);
422 new_cursor = XcursorShapeLoadCursor (xdisplay, cursor->type);
424 if (new_cursor != None)
426 XFixesChangeCursor (xdisplay, new_cursor, private->xcursor);
427 private->xcursor = new_cursor;
433 update_cursor (gpointer data,
438 cursor = (GdkCursor*)(data);
443 _gdk_x11_cursor_update_theme (cursor);
447 * gdk_x11_display_set_cursor_theme:
448 * @display: a #GdkDisplay
449 * @theme: the name of the cursor theme to use, or %NULL to unset
450 * a previously set value
451 * @size: the cursor size to use, or 0 to keep the previous size
453 * Sets the cursor theme from which the images for cursor
456 * If the windowing system supports it, existing cursors created
457 * with gdk_cursor_new(), gdk_cursor_new_for_display() and
458 * gdk_cursor_new_for_name() are updated to reflect the theme
459 * change. Custom cursors constructed with
460 * gdk_cursor_new_from_pixbuf() will have to be handled
461 * by the application (GTK+ applications can learn about
462 * cursor theme changes by listening for change notification
463 * for the corresponding #GtkSetting).
468 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
472 GdkX11Display *display_x11;
477 g_return_if_fail (GDK_IS_DISPLAY (display));
479 display_x11 = GDK_X11_DISPLAY (display);
480 xdisplay = GDK_DISPLAY_XDISPLAY (display);
482 old_theme = XcursorGetTheme (xdisplay);
483 old_size = XcursorGetDefaultSize (xdisplay);
485 if (old_size == size &&
486 (old_theme == theme ||
487 (old_theme && theme && strcmp (old_theme, theme) == 0)))
492 XcursorSetTheme (xdisplay, theme);
494 XcursorSetDefaultSize (xdisplay, size);
496 g_slist_foreach (cursor_cache, update_cursor, NULL);
502 gdk_x11_cursor_get_image (GdkCursor *cursor)
508 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
512 g_return_if_fail (GDK_IS_DISPLAY (display));
516 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
518 g_return_if_fail (cursor != NULL);
526 create_cursor_image (GdkPixbuf *pixbuf,
531 XcursorImage *xcimage;
532 cairo_surface_t *surface;
535 width = gdk_pixbuf_get_width (pixbuf);
536 height = gdk_pixbuf_get_height (pixbuf);
538 xcimage = XcursorImageCreate (width, height);
543 surface = cairo_image_surface_create_for_data ((guchar *) xcimage->pixels,
549 cr = cairo_create (surface);
550 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
551 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
555 cairo_surface_destroy (surface);
561 _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
566 XcursorImage *xcimage;
568 GdkX11Cursor *private;
573 if (x == -1 && (option = gdk_pixbuf_get_option (pixbuf, "x_hot")))
577 value = g_ascii_strtoll (option, &end, 10);
580 value >= 0 && value < G_MAXINT)
583 if (y == -1 && (option = gdk_pixbuf_get_option (pixbuf, "y_hot")))
587 value = g_ascii_strtoll (option, &end, 10);
590 value >= 0 && value < G_MAXINT)
594 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
595 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
597 if (gdk_display_is_closed (display))
603 xcimage = create_cursor_image (pixbuf, x, y);
604 xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
605 XcursorImageDestroy (xcimage);
608 private = g_object_new (GDK_TYPE_X11_CURSOR,
609 "cursor-type", GDK_CURSOR_IS_PIXMAP,
612 private->xcursor = xcursor;
613 private->name = NULL;
614 private->serial = theme_serial;
616 return GDK_CURSOR (private);
620 _gdk_x11_display_get_cursor_for_name (GdkDisplay *display,
625 GdkX11Cursor *private;
627 if (gdk_display_is_closed (display))
633 private = find_in_cache (display, GDK_CURSOR_IS_PIXMAP, name);
637 /* Cache had it, add a ref for this user */
638 g_object_ref (private);
640 return (GdkCursor*) private;
643 xdisplay = GDK_DISPLAY_XDISPLAY (display);
644 xcursor = XcursorLibraryLoadCursor (xdisplay, name);
649 private = g_object_new (GDK_TYPE_X11_CURSOR,
650 "cursor-type", GDK_CURSOR_IS_PIXMAP,
653 private->xcursor = xcursor;
654 private->name = g_strdup (name);
655 private->serial = theme_serial;
657 add_to_cache (private);
659 return GDK_CURSOR (private);
663 _gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
665 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
669 _gdk_x11_display_supports_cursor_color (GdkDisplay *display)
671 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
675 _gdk_x11_display_get_default_cursor_size (GdkDisplay *display,
679 *width = *height = XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
685 gdk_cursor_new_from_pixmap (GdkDisplay *display,
686 Pixmap source_pixmap,
693 GdkX11Cursor *private;
697 g_return_val_if_fail (fg != NULL, NULL);
698 g_return_val_if_fail (bg != NULL, NULL);
700 xfg.pixel = fg->pixel;
703 xfg.green = fg->green;
704 xbg.pixel = bg->pixel;
707 xbg.green = bg->green;
709 if (gdk_display_is_closed (display->closed))
712 xcursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
713 source_pixmap, mask_pixmap, &xfg, &xbg, x, y);
714 private = g_object_new (GDK_TYPE_X11_CURSOR,
715 "cursor-type", GDK_CURSOR_IS_PIXMAP,
718 private->xcursor = xcursor;
719 private->name = NULL;
720 private->serial = theme_serial;
722 return GDK_CURSOR (private);
726 _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
732 cairo_surface_t *pixmap, *mask;
733 guint width, height, n_channels, rowstride, data_stride, i, j;
734 guint8 *data, *mask_data, *pixels;
735 GdkColor fg = { 0, 0, 0, 0 };
736 GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
738 cairo_surface_t *image;
741 width = gdk_pixbuf_get_width (pixbuf);
742 height = gdk_pixbuf_get_height (pixbuf);
744 g_return_val_if_fail (0 <= x && x < width, NULL);
745 g_return_val_if_fail (0 <= y && y < height, NULL);
747 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
748 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
749 pixels = gdk_pixbuf_get_pixels (pixbuf);
751 data_stride = 4 * ((width + 31) / 32);
752 data = g_new0 (guint8, data_stride * height);
753 mask_data = g_new0 (guint8, data_stride * height);
755 for (j = 0; j < height; j++)
757 guint8 *src = pixels + j * rowstride;
758 guint8 *d = data + data_stride * j;
759 guint8 *md = mask_data + data_stride * j;
761 for (i = 0; i < width; i++)
766 if (n_channels == 3 || src[3] >= 0x80)
778 screen = gdk_display_get_default_screen (display);
780 pixmap = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
782 cr = cairo_create (pixmap);
783 image = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_A1,
784 width, height, data_stride);
785 cairo_set_source_surface (cr, image, 0, 0);
786 cairo_surface_destroy (image);
787 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
791 mask = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
793 cr = cairo_create (mask);
794 image = cairo_image_surface_create_for_data (mask_data, CAIRO_FORMAT_A1,
795 width, height, data_stride);
796 cairo_set_source_surface (cr, image, 0, 0);
797 cairo_surface_destroy (image);
798 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
802 cursor = gdk_cursor_new_from_pixmap (display,
803 cairo_xlib_surface_get_drawable (pixmap),
804 cairo_xlib_surface_get_drawable (mask),
808 cairo_surface_destroy (pixmap);
809 cairo_surface_destroy (mask);
818 _gdk_x11_display_get_cursor_for_name (GdkDisplay *display,
825 _gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
831 _gdk_x11_display_supports_cursor_color (GdkDisplay *display)
837 _gdk_x11_display_get_default_cursor_size (GdkDisplay *display)
839 /* no idea, really */
846 _gdk_x11_display_get_maximal_cursor_size (GdkDisplay *display,
853 g_return_if_fail (GDK_IS_DISPLAY (display));
855 screen = gdk_display_get_default_screen (display);
856 window = gdk_screen_get_root_window (screen);
857 XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display),
858 GDK_WINDOW_XID (window),
859 128, 128, width, height);