]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkcursor-x11.c
Don't update blank cursors
[~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 only removed when
54  * their display is closed. We make the assumption that since there are 
55  * a small number of display's and a small number of cursor's that this 
56  * list will stay small enough 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_BLANK_CURSOR)
579         return;
580
581       if (cursor->type == GDK_CURSOR_IS_PIXMAP)
582         {
583           if (private->name)
584             new_cursor = XcursorLibraryLoadCursor (xdisplay, private->name);
585         }
586       else 
587         new_cursor = XcursorShapeLoadCursor (xdisplay, cursor->type);
588       
589       if (new_cursor != None)
590         {
591           XFixesChangeCursor (xdisplay, new_cursor, private->xcursor);
592           private->xcursor = new_cursor;
593         }
594     }
595 }
596
597 static void
598 update_cursor (gpointer data,
599                gpointer user_data)
600 {
601   GdkCursor *cursor;
602
603   cursor = (GdkCursor*)(data);
604
605   if (!cursor)
606     return;
607   
608   _gdk_x11_cursor_update_theme (cursor);
609 }
610
611 /**
612  * gdk_x11_display_set_cursor_theme:
613  * @display: a #GdkDisplay
614  * @theme: the name of the cursor theme to use, or %NULL to unset
615  *         a previously set value 
616  * @size: the cursor size to use, or 0 to keep the previous size
617  *
618  * Sets the cursor theme from which the images for cursor
619  * should be taken. 
620  * 
621  * If the windowing system supports it, existing cursors created 
622  * with gdk_cursor_new(), gdk_cursor_new_for_display() and 
623  * gdk_cursor_new_for_name() are updated to reflect the theme 
624  * change. Custom cursors constructed with gdk_cursor_new_from_pixmap() 
625  * or gdk_cursor_new_from_pixbuf() will have to be handled
626  * by the application (GTK+ applications can learn about 
627  * cursor theme changes by listening for change notification
628  * for the corresponding #GtkSetting).
629  *
630  * Since: 2.8
631  */
632 void
633 gdk_x11_display_set_cursor_theme (GdkDisplay  *display,
634                                   const gchar *theme,
635                                   const gint   size)
636 {
637   GdkDisplayX11 *display_x11;
638   Display *xdisplay;
639   gchar *old_theme;
640   gint old_size;
641
642   g_return_if_fail (GDK_IS_DISPLAY (display));
643
644   display_x11 = GDK_DISPLAY_X11 (display);
645   xdisplay = GDK_DISPLAY_XDISPLAY (display);
646
647   old_theme = XcursorGetTheme (xdisplay);
648   old_size = XcursorGetDefaultSize (xdisplay);
649
650   if (old_size == size &&
651       (old_theme == theme ||
652        (old_theme && theme && strcmp (old_theme, theme) == 0)))
653     return;
654
655   theme_serial++;
656
657   XcursorSetTheme (xdisplay, theme);
658   if (size > 0)
659     XcursorSetDefaultSize (xdisplay, size);
660     
661   g_slist_foreach (cursor_cache, update_cursor, NULL);
662 }
663
664 #else
665
666 GdkPixbuf*  
667 gdk_cursor_get_image (GdkCursor *cursor)
668 {
669   g_return_val_if_fail (cursor != NULL, NULL);
670   
671   return NULL;
672 }
673
674 void
675 gdk_x11_display_set_cursor_theme (GdkDisplay  *display,
676                                   const gchar *theme,
677                                   const gint   size)
678 {
679   g_return_if_fail (GDK_IS_DISPLAY (display));
680 }
681
682 void
683 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
684 {
685   g_return_if_fail (cursor != NULL);
686 }
687
688 #endif
689
690 #ifdef HAVE_XCURSOR
691
692 static XcursorImage*
693 create_cursor_image (GdkPixbuf *pixbuf,
694                      gint       x,
695                      gint       y)
696 {
697   guint width, height, rowstride, n_channels;
698   guchar *pixels, *src;
699   XcursorImage *xcimage;
700   XcursorPixel *dest;
701
702   width = gdk_pixbuf_get_width (pixbuf);
703   height = gdk_pixbuf_get_height (pixbuf);
704
705   n_channels = gdk_pixbuf_get_n_channels (pixbuf);
706   rowstride = gdk_pixbuf_get_rowstride (pixbuf);
707   pixels = gdk_pixbuf_get_pixels (pixbuf);
708
709   xcimage = XcursorImageCreate (width, height);
710
711   xcimage->xhot = x;
712   xcimage->yhot = y;
713
714   dest = xcimage->pixels;
715
716   if (n_channels == 3)
717     {
718       gint i, j;
719
720       for (j = 0; j < height; j++)
721         {
722           src = pixels + j * rowstride;
723           for (i = 0; i < width; i++)
724             {
725               *dest = (0xff << 24) | (src[0] << 16) | (src[1] << 8) | src[2];
726             }
727
728           src += n_channels;
729           dest++;
730         }
731     }
732   else
733     {
734       _gdk_x11_convert_to_format (pixels, rowstride,
735                                   (guchar *) dest, 4 * width,
736                                   GDK_X11_FORMAT_ARGB,
737                                   (G_BYTE_ORDER == G_BIG_ENDIAN) ?
738                                   GDK_MSB_FIRST : GDK_LSB_FIRST,
739                                   width, height);
740     }
741
742   return xcimage;
743 }
744
745
746 /**
747  * gdk_cursor_new_from_pixbuf:
748  * @display: the #GdkDisplay for which the cursor will be created
749  * @pixbuf: the #GdkPixbuf containing the cursor image
750  * @x: the horizontal offset of the 'hotspot' of the cursor. 
751  * @y: the vertical offset of the 'hotspot' of the cursor.
752  *
753  * Creates a new cursor from a pixbuf. 
754  *
755  * Not all GDK backends support RGBA cursors. If they are not 
756  * supported, a monochrome approximation will be displayed. 
757  * The functions gdk_display_supports_cursor_alpha() and 
758  * gdk_display_supports_cursor_color() can be used to determine
759  * whether RGBA cursors are supported; 
760  * gdk_display_get_default_cursor_size() and 
761  * gdk_display_get_maximal_cursor_size() give information about 
762  * cursor sizes.
763  *
764  * On the X backend, support for RGBA cursors requires a
765  * sufficently new version of the X Render extension. 
766  *
767  * Returns: a new #GdkCursor.
768  * 
769  * Since: 2.4
770  */
771 GdkCursor *
772 gdk_cursor_new_from_pixbuf (GdkDisplay *display, 
773                             GdkPixbuf  *pixbuf,
774                             gint        x,
775                             gint        y)
776 {
777   XcursorImage *xcimage;
778   Cursor xcursor;
779   GdkCursorPrivate *private;
780   GdkCursor *cursor;
781
782   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
783   g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
784   g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
785   g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
786
787   if (display->closed)
788     xcursor = None;
789   else 
790     {
791       xcimage = create_cursor_image (pixbuf, x, y);
792       xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
793       XcursorImageDestroy (xcimage);
794     }
795
796   private = g_new (GdkCursorPrivate, 1);
797   private->display = display;
798   private->xcursor = xcursor;
799   private->name = NULL;
800   private->serial = theme_serial;
801
802   cursor = (GdkCursor *) private;
803   cursor->type = GDK_CURSOR_IS_PIXMAP;
804   cursor->ref_count = 1;
805   
806   return cursor;
807 }
808
809 /**
810  * gdk_cursor_new_from_name:
811  * @display: the #GdkDisplay for which the cursor will be created
812  * @name: the name of the cursor
813  *
814  * Creates a new cursor by looking up @name in the current cursor
815  * theme. 
816  * 
817  * Returns: a new #GdkCursor, or %NULL if there is no cursor with 
818  *   the given name 
819  *
820  * Since: 2.8
821  */
822 GdkCursor*  
823 gdk_cursor_new_from_name (GdkDisplay  *display,
824                           const gchar *name)
825 {
826   Cursor xcursor;
827   Display *xdisplay;
828   GdkCursorPrivate *private;
829   GdkCursor *cursor;
830
831   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
832
833   if (display->closed)
834     xcursor = None;
835   else 
836     {
837       private = find_in_cache (display, GDK_CURSOR_IS_PIXMAP, name);
838
839       if (private)
840         {
841           /* Cache had it, add a ref for this user */
842           gdk_cursor_ref ((GdkCursor*) private);
843
844           return (GdkCursor*) private;
845         }
846
847       xdisplay = GDK_DISPLAY_XDISPLAY (display);
848       xcursor = XcursorLibraryLoadCursor (xdisplay, name);
849       if (xcursor == None)
850         return NULL;
851     }
852
853   private = g_new (GdkCursorPrivate, 1);
854   private->display = display;
855   private->xcursor = xcursor;
856   private->name = g_strdup (name);
857   private->serial = theme_serial;
858
859   cursor = (GdkCursor *) private;
860   cursor->type = GDK_CURSOR_IS_PIXMAP;
861   cursor->ref_count = 1;
862   add_to_cache (private);
863
864   return cursor;
865 }
866
867 /**
868  * gdk_display_supports_cursor_alpha:
869  * @display: a #GdkDisplay
870  *
871  * Returns %TRUE if cursors can use an 8bit alpha channel 
872  * on @display. Otherwise, cursors are restricted to bilevel 
873  * alpha (i.e. a mask).
874  *
875  * Returns: whether cursors can have alpha channels.
876  *
877  * Since: 2.4
878  */
879 gboolean 
880 gdk_display_supports_cursor_alpha (GdkDisplay *display)
881 {
882   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
883
884   return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
885 }
886
887 /**
888  * gdk_display_supports_cursor_color:
889  * @display: a #GdkDisplay
890  *
891  * Returns %TRUE if multicolored cursors are supported
892  * on @display. Otherwise, cursors have only a forground
893  * and a background color.
894  *
895  * Returns: whether cursors can have multiple colors.
896  *
897  * Since: 2.4
898  */
899 gboolean 
900 gdk_display_supports_cursor_color (GdkDisplay *display)
901 {
902   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
903
904   return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
905 }
906
907 /**
908  * gdk_display_get_default_cursor_size:
909  * @display: a #GdkDisplay
910  *
911  * Returns the default size to use for cursors on @display.
912  *
913  * Returns: the default cursor size.
914  *
915  * Since: 2.4
916  */
917 guint     
918 gdk_display_get_default_cursor_size (GdkDisplay *display)
919 {
920   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
921
922   return XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
923 }
924
925 #else
926
927 GdkCursor *
928 gdk_cursor_new_from_pixbuf (GdkDisplay *display, 
929                             GdkPixbuf  *pixbuf,
930                             gint        x,
931                             gint        y)
932 {
933   GdkCursor *cursor;
934   GdkPixmap *pixmap, *mask;
935   guint width, height, n_channels, rowstride, i, j;
936   guint8 *data, *mask_data, *pixels;
937   GdkColor fg = { 0, 0, 0, 0 };
938   GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
939   GdkScreen *screen;
940
941   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
942   g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
943
944   width = gdk_pixbuf_get_width (pixbuf);
945   height = gdk_pixbuf_get_height (pixbuf);
946
947   g_return_val_if_fail (0 <= x && x < width, NULL);
948   g_return_val_if_fail (0 <= y && y < height, NULL);
949
950   n_channels = gdk_pixbuf_get_n_channels (pixbuf);
951   rowstride = gdk_pixbuf_get_rowstride (pixbuf);
952   pixels = gdk_pixbuf_get_pixels (pixbuf);
953
954   data = g_new0 (guint8, (width + 7) / 8 * height);
955   mask_data = g_new0 (guint8, (width + 7) / 8 * height);
956
957   for (j = 0; j < height; j++)
958     {
959       guint8 *src = pixels + j * rowstride;
960       guint8 *d = data + (width + 7) / 8 * j;
961       guint8 *md = mask_data + (width + 7) / 8 * j;
962         
963       for (i = 0; i < width; i++)
964         {
965           if (src[1] < 0x80)
966             *d |= 1 << (i % 8);
967           
968           if (n_channels == 3 || src[3] >= 0x80)
969             *md |= 1 << (i % 8);
970           
971           src += n_channels;
972           if (i % 8 == 7)
973             {
974               d++; 
975               md++;
976             }
977         }
978     }
979       
980   screen = gdk_display_get_default_screen (display);
981   pixmap = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen), 
982                                         data, width, height);
983  
984   mask = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
985                                       mask_data, width, height);
986    
987   cursor = gdk_cursor_new_from_pixmap (pixmap, mask, &fg, &bg, x, y);
988    
989   g_object_unref (pixmap);
990   g_object_unref (mask);
991
992   g_free (data);
993   g_free (mask_data);
994   
995   return cursor;
996 }
997
998 GdkCursor*  
999 gdk_cursor_new_from_name (GdkDisplay  *display,
1000                           const gchar *name)
1001 {
1002   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
1003
1004   return NULL;
1005 }
1006
1007 gboolean 
1008 gdk_display_supports_cursor_alpha (GdkDisplay    *display)
1009 {
1010   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
1011
1012   return FALSE;
1013 }
1014
1015 gboolean 
1016 gdk_display_supports_cursor_color (GdkDisplay    *display)
1017 {
1018   g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
1019
1020   return FALSE;
1021 }
1022
1023 guint     
1024 gdk_display_get_default_cursor_size (GdkDisplay    *display)
1025 {
1026   g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
1027   
1028   /* no idea, really */
1029   return 20; 
1030 }
1031
1032 #endif
1033
1034
1035 /**
1036  * gdk_display_get_maximal_cursor_size:
1037  * @display: a #GdkDisplay
1038  * @width: the return location for the maximal cursor width
1039  * @height: the return location for the maximal cursor height
1040  *
1041  * Gets the maximal size to use for cursors on @display.
1042  *
1043  * Since: 2.4
1044  */
1045 void     
1046 gdk_display_get_maximal_cursor_size (GdkDisplay *display,
1047                                      guint       *width,
1048                                      guint       *height)
1049 {
1050   GdkScreen *screen;
1051   GdkWindow *window;
1052
1053   g_return_if_fail (GDK_IS_DISPLAY (display));
1054   
1055   screen = gdk_display_get_default_screen (display);
1056   window = gdk_screen_get_root_window (screen);
1057   XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display), 
1058                     GDK_WINDOW_XWINDOW (window), 
1059                     128, 128, width, height);
1060 }
1061
1062 #define __GDK_CURSOR_X11_C__
1063 #include "gdkaliasdef.c"