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