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_display_x11_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)
185 gdk_x11_cursor_finalize (GObject *object)
187 GdkX11Cursor *private = GDK_X11_CURSOR (object);
190 display = gdk_cursor_get_display (GDK_CURSOR (object));
191 if (private->xcursor && !gdk_display_is_closed (display))
192 XFreeCursor (GDK_DISPLAY_XDISPLAY (display), private->xcursor);
194 g_free (private->name);
196 G_OBJECT_CLASS (gdk_x11_cursor_parent_class)->finalize (object);
200 gdk_x11_cursor_class_init (GdkX11CursorClass *cursor_class)
202 GObjectClass *object_class = G_OBJECT_CLASS (cursor_class);
204 object_class->finalize = gdk_x11_cursor_finalize;
208 gdk_x11_cursor_init (GdkX11Cursor *cursor)
213 get_blank_cursor (GdkDisplay *display)
219 cairo_surface_t *surface;
222 screen = gdk_display_get_default_screen (display);
223 surface = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen), 1, 1);
225 cr = cairo_create (surface);
226 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
230 pixmap = cairo_xlib_surface_get_drawable (surface);
233 color.red = color.blue = color.green = 0;
235 if (gdk_display_is_closed (display))
238 cursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
240 &color, &color, 1, 1);
241 cairo_surface_destroy (surface);
247 _gdk_x11_display_get_cursor_for_type (GdkDisplay *display,
248 GdkCursorType cursor_type)
250 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 g_object_ref (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,
279 "cursor-type", GDK_CURSOR_IS_PIXMAP,
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 (gdk_cursor_get_display (cursor));
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;
324 #if defined(HAVE_XCURSOR) && defined(HAVE_XFIXES) && XFIXES_MAJOR >= 2
327 * gdk_cursor_get_image:
328 * @cursor: a #GdkCursor
330 * Returns a #GdkPixbuf with the image used to display the cursor.
332 * Note that depending on the capabilities of the windowing system and
333 * on the cursor, GDK may not be able to obtain the image data. In this
334 * case, %NULL is returned.
336 * Returns: (transfer full): a #GdkPixbuf representing @cursor, or %NULL
341 gdk_cursor_get_image (GdkCursor *cursor)
344 GdkX11Cursor *private;
345 XcursorImages *images = NULL;
349 guchar *data, *p, tmp;
353 g_return_val_if_fail (cursor != NULL, NULL);
355 private = (GdkX11Cursor *) cursor;
357 xdisplay = GDK_DISPLAY_XDISPLAY (gdk_cursor_get_display (cursor));
359 size = XcursorGetDefaultSize (xdisplay);
360 theme = XcursorGetTheme (xdisplay);
362 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
365 images = XcursorLibraryLoadImages (private->name, theme, size);
368 images = XcursorShapeLoadImages (cursor->type, theme, size);
373 image = images->images[0];
375 data = g_malloc (4 * image->width * image->height);
376 memcpy (data, image->pixels, 4 * image->width * image->height);
378 for (p = data; p < data + (4 * image->width * image->height); p += 4)
385 pixbuf = gdk_pixbuf_new_from_data (data, GDK_COLORSPACE_RGB, TRUE,
386 8, image->width, image->height,
388 (GdkPixbufDestroyNotify)g_free, NULL);
391 gdk_pixbuf_set_option (pixbuf, "name", private->name);
392 g_snprintf (buf, 32, "%d", image->xhot);
393 gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
394 g_snprintf (buf, 32, "%d", image->yhot);
395 gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
397 XcursorImagesDestroy (images);
403 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
406 GdkX11Cursor *private;
407 Cursor new_cursor = None;
408 GdkDisplayX11 *display_x11;
410 private = (GdkX11Cursor *) cursor;
411 display_x11 = GDK_DISPLAY_X11 (gdk_cursor_get_display (cursor));
412 xdisplay = GDK_DISPLAY_XDISPLAY (display_x11);
414 if (!display_x11->have_xfixes)
417 if (private->serial == theme_serial)
420 private->serial = theme_serial;
422 if (private->xcursor != None)
424 if (cursor->type == GDK_BLANK_CURSOR)
427 if (cursor->type == GDK_CURSOR_IS_PIXMAP)
430 new_cursor = XcursorLibraryLoadCursor (xdisplay, private->name);
433 new_cursor = XcursorShapeLoadCursor (xdisplay, cursor->type);
435 if (new_cursor != None)
437 XFixesChangeCursor (xdisplay, new_cursor, private->xcursor);
438 private->xcursor = new_cursor;
444 update_cursor (gpointer data,
449 cursor = (GdkCursor*)(data);
454 _gdk_x11_cursor_update_theme (cursor);
458 * gdk_x11_display_set_cursor_theme:
459 * @display: a #GdkDisplay
460 * @theme: the name of the cursor theme to use, or %NULL to unset
461 * a previously set value
462 * @size: the cursor size to use, or 0 to keep the previous size
464 * Sets the cursor theme from which the images for cursor
467 * If the windowing system supports it, existing cursors created
468 * with gdk_cursor_new(), gdk_cursor_new_for_display() and
469 * gdk_cursor_new_for_name() are updated to reflect the theme
470 * change. Custom cursors constructed with
471 * gdk_cursor_new_from_pixbuf() will have to be handled
472 * by the application (GTK+ applications can learn about
473 * cursor theme changes by listening for change notification
474 * for the corresponding #GtkSetting).
479 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
483 GdkDisplayX11 *display_x11;
488 g_return_if_fail (GDK_IS_DISPLAY (display));
490 display_x11 = GDK_DISPLAY_X11 (display);
491 xdisplay = GDK_DISPLAY_XDISPLAY (display);
493 old_theme = XcursorGetTheme (xdisplay);
494 old_size = XcursorGetDefaultSize (xdisplay);
496 if (old_size == size &&
497 (old_theme == theme ||
498 (old_theme && theme && strcmp (old_theme, theme) == 0)))
503 XcursorSetTheme (xdisplay, theme);
505 XcursorSetDefaultSize (xdisplay, size);
507 g_slist_foreach (cursor_cache, update_cursor, NULL);
513 gdk_cursor_get_image (GdkCursor *cursor)
515 g_return_val_if_fail (cursor != NULL, NULL);
521 gdk_x11_display_set_cursor_theme (GdkDisplay *display,
525 g_return_if_fail (GDK_IS_DISPLAY (display));
529 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
531 g_return_if_fail (cursor != NULL);
539 create_cursor_image (GdkPixbuf *pixbuf,
544 XcursorImage *xcimage;
545 cairo_surface_t *surface;
548 width = gdk_pixbuf_get_width (pixbuf);
549 height = gdk_pixbuf_get_height (pixbuf);
551 xcimage = XcursorImageCreate (width, height);
556 surface = cairo_image_surface_create_for_data ((guchar *) xcimage->pixels,
562 cr = cairo_create (surface);
563 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
564 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
568 cairo_surface_destroy (surface);
574 _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
579 XcursorImage *xcimage;
581 GdkX11Cursor *private;
586 if (x == -1 && (option = gdk_pixbuf_get_option (pixbuf, "x_hot")))
590 value = g_ascii_strtoll (option, &end, 10);
593 value >= 0 && value < G_MAXINT)
596 if (y == -1 && (option = gdk_pixbuf_get_option (pixbuf, "y_hot")))
600 value = g_ascii_strtoll (option, &end, 10);
603 value >= 0 && value < G_MAXINT)
607 g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
608 g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
610 if (gdk_display_is_closed (display))
616 xcimage = create_cursor_image (pixbuf, x, y);
617 xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
618 XcursorImageDestroy (xcimage);
621 private = g_object_new (GDK_TYPE_X11_CURSOR,
622 "cursor-type", GDK_CURSOR_IS_PIXMAP,
625 private->xcursor = xcursor;
626 private->name = NULL;
627 private->serial = theme_serial;
629 return GDK_CURSOR (private);
633 _gdk_x11_display_get_cursor_for_name (GdkDisplay *display,
638 GdkX11Cursor *private;
640 if (gdk_display_is_closed (display))
646 private = find_in_cache (display, GDK_CURSOR_IS_PIXMAP, name);
650 /* Cache had it, add a ref for this user */
651 g_object_ref (private);
653 return (GdkCursor*) private;
656 xdisplay = GDK_DISPLAY_XDISPLAY (display);
657 xcursor = XcursorLibraryLoadCursor (xdisplay, name);
662 private = g_object_new (GDK_TYPE_X11_CURSOR,
663 "cursor-type", GDK_CURSOR_IS_PIXMAP,
666 private->xcursor = xcursor;
667 private->name = g_strdup (name);
668 private->serial = theme_serial;
670 add_to_cache (private);
672 return GDK_CURSOR (private);
676 _gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
678 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
682 _gdk_x11_display_supports_cursor_color (GdkDisplay *display)
684 return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
688 _gdk_x11_display_get_default_cursor_size (GdkDisplay *display,
692 *width = *height = XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
698 gdk_cursor_new_from_pixmap (GdkDisplay *display,
699 Pixmap source_pixmap,
706 GdkX11Cursor *private;
710 g_return_val_if_fail (fg != NULL, NULL);
711 g_return_val_if_fail (bg != NULL, NULL);
713 xfg.pixel = fg->pixel;
716 xfg.green = fg->green;
717 xbg.pixel = bg->pixel;
720 xbg.green = bg->green;
722 if (gdk_display_is_closed (display->closed))
725 xcursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
726 source_pixmap, mask_pixmap, &xfg, &xbg, x, y);
727 private = g_object_new (GDK_TYPE_X11_CURSOR,
728 "cursor-type", GDK_CURSOR_IS_PIXMAP,
731 private->xcursor = xcursor;
732 private->name = NULL;
733 private->serial = theme_serial;
735 return GDK_CURSOR (private);
739 _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
745 cairo_surface_t *pixmap, *mask;
746 guint width, height, n_channels, rowstride, data_stride, i, j;
747 guint8 *data, *mask_data, *pixels;
748 GdkColor fg = { 0, 0, 0, 0 };
749 GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
751 cairo_surface_t *image;
754 width = gdk_pixbuf_get_width (pixbuf);
755 height = gdk_pixbuf_get_height (pixbuf);
757 g_return_val_if_fail (0 <= x && x < width, NULL);
758 g_return_val_if_fail (0 <= y && y < height, NULL);
760 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
761 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
762 pixels = gdk_pixbuf_get_pixels (pixbuf);
764 data_stride = 4 * ((width + 31) / 32);
765 data = g_new0 (guint8, data_stride * height);
766 mask_data = g_new0 (guint8, data_stride * height);
768 for (j = 0; j < height; j++)
770 guint8 *src = pixels + j * rowstride;
771 guint8 *d = data + data_stride * j;
772 guint8 *md = mask_data + data_stride * j;
774 for (i = 0; i < width; i++)
779 if (n_channels == 3 || src[3] >= 0x80)
791 screen = gdk_display_get_default_screen (display);
793 pixmap = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
795 cr = cairo_create (pixmap);
796 image = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_A1,
797 width, height, data_stride);
798 cairo_set_source_surface (cr, image, 0, 0);
799 cairo_surface_destroy (image);
800 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
804 mask = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
806 cr = cairo_create (mask);
807 image = cairo_image_surface_create_for_data (mask_data, CAIRO_FORMAT_A1,
808 width, height, data_stride);
809 cairo_set_source_surface (cr, image, 0, 0);
810 cairo_surface_destroy (image);
811 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
815 cursor = gdk_cursor_new_from_pixmap (display,
816 cairo_xlib_surface_get_drawable (pixmap),
817 cairo_xlib_surface_get_drawable (mask),
821 cairo_surface_destroy (pixmap);
822 cairo_surface_destroy (mask);
831 _gdk_x11_display_get_cursor_for_name (GdkDisplay *display,
838 _gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
844 _gdk_x11_display_supports_cursor_color (GdkDisplay *display)
850 _gdk_x11_display_get_default_cursor_size (GdkDisplay *display)
852 /* no idea, really */
860 * gdk_display_get_maximal_cursor_size:
861 * @display: a #GdkDisplay
862 * @width: (out): the return location for the maximal cursor width
863 * @height: (out): the return location for the maximal cursor height
865 * Gets the maximal size to use for cursors on @display.
870 _gdk_x11_display_get_maximal_cursor_size (GdkDisplay *display,
877 g_return_if_fail (GDK_IS_DISPLAY (display));
879 screen = gdk_display_get_default_screen (display);
880 window = gdk_screen_get_root_window (screen);
881 XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display),
882 GDK_WINDOW_XID (window),
883 128, 128, width, height);