]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkcursor-x11.c
Cache cursors to avoid libXcursor theme lookup overhead.
[~andy/gtk] / gdk / x11 / gdkcursor-x11.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include "config.h"
28
29 #define GDK_PIXBUF_ENABLE_BACKEND
30
31 #include <X11/Xlib.h>
32 #include <X11/cursorfont.h>
33 #ifdef HAVE_XCURSOR
34 #include <X11/Xcursor/Xcursor.h>
35 #endif
36 #ifdef HAVE_XFIXES
37 #include <X11/extensions/Xfixes.h>
38 #endif
39 #include <string.h>
40
41 #include "gdkprivate-x11.h"
42 #include "gdkcursor.h"
43 #include "gdkdisplay-x11.h"
44 #include "gdkpixmap-x11.h"
45 #include "gdkx.h"
46 #include <gdk/gdkpixmap.h>
47 #include <gdk-pixbuf/gdk-pixbuf.h>
48 #include "gdkalias.h"
49
50 static guint theme_serial = 0;
51
52 /* cursor_cache holds a cache of non-pixmap cursors to avoid expensive 
53  * libXcursor searches, cursors are added to it but never removed. We make 
54  * the assumption that since there are a small number of GdkDisplay's and 
55  * a small number of cursor's that this list will stay small enough
56  * not to be a problem.
57  */
58 static GSList* cursor_cache = NULL;
59
60 struct cursor_cache_key
61 {
62   GdkDisplay* display;
63   GdkCursorType type;
64   const char* name;
65 };
66
67 /* Caller should check if there is already a match first.
68  * Cursor MUST be either a typed cursor or a pixmap with 
69  * a non-NULL name.
70  */
71 static void
72 add_to_cache (GdkCursorPrivate* cursor)
73 {
74   cursor_cache = g_slist_prepend (cursor_cache, cursor);
75
76   /* Take a ref so that if the caller frees it we still have it */
77   gdk_cursor_ref ((GdkCursor*) cursor);
78 }
79
80 /* Returns 0 on a match
81  */
82 static gint
83 cache_compare_func (gconstpointer listelem, 
84                     gconstpointer target)
85 {
86   GdkCursorPrivate* cursor = (GdkCursorPrivate*)listelem;
87   struct cursor_cache_key* key = (struct cursor_cache_key*)target;
88
89   if ((cursor->cursor.type != key->type) ||
90       (cursor->display != key->display))
91     return 1; /* No match */
92   
93   /* Elements marked as pixmap must be named cursors 
94    * (since we don't store normal pixmap cursors 
95    */
96   if (key->type == GDK_CURSOR_IS_PIXMAP)
97     return strcmp (key->name, cursor->name);
98
99   return 0; /* Match */
100 }
101
102 /* Returns the cursor if there is a match, NULL if not
103  * For named cursors type shall be GDK_CURSOR_IS_PIXMAP
104  * For unnamed, typed cursors, name shall be NULL
105  */
106 static GdkCursorPrivate*
107 find_in_cache (GdkDisplay    *display, 
108                GdkCursorType  type,
109                const char    *name)
110 {
111   GSList* res;
112   struct cursor_cache_key key;
113
114   key.display = display;
115   key.type = type;
116   key.name = name;
117
118   res = g_slist_find_custom (cursor_cache, &key, cache_compare_func);
119
120   if (res)
121     return (GdkCursorPrivate *) res->data;
122
123   return NULL;
124 }
125
126 /* Called by gdk_display_x11_finalize to flush any cached cursors
127  * for a dead display.
128  */
129 void 
130 _gdk_x11_cursor_display_finalize (GdkDisplay *display)
131 {
132   GSList* item;
133   GSList** itemp; /* Pointer to the thing to fix when we delete an item */
134   item = cursor_cache;
135   itemp = &cursor_cache;
136   while (item)
137     {
138       GdkCursorPrivate* cursor = (GdkCursorPrivate*)(item->data);
139       if (cursor->display == display)
140         {
141           GSList* olditem;
142           gdk_cursor_unref ((GdkCursor*) cursor);
143           /* Remove this item from the list */
144           *(itemp) = item->next;
145           olditem = item;
146           item = g_slist_next (item);
147           g_slist_free_1 (olditem);
148         } 
149       else 
150         {
151           itemp = &(item->next);
152           item = g_slist_next (item);
153         }
154     }
155 }
156
157 static Cursor
158 get_blank_cursor (GdkDisplay *display)
159 {
160   GdkScreen *screen;
161   GdkPixmap *pixmap;
162   Pixmap source_pixmap;
163   XColor color;
164   Cursor cursor;
165
166   screen = gdk_display_get_default_screen (display);
167   pixmap = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen), 
168                                         "\0\0\0\0\0\0\0\0", 1, 1);
169  
170   source_pixmap = GDK_PIXMAP_XID (pixmap);
171
172   color.pixel = 0; 
173   color.red = color.blue = color.green = 0;
174   
175   if (display->closed)
176     cursor = None;
177   else
178     cursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
179                                   source_pixmap, source_pixmap,
180                                   &color, &color, 1, 1);
181   g_object_unref (pixmap);
182
183   return cursor;
184 }
185
186 /**
187  * gdk_cursor_new_for_display:
188  * @display: the #GdkDisplay for which the cursor will be created
189  * @cursor_type: cursor to create
190  * 
191  * Creates a new cursor from the set of builtin cursors.
192  * Some useful ones are:
193  * <itemizedlist>
194  * <listitem><para>
195  *  <inlinegraphic format="PNG" fileref="right_ptr.png"></inlinegraphic> #GDK_RIGHT_PTR (right-facing arrow)
196  * </para></listitem>
197  * <listitem><para>
198  *  <inlinegraphic format="PNG" fileref="crosshair.png"></inlinegraphic> #GDK_CROSSHAIR (crosshair)
199  * </para></listitem>
200  * <listitem><para>
201  *  <inlinegraphic format="PNG" fileref="xterm.png"></inlinegraphic> #GDK_XTERM (I-beam)
202  * </para></listitem>
203  * <listitem><para>
204  * <inlinegraphic format="PNG" fileref="watch.png"></inlinegraphic> #GDK_WATCH (busy)
205  * </para></listitem>
206  * <listitem><para>
207  * <inlinegraphic format="PNG" fileref="fleur.png"></inlinegraphic> #GDK_FLEUR (for moving objects)
208  * </para></listitem>
209  * <listitem><para>
210  * <inlinegraphic format="PNG" fileref="hand1.png"></inlinegraphic> #GDK_HAND1 (a right-pointing hand)
211  * </para></listitem>
212  * <listitem><para>
213  * <inlinegraphic format="PNG" fileref="hand2.png"></inlinegraphic> #GDK_HAND2 (a left-pointing hand)
214  * </para></listitem>
215  * <listitem><para>
216  * <inlinegraphic format="PNG" fileref="left_side.png"></inlinegraphic> #GDK_LEFT_SIDE (resize left side)
217  * </para></listitem>
218  * <listitem><para>
219  * <inlinegraphic format="PNG" fileref="right_side.png"></inlinegraphic> #GDK_RIGHT_SIDE (resize right side)
220  * </para></listitem>
221  * <listitem><para>
222  * <inlinegraphic format="PNG" fileref="top_left_corner.png"></inlinegraphic> #GDK_TOP_LEFT_CORNER (resize northwest corner)
223  * </para></listitem>
224  * <listitem><para>
225  * <inlinegraphic format="PNG" fileref="top_right_corner.png"></inlinegraphic> #GDK_TOP_RIGHT_CORNER (resize northeast corner)
226  * </para></listitem>
227  * <listitem><para>
228  * <inlinegraphic format="PNG" fileref="bottom_left_corner.png"></inlinegraphic> #GDK_BOTTOM_LEFT_CORNER (resize southwest corner)
229  * </para></listitem>
230  * <listitem><para>
231  * <inlinegraphic format="PNG" fileref="bottom_right_corner.png"></inlinegraphic> #GDK_BOTTOM_RIGHT_CORNER (resize southeast corner)
232  * </para></listitem>
233  * <listitem><para>
234  * <inlinegraphic format="PNG" fileref="top_side.png"></inlinegraphic> #GDK_TOP_SIDE (resize top side)
235  * </para></listitem>
236  * <listitem><para>
237  * <inlinegraphic format="PNG" fileref="bottom_side.png"></inlinegraphic> #GDK_BOTTOM_SIDE (resize bottom side)
238  * </para></listitem>
239  * <listitem><para>
240  * <inlinegraphic format="PNG" fileref="sb_h_double_arrow.png"></inlinegraphic> #GDK_SB_H_DOUBLE_ARROW (move vertical splitter)
241  * </para></listitem>
242  * <listitem><para>
243  * <inlinegraphic format="PNG" fileref="sb_v_double_arrow.png"></inlinegraphic> #GDK_SB_V_DOUBLE_ARROW (move horizontal splitter)
244  * </para></listitem>
245  * <listitem><para>
246  * #GDK_BLANK_CURSOR (Blank cursor). Since 2.16
247  * </para></listitem>
248  * </itemizedlist>
249  *
250  * Return value: a new #GdkCursor
251  *
252  * Since: 2.2
253  **/
254 GdkCursor*
255 gdk_cursor_new_for_display (GdkDisplay    *display,
256                             GdkCursorType  cursor_type)
257 {
258   GdkCursorPrivate *private;
259   GdkCursor *cursor;
260   Cursor xcursor;
261
262   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
263
264   if (display->closed)
265     {
266       xcursor = None;
267     } 
268   else 
269     {
270       private = find_in_cache (display, cursor_type, NULL);
271
272       if (private)
273         {
274           /* Cache had it, add a ref for this user */
275           gdk_cursor_ref ((GdkCursor*) private);
276        
277           return (GdkCursor*) private;
278         } 
279       else 
280         {
281           if (cursor_type != GDK_BLANK_CURSOR)
282             xcursor = XCreateFontCursor (GDK_DISPLAY_XDISPLAY (display),
283                                          cursor_type);
284           else
285             xcursor = get_blank_cursor (display);
286        }
287     }
288   
289   private = g_new (GdkCursorPrivate, 1);
290   private->display = display;
291   private->xcursor = xcursor;
292   private->name = NULL;
293   private->serial = theme_serial;
294
295   cursor = (GdkCursor *) private;
296   cursor->type = cursor_type;
297   cursor->ref_count = 1;
298   
299   if (xcursor != None)
300     add_to_cache (private);
301
302   return cursor;
303 }
304
305 /**
306  * gdk_cursor_new_from_pixmap:
307  * @source: the pixmap specifying the cursor.
308  * @mask: the pixmap specifying the mask, which must be the same size as 
309  *    @source.
310  * @fg: the foreground color, used for the bits in the source which are 1.
311  *    The color does not have to be allocated first. 
312  * @bg: the background color, used for the bits in the source which are 0.
313  *    The color does not have to be allocated first.
314  * @x: the horizontal offset of the 'hotspot' of the cursor. 
315  * @y: the vertical offset of the 'hotspot' of the cursor.
316  * 
317  * Creates a new cursor from a given pixmap and mask. Both the pixmap and mask
318  * must have a depth of 1 (i.e. each pixel has only 2 values - on or off).
319  * The standard cursor size is 16 by 16 pixels. You can create a bitmap 
320  * from inline data as in the below example.
321  * 
322  * <example><title>Creating a custom cursor</title>
323  * <programlisting>
324  * /<!-- -->* This data is in X bitmap format, and can be created with the 'bitmap'
325  *    utility. *<!-- -->/
326  * &num;define cursor1_width 16
327  * &num;define cursor1_height 16
328  * static unsigned char cursor1_bits[] = {
329  *   0x80, 0x01, 0x40, 0x02, 0x20, 0x04, 0x10, 0x08, 0x08, 0x10, 0x04, 0x20,
330  *   0x82, 0x41, 0x41, 0x82, 0x41, 0x82, 0x82, 0x41, 0x04, 0x20, 0x08, 0x10,
331  *   0x10, 0x08, 0x20, 0x04, 0x40, 0x02, 0x80, 0x01};
332  *  
333  * static unsigned char cursor1mask_bits[] = {
334  *   0x80, 0x01, 0xc0, 0x03, 0x60, 0x06, 0x30, 0x0c, 0x18, 0x18, 0x8c, 0x31,
335  *   0xc6, 0x63, 0x63, 0xc6, 0x63, 0xc6, 0xc6, 0x63, 0x8c, 0x31, 0x18, 0x18,
336  *   0x30, 0x0c, 0x60, 0x06, 0xc0, 0x03, 0x80, 0x01};
337  *  
338  *  
339  *  GdkCursor *cursor;
340  *  GdkPixmap *source, *mask;
341  *  GdkColor fg = { 0, 65535, 0, 0 }; /<!-- -->* Red. *<!-- -->/
342  *  GdkColor bg = { 0, 0, 0, 65535 }; /<!-- -->* Blue. *<!-- -->/
343  *  
344  *  
345  *  source = gdk_bitmap_create_from_data (NULL, cursor1_bits,
346  *                                        cursor1_width, cursor1_height);
347  *  mask = gdk_bitmap_create_from_data (NULL, cursor1mask_bits,
348  *                                      cursor1_width, cursor1_height);
349  *  cursor = gdk_cursor_new_from_pixmap (source, mask, &amp;fg, &amp;bg, 8, 8);
350  *  gdk_pixmap_unref (source);
351  *  gdk_pixmap_unref (mask);
352  *  
353  *  
354  *  gdk_window_set_cursor (widget->window, cursor);
355  * </programlisting>
356  * </example>
357  *
358  * Return value: a new #GdkCursor.
359  **/
360 GdkCursor*
361 gdk_cursor_new_from_pixmap (GdkPixmap      *source,
362                             GdkPixmap      *mask,
363                             const GdkColor *fg,
364                             const GdkColor *bg,
365                             gint            x,
366                             gint            y)
367 {
368   GdkCursorPrivate *private;
369   GdkCursor *cursor;
370   Pixmap source_pixmap, mask_pixmap;
371   Cursor xcursor;
372   XColor xfg, xbg;
373   GdkDisplay *display;
374
375   g_return_val_if_fail (GDK_IS_PIXMAP (source), NULL);
376   g_return_val_if_fail (GDK_IS_PIXMAP (mask), NULL);
377   g_return_val_if_fail (fg != NULL, NULL);
378   g_return_val_if_fail (bg != NULL, NULL);
379
380   source_pixmap = GDK_PIXMAP_XID (source);
381   mask_pixmap   = GDK_PIXMAP_XID (mask);
382   display = GDK_PIXMAP_DISPLAY (source);
383
384   xfg.pixel = fg->pixel;
385   xfg.red = fg->red;
386   xfg.blue = fg->blue;
387   xfg.green = fg->green;
388   xbg.pixel = bg->pixel;
389   xbg.red = bg->red;
390   xbg.blue = bg->blue;
391   xbg.green = bg->green;
392   
393   if (display->closed)
394     xcursor = None;
395   else
396     xcursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
397                                    source_pixmap, mask_pixmap, &xfg, &xbg, x, y);
398   private = g_new (GdkCursorPrivate, 1);
399   private->display = display;
400   private->xcursor = xcursor;
401   private->name = NULL;
402   private->serial = theme_serial;
403
404   cursor = (GdkCursor *) private;
405   cursor->type = GDK_CURSOR_IS_PIXMAP;
406   cursor->ref_count = 1;
407   
408   return cursor;
409 }
410
411 void
412 _gdk_cursor_destroy (GdkCursor *cursor)
413 {
414   GdkCursorPrivate *private;
415
416   g_return_if_fail (cursor != NULL);
417   g_return_if_fail (cursor->ref_count == 0);
418
419   private = (GdkCursorPrivate *) cursor;
420   if (!private->display->closed && private->xcursor)
421     XFreeCursor (GDK_DISPLAY_XDISPLAY (private->display), private->xcursor);
422
423   g_free (private->name);
424   g_free (private);
425 }
426
427 /**
428  * gdk_x11_cursor_get_xdisplay:
429  * @cursor: a #GdkCursor.
430  * 
431  * Returns the display of a #GdkCursor.
432  * 
433  * Return value: an Xlib <type>Display*</type>.
434  **/
435 Display *
436 gdk_x11_cursor_get_xdisplay (GdkCursor *cursor)
437 {
438   g_return_val_if_fail (cursor != NULL, NULL);
439
440   return GDK_DISPLAY_XDISPLAY(((GdkCursorPrivate *)cursor)->display);
441 }
442
443 /**
444  * gdk_x11_cursor_get_xcursor:
445  * @cursor: a #GdkCursor.
446  * 
447  * Returns the X cursor belonging to a #GdkCursor.
448  * 
449  * Return value: an Xlib <type>Cursor</type>.
450  **/
451 Cursor
452 gdk_x11_cursor_get_xcursor (GdkCursor *cursor)
453 {
454   g_return_val_if_fail (cursor != NULL, None);
455
456   return ((GdkCursorPrivate *)cursor)->xcursor;
457 }
458
459 /** 
460  * gdk_cursor_get_display:
461  * @cursor: a #GdkCursor.
462  *
463  * Returns the display on which the #GdkCursor is defined.
464  *
465  * Returns: the #GdkDisplay associated to @cursor
466  *
467  * Since: 2.2
468  */
469
470 GdkDisplay *
471 gdk_cursor_get_display (GdkCursor *cursor)
472 {
473   g_return_val_if_fail (cursor != NULL, NULL);
474
475   return ((GdkCursorPrivate *)cursor)->display;
476 }
477
478 #if defined(HAVE_XCURSOR) && defined(HAVE_XFIXES) && XFIXES_MAJOR >= 2
479
480 /**
481  * gdk_cursor_get_image:
482  * @cursor: a #GdkCursor
483  *
484  * Returns a #GdkPixbuf with the image used to display the cursor.
485  *
486  * Note that depending on the capabilities of the windowing system and 
487  * on the cursor, GDK may not be able to obtain the image data. In this 
488  * case, %NULL is returned.
489  *
490  * Returns: a #GdkPixbuf representing @cursor, or %NULL
491  *
492  * Since: 2.8
493  */
494 GdkPixbuf*  
495 gdk_cursor_get_image (GdkCursor *cursor)
496 {
497   Display *xdisplay;
498   GdkCursorPrivate *private;
499   XcursorImages *images = NULL;
500   XcursorImage *image;
501   gint size;
502   gchar buf[32];
503   guchar *data, *p, tmp;
504   GdkPixbuf *pixbuf;
505   gchar *theme;
506   
507   g_return_val_if_fail (cursor != NULL, NULL);
508
509   private = (GdkCursorPrivate *) cursor;
510     
511   xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
512
513   size = XcursorGetDefaultSize (xdisplay);
514   theme = XcursorGetTheme (xdisplay);
515
516   if (cursor->type == GDK_CURSOR_IS_PIXMAP)
517     {
518       if (private->name)
519         images = XcursorLibraryLoadImages (private->name, theme, size);
520     }
521   else 
522     images = XcursorShapeLoadImages (cursor->type, theme, size);
523
524   if (!images)
525     return NULL;
526   
527   image = images->images[0];
528
529   data = g_malloc (4 * image->width * image->height);
530   memcpy (data, image->pixels, 4 * image->width * image->height);
531
532   for (p = data; p < data + (4 * image->width * image->height); p += 4)
533     {
534       tmp = p[0];
535       p[0] = p[2];
536       p[2] = tmp;
537     }
538
539   pixbuf = gdk_pixbuf_new_from_data (data, GDK_COLORSPACE_RGB, TRUE,
540                                      8, image->width, image->height,
541                                      4 * image->width, 
542                                      (GdkPixbufDestroyNotify)g_free, NULL);
543
544   if (private->name)
545     gdk_pixbuf_set_option (pixbuf, "name", private->name);
546   g_snprintf (buf, 32, "%d", image->xhot);
547   gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
548   g_snprintf (buf, 32, "%d", image->yhot);
549   gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
550
551   XcursorImagesDestroy (images);
552
553   return pixbuf;
554 }
555
556 void
557 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
558 {
559   Display *xdisplay;
560   GdkCursorPrivate *private;
561   Cursor new_cursor = None;
562   GdkDisplayX11 *display_x11;
563
564   private = (GdkCursorPrivate *) cursor;
565   xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
566   display_x11 = GDK_DISPLAY_X11 (private->display);
567
568   if (!display_x11->have_xfixes)
569     return;
570
571   if (private->serial == theme_serial)
572     return;
573
574   private->serial = theme_serial;
575
576   if (private->xcursor != None)
577     {
578       if (cursor->type == GDK_CURSOR_IS_PIXMAP)
579         {
580           if (private->name)
581             new_cursor = XcursorLibraryLoadCursor (xdisplay, private->name);
582         }
583       else 
584         new_cursor = XcursorShapeLoadCursor (xdisplay, cursor->type);
585       
586       if (new_cursor != None)
587         {
588           XFixesChangeCursor (xdisplay, new_cursor, private->xcursor);
589           private->xcursor = new_cursor;
590         }
591     }
592 }
593
594 static void
595 update_cursor (gpointer data,
596                gpointer user_data)
597 {
598   GdkCursor *cursor;
599
600   cursor = (GdkCursor*)(data);
601
602   if (!cursor)
603     return;
604   
605   _gdk_x11_cursor_update_theme (cursor);
606 }
607
608 /**
609  * gdk_x11_display_set_cursor_theme:
610  * @display: a #GdkDisplay
611  * @theme: the name of the cursor theme to use, or %NULL to unset
612  *         a previously set value 
613  * @size: the cursor size to use, or 0 to keep the previous size
614  *
615  * Sets the cursor theme from which the images for cursor
616  * should be taken. 
617  * 
618  * If the windowing system supports it, existing cursors created 
619  * with gdk_cursor_new(), gdk_cursor_new_for_display() and 
620  * gdk_cursor_new_for_name() are updated to reflect the theme 
621  * change. Custom cursors constructed with gdk_cursor_new_from_pixmap() 
622  * or gdk_cursor_new_from_pixbuf() will have to be handled
623  * by the application (GTK+ applications can learn about 
624  * cursor theme changes by listening for change notification
625  * for the corresponding #GtkSetting).
626  *
627  * Since: 2.8
628  */
629 void
630 gdk_x11_display_set_cursor_theme (GdkDisplay  *display,
631                                   const gchar *theme,
632                                   const gint   size)
633 {
634   GdkDisplayX11 *display_x11;
635   Display *xdisplay;
636   gchar *old_theme;
637   gint old_size;
638
639   g_return_if_fail (GDK_IS_DISPLAY (display));
640
641   display_x11 = GDK_DISPLAY_X11 (display);
642   xdisplay = GDK_DISPLAY_XDISPLAY (display);
643
644   old_theme = XcursorGetTheme (xdisplay);
645   old_size = XcursorGetDefaultSize (xdisplay);
646
647   if (old_size == size &&
648       (old_theme == theme ||
649        (old_theme && theme && strcmp (old_theme, theme) == 0)))
650     return;
651
652   theme_serial++;
653
654   XcursorSetTheme (xdisplay, theme);
655   if (size > 0)
656     XcursorSetDefaultSize (xdisplay, size);
657     
658   g_slist_foreach (cursor_cache, update_cursor, NULL);
659 }
660
661 #else
662
663 GdkPixbuf*  
664 gdk_cursor_get_image (GdkCursor *cursor)
665 {
666   g_return_val_if_fail (cursor != NULL, NULL);
667   
668   return NULL;
669 }
670
671 void
672 gdk_x11_display_set_cursor_theme (GdkDisplay  *display,
673                                   const gchar *theme,
674                                   const gint   size)
675 {
676   g_return_if_fail (GDK_IS_DISPLAY (display));
677 }
678
679 void
680 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
681 {
682   g_return_if_fail (cursor != NULL);
683 }
684
685 #endif
686
687 #ifdef HAVE_XCURSOR
688
689 static XcursorImage*
690 create_cursor_image (GdkPixbuf *pixbuf,
691                      gint       x,
692                      gint       y)
693 {
694   guint width, height, rowstride, n_channels;
695   guchar *pixels, *src;
696   XcursorImage *xcimage;
697   XcursorPixel *dest;
698
699   width = gdk_pixbuf_get_width (pixbuf);
700   height = gdk_pixbuf_get_height (pixbuf);
701
702   n_channels = gdk_pixbuf_get_n_channels (pixbuf);
703   rowstride = gdk_pixbuf_get_rowstride (pixbuf);
704   pixels = gdk_pixbuf_get_pixels (pixbuf);
705
706   xcimage = XcursorImageCreate (width, height);
707
708   xcimage->xhot = x;
709   xcimage->yhot = y;
710
711   dest = xcimage->pixels;
712
713   if (n_channels == 3)
714     {
715       gint i, j;
716
717       for (j = 0; j < height; j++)
718         {
719           src = pixels + j * rowstride;
720           for (i = 0; i < width; i++)
721             {
722               *dest = (0xff << 24) | (src[0] << 16) | (src[1] << 8) | src[2];
723             }
724
725           src += n_channels;
726           dest++;
727         }
728     }
729   else
730     {
731       _gdk_x11_convert_to_format (pixels, rowstride,
732                                   (guchar *) dest, 4 * width,
733                                   GDK_X11_FORMAT_ARGB,
734                                   (G_BYTE_ORDER == G_BIG_ENDIAN) ?
735                                   GDK_MSB_FIRST : GDK_LSB_FIRST,
736                                   width, height);
737     }
738
739   return xcimage;
740 }
741
742
743 /**
744  * gdk_cursor_new_from_pixbuf:
745  * @display: the #GdkDisplay for which the cursor will be created
746  * @pixbuf: the #GdkPixbuf containing the cursor image
747  * @x: the horizontal offset of the 'hotspot' of the cursor. 
748  * @y: the vertical offset of the 'hotspot' of the cursor.
749  *
750  * Creates a new cursor from a pixbuf. 
751  *
752  * Not all GDK backends support RGBA cursors. If they are not 
753  * supported, a monochrome approximation will be displayed. 
754  * The functions gdk_display_supports_cursor_alpha() and 
755  * gdk_display_supports_cursor_color() can be used to determine
756  * whether RGBA cursors are supported; 
757  * gdk_display_get_default_cursor_size() and 
758  * gdk_display_get_maximal_cursor_size() give information about 
759  * cursor sizes.
760  *
761  * On the X backend, support for RGBA cursors requires a
762  * sufficently new version of the X Render extension. 
763  *
764  * Returns: a new #GdkCursor.
765  * 
766  * Since: 2.4
767  */
768 GdkCursor *
769 gdk_cursor_new_from_pixbuf (GdkDisplay *display, 
770                             GdkPixbuf  *pixbuf,
771                             gint        x,
772                             gint        y)
773 {
774   XcursorImage *xcimage;
775   Cursor xcursor;
776   GdkCursorPrivate *private;
777   GdkCursor *cursor;
778
779   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
780   g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
781   g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
782   g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
783
784   if (display->closed)
785     xcursor = None;
786   else 
787     {
788       xcimage = create_cursor_image (pixbuf, x, y);
789       xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
790       XcursorImageDestroy (xcimage);
791     }
792
793   private = g_new (GdkCursorPrivate, 1);
794   private->display = display;
795   private->xcursor = xcursor;
796   private->name = NULL;
797   private->serial = theme_serial;
798
799   cursor = (GdkCursor *) private;
800   cursor->type = GDK_CURSOR_IS_PIXMAP;
801   cursor->ref_count = 1;
802   
803   return cursor;
804 }
805
806 /**
807  * gdk_cursor_new_from_name:
808  * @display: the #GdkDisplay for which the cursor will be created
809  * @name: the name of the cursor
810  *
811  * Creates a new cursor by looking up @name in the current cursor
812  * theme. 
813  * 
814  * Returns: a new #GdkCursor, or %NULL if there is no cursor with 
815  *   the given name 
816  *
817  * Since: 2.8
818  */
819 GdkCursor*  
820 gdk_cursor_new_from_name (GdkDisplay  *display,
821                           const gchar *name)
822 {
823   Cursor xcursor;
824   Display *xdisplay;
825   GdkCursorPrivate *private;
826   GdkCursor *cursor;
827
828   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
829
830   if (display->closed)
831     xcursor = None;
832   else 
833     {
834       private = find_in_cache (display, GDK_CURSOR_IS_PIXMAP, name);
835
836       if (private)
837         {
838           /* Cache had it, add a ref for this user */
839           gdk_cursor_ref ((GdkCursor*) private);
840
841           return (GdkCursor*) private;
842         }
843
844       xdisplay = GDK_DISPLAY_XDISPLAY (display);
845       xcursor = XcursorLibraryLoadCursor (xdisplay, name);
846       if (xcursor == None)
847         return NULL;
848     }
849
850   private = g_new (GdkCursorPrivate, 1);
851   private->display = display;
852   private->xcursor = xcursor;
853   private->name = g_strdup (name);
854   private->serial = theme_serial;
855
856   cursor = (GdkCursor *) private;
857   cursor->type = GDK_CURSOR_IS_PIXMAP;
858   cursor->ref_count = 1;
859   add_to_cache (private);
860
861   return cursor;
862 }
863
864 /**
865  * gdk_display_supports_cursor_alpha:
866  * @display: a #GdkDisplay
867  *
868  * Returns %TRUE if cursors can use an 8bit alpha channel 
869  * on @display. Otherwise, cursors are restricted to bilevel 
870  * alpha (i.e. a mask).
871  *
872  * Returns: whether cursors can have alpha channels.
873  *
874  * Since: 2.4
875  */
876 gboolean 
877 gdk_display_supports_cursor_alpha (GdkDisplay *display)
878 {
879   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
880
881   return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
882 }
883
884 /**
885  * gdk_display_supports_cursor_color:
886  * @display: a #GdkDisplay
887  *
888  * Returns %TRUE if multicolored cursors are supported
889  * on @display. Otherwise, cursors have only a forground
890  * and a background color.
891  *
892  * Returns: whether cursors can have multiple colors.
893  *
894  * Since: 2.4
895  */
896 gboolean 
897 gdk_display_supports_cursor_color (GdkDisplay *display)
898 {
899   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
900
901   return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
902 }
903
904 /**
905  * gdk_display_get_default_cursor_size:
906  * @display: a #GdkDisplay
907  *
908  * Returns the default size to use for cursors on @display.
909  *
910  * Returns: the default cursor size.
911  *
912  * Since: 2.4
913  */
914 guint     
915 gdk_display_get_default_cursor_size (GdkDisplay *display)
916 {
917   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
918
919   return XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
920 }
921
922 #else
923
924 GdkCursor *
925 gdk_cursor_new_from_pixbuf (GdkDisplay *display, 
926                             GdkPixbuf  *pixbuf,
927                             gint        x,
928                             gint        y)
929 {
930   GdkCursor *cursor;
931   GdkPixmap *pixmap, *mask;
932   guint width, height, n_channels, rowstride, i, j;
933   guint8 *data, *mask_data, *pixels;
934   GdkColor fg = { 0, 0, 0, 0 };
935   GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
936   GdkScreen *screen;
937
938   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
939   g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
940
941   width = gdk_pixbuf_get_width (pixbuf);
942   height = gdk_pixbuf_get_height (pixbuf);
943
944   g_return_val_if_fail (0 <= x && x < width, NULL);
945   g_return_val_if_fail (0 <= y && y < height, NULL);
946
947   n_channels = gdk_pixbuf_get_n_channels (pixbuf);
948   rowstride = gdk_pixbuf_get_rowstride (pixbuf);
949   pixels = gdk_pixbuf_get_pixels (pixbuf);
950
951   data = g_new0 (guint8, (width + 7) / 8 * height);
952   mask_data = g_new0 (guint8, (width + 7) / 8 * height);
953
954   for (j = 0; j < height; j++)
955     {
956       guint8 *src = pixels + j * rowstride;
957       guint8 *d = data + (width + 7) / 8 * j;
958       guint8 *md = mask_data + (width + 7) / 8 * j;
959         
960       for (i = 0; i < width; i++)
961         {
962           if (src[1] < 0x80)
963             *d |= 1 << (i % 8);
964           
965           if (n_channels == 3 || src[3] >= 0x80)
966             *md |= 1 << (i % 8);
967           
968           src += n_channels;
969           if (i % 8 == 7)
970             {
971               d++; 
972               md++;
973             }
974         }
975     }
976       
977   screen = gdk_display_get_default_screen (display);
978   pixmap = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen), 
979                                         data, width, height);
980  
981   mask = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
982                                       mask_data, width, height);
983    
984   cursor = gdk_cursor_new_from_pixmap (pixmap, mask, &fg, &bg, x, y);
985    
986   g_object_unref (pixmap);
987   g_object_unref (mask);
988
989   g_free (data);
990   g_free (mask_data);
991   
992   return cursor;
993 }
994
995 GdkCursor*  
996 gdk_cursor_new_from_name (GdkDisplay  *display,
997                           const gchar *name)
998 {
999   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1000
1001   return NULL;
1002 }
1003
1004 gboolean 
1005 gdk_display_supports_cursor_alpha (GdkDisplay    *display)
1006 {
1007   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
1008
1009   return FALSE;
1010 }
1011
1012 gboolean 
1013 gdk_display_supports_cursor_color (GdkDisplay    *display)
1014 {
1015   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
1016
1017   return FALSE;
1018 }
1019
1020 guint     
1021 gdk_display_get_default_cursor_size (GdkDisplay    *display)
1022 {
1023   g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
1024   
1025   /* no idea, really */
1026   return 20; 
1027 }
1028
1029 #endif
1030
1031
1032 /**
1033  * gdk_display_get_maximal_cursor_size:
1034  * @display: a #GdkDisplay
1035  * @width: the return location for the maximal cursor width
1036  * @height: the return location for the maximal cursor height
1037  *
1038  * Gets the maximal size to use for cursors on @display.
1039  *
1040  * Since: 2.4
1041  */
1042 void     
1043 gdk_display_get_maximal_cursor_size (GdkDisplay *display,
1044                                      guint       *width,
1045                                      guint       *height)
1046 {
1047   GdkScreen *screen;
1048   GdkWindow *window;
1049
1050   g_return_if_fail (GDK_IS_DISPLAY (display));
1051   
1052   screen = gdk_display_get_default_screen (display);
1053   window = gdk_screen_get_root_window (screen);
1054   XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display), 
1055                     GDK_WINDOW_XWINDOW (window), 
1056                     128, 128, width, height);
1057 }
1058
1059 #define __GDK_CURSOR_X11_C__
1060 #include "gdkaliasdef.c"