]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkcursor-x11.c
x11: Rename GdkDisplayX11 to GdkX11Display
[~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 #include "gdkcursorprivate.h"
35 #include "gdkprivate-x11.h"
36 #include "gdkdisplay-x11.h"
37
38 #include <X11/Xlib.h>
39 #include <X11/cursorfont.h>
40 #ifdef HAVE_XCURSOR
41 #include <X11/Xcursor/Xcursor.h>
42 #endif
43 #ifdef HAVE_XFIXES
44 #include <X11/extensions/Xfixes.h>
45 #endif
46 #include <string.h>
47 #include <errno.h>
48
49 #define GDK_TYPE_X11_CURSOR              (gdk_x11_cursor_get_type ())
50 #define GDK_X11_CURSOR(object)           (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_X11_CURSOR, GdkX11Cursor))
51 #define GDK_X11_CURSOR_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_X11_CURSOR, GdkX11CursorClass))
52 #define GDK_IS_X11_CURSOR(object)        (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_X11_CURSOR))
53 #define GDK_IS_X11_CURSOR_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_X11_CURSOR))
54 #define GDK_X11_CURSOR_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_X11_CURSOR, GdkX11CursorClass))
55
56 typedef struct _GdkX11Cursor GdkX11Cursor;
57 typedef struct _GdkX11CursorClass GdkX11CursorClass;
58
59 struct _GdkX11Cursor
60 {
61   GdkCursor cursor;
62
63   Cursor xcursor;
64   gchar *name;
65   guint serial;
66 };
67
68 struct _GdkX11CursorClass
69 {
70   GdkCursorClass cursor_class;
71 };
72
73 static guint theme_serial = 0;
74
75 /* cursor_cache holds a cache of non-pixmap cursors to avoid expensive 
76  * libXcursor searches, cursors are added to it but only removed when
77  * their display is closed. We make the assumption that since there are 
78  * a small number of display's and a small number of cursor's that this 
79  * list will stay small enough not to be a problem.
80  */
81 static GSList* cursor_cache = NULL;
82
83 struct cursor_cache_key
84 {
85   GdkDisplay* display;
86   GdkCursorType type;
87   const char* name;
88 };
89
90 /* Caller should check if there is already a match first.
91  * Cursor MUST be either a typed cursor or a pixmap with 
92  * a non-NULL name.
93  */
94 static void
95 add_to_cache (GdkX11Cursor* cursor)
96 {
97   cursor_cache = g_slist_prepend (cursor_cache, cursor);
98
99   /* Take a ref so that if the caller frees it we still have it */
100   g_object_ref (cursor);
101 }
102
103 /* Returns 0 on a match
104  */
105 static gint
106 cache_compare_func (gconstpointer listelem, 
107                     gconstpointer target)
108 {
109   GdkX11Cursor* cursor = (GdkX11Cursor*)listelem;
110   struct cursor_cache_key* key = (struct cursor_cache_key*)target;
111
112   if ((cursor->cursor.type != key->type) ||
113       (gdk_cursor_get_display (GDK_CURSOR (cursor)) != key->display))
114     return 1; /* No match */
115   
116   /* Elements marked as pixmap must be named cursors 
117    * (since we don't store normal pixmap cursors 
118    */
119   if (key->type == GDK_CURSOR_IS_PIXMAP)
120     return strcmp (key->name, cursor->name);
121
122   return 0; /* Match */
123 }
124
125 /* Returns the cursor if there is a match, NULL if not
126  * For named cursors type shall be GDK_CURSOR_IS_PIXMAP
127  * For unnamed, typed cursors, name shall be NULL
128  */
129 static GdkX11Cursor*
130 find_in_cache (GdkDisplay    *display, 
131                GdkCursorType  type,
132                const char    *name)
133 {
134   GSList* res;
135   struct cursor_cache_key key;
136
137   key.display = display;
138   key.type = type;
139   key.name = name;
140
141   res = g_slist_find_custom (cursor_cache, &key, cache_compare_func);
142
143   if (res)
144     return (GdkX11Cursor *) res->data;
145
146   return NULL;
147 }
148
149 /* Called by gdk_x11_display_finalize to flush any cached cursors
150  * for a dead display.
151  */
152 void 
153 _gdk_x11_cursor_display_finalize (GdkDisplay *display)
154 {
155   GSList* item;
156   GSList** itemp; /* Pointer to the thing to fix when we delete an item */
157   item = cursor_cache;
158   itemp = &cursor_cache;
159   while (item)
160     {
161       GdkX11Cursor* cursor = (GdkX11Cursor*)(item->data);
162       if (gdk_cursor_get_display (GDK_CURSOR (cursor)) == display)
163         {
164           GSList* olditem;
165           gdk_cursor_unref ((GdkCursor*) cursor);
166           /* Remove this item from the list */
167           *(itemp) = item->next;
168           olditem = item;
169           item = g_slist_next (item);
170           g_slist_free_1 (olditem);
171         } 
172       else 
173         {
174           itemp = &(item->next);
175           item = g_slist_next (item);
176         }
177     }
178 }
179
180 /*** GdkX11Cursor ***/
181
182 G_DEFINE_TYPE (GdkX11Cursor, gdk_x11_cursor, GDK_TYPE_CURSOR)
183
184 static GdkPixbuf* gdk_x11_cursor_get_image (GdkCursor *cursor);
185
186 void
187 gdk_x11_cursor_finalize (GObject *object)
188 {
189   GdkX11Cursor *private = GDK_X11_CURSOR (object);
190   GdkDisplay *display;
191
192   display = gdk_cursor_get_display (GDK_CURSOR (object));
193   if (private->xcursor && !gdk_display_is_closed (display))
194     XFreeCursor (GDK_DISPLAY_XDISPLAY (display), private->xcursor);
195
196   g_free (private->name);
197
198   G_OBJECT_CLASS (gdk_x11_cursor_parent_class)->finalize (object);
199 }
200
201 static void
202 gdk_x11_cursor_class_init (GdkX11CursorClass *xcursor_class)
203 {
204   GdkCursorClass *cursor_class = GDK_CURSOR_CLASS (xcursor_class);
205   GObjectClass *object_class = G_OBJECT_CLASS (xcursor_class);
206
207   object_class->finalize = gdk_x11_cursor_finalize;
208
209   cursor_class->get_image = gdk_x11_cursor_get_image;
210 }
211
212 static void
213 gdk_x11_cursor_init (GdkX11Cursor *cursor)
214 {
215 }
216
217 static Cursor
218 get_blank_cursor (GdkDisplay *display)
219 {
220   GdkScreen *screen;
221   Pixmap pixmap;
222   XColor color;
223   Cursor cursor;
224   cairo_surface_t *surface;
225   cairo_t *cr;
226
227   screen = gdk_display_get_default_screen (display);
228   surface = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen), 1, 1);
229   /* Clear surface */
230   cr = cairo_create (surface);
231   cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
232   cairo_paint (cr);
233   cairo_destroy (cr);
234  
235   pixmap = cairo_xlib_surface_get_drawable (surface);
236
237   color.pixel = 0; 
238   color.red = color.blue = color.green = 0;
239
240   if (gdk_display_is_closed (display))
241     cursor = None;
242   else
243     cursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
244                                   pixmap, pixmap,
245                                   &color, &color, 1, 1);
246   cairo_surface_destroy (surface);
247
248   return cursor;
249 }
250
251 GdkCursor*
252 _gdk_x11_display_get_cursor_for_type (GdkDisplay    *display,
253                                       GdkCursorType  cursor_type)
254 {
255   GdkX11Cursor *private;
256   Cursor xcursor;
257
258   if (gdk_display_is_closed (display))
259     {
260       xcursor = None;
261     }
262   else
263     {
264       private = find_in_cache (display, cursor_type, NULL);
265
266       if (private)
267         {
268           /* Cache had it, add a ref for this user */
269           g_object_ref (private);
270
271           return (GdkCursor*) private;
272         }
273       else
274         {
275           if (cursor_type != GDK_BLANK_CURSOR)
276             xcursor = XCreateFontCursor (GDK_DISPLAY_XDISPLAY (display),
277                                          cursor_type);
278           else
279             xcursor = get_blank_cursor (display);
280        }
281     }
282
283   private = g_object_new (GDK_TYPE_X11_CURSOR,
284                           "cursor-type", GDK_CURSOR_IS_PIXMAP,
285                           "display", display,
286                           NULL);
287   private->xcursor = xcursor;
288   private->name = NULL;
289   private->serial = theme_serial;
290
291   if (xcursor != None)
292     add_to_cache (private);
293
294   return GDK_CURSOR (private);
295 }
296
297 /**
298  * gdk_x11_cursor_get_xdisplay:
299  * @cursor: a #GdkCursor.
300  * 
301  * Returns the display of a #GdkCursor.
302  * 
303  * Return value: an Xlib <type>Display*</type>.
304  **/
305 Display *
306 gdk_x11_cursor_get_xdisplay (GdkCursor *cursor)
307 {
308   g_return_val_if_fail (cursor != NULL, NULL);
309
310   return GDK_DISPLAY_XDISPLAY (gdk_cursor_get_display (cursor));
311 }
312
313 /**
314  * gdk_x11_cursor_get_xcursor:
315  * @cursor: a #GdkCursor.
316  * 
317  * Returns the X cursor belonging to a #GdkCursor.
318  * 
319  * Return value: an Xlib <type>Cursor</type>.
320  **/
321 Cursor
322 gdk_x11_cursor_get_xcursor (GdkCursor *cursor)
323 {
324   g_return_val_if_fail (cursor != NULL, None);
325
326   return ((GdkX11Cursor *)cursor)->xcursor;
327 }
328
329 #if defined(HAVE_XCURSOR) && defined(HAVE_XFIXES) && XFIXES_MAJOR >= 2
330
331 static GdkPixbuf*  
332 gdk_x11_cursor_get_image (GdkCursor *cursor)
333 {
334   Display *xdisplay;
335   GdkX11Cursor *private;
336   XcursorImages *images = NULL;
337   XcursorImage *image;
338   gint size;
339   gchar buf[32];
340   guchar *data, *p, tmp;
341   GdkPixbuf *pixbuf;
342   gchar *theme;
343   
344   private = GDK_X11_CURSOR (cursor);
345     
346   xdisplay = GDK_DISPLAY_XDISPLAY (gdk_cursor_get_display (cursor));
347
348   size = XcursorGetDefaultSize (xdisplay);
349   theme = XcursorGetTheme (xdisplay);
350
351   if (cursor->type == GDK_CURSOR_IS_PIXMAP)
352     {
353       if (private->name)
354         images = XcursorLibraryLoadImages (private->name, theme, size);
355     }
356   else
357     images = XcursorShapeLoadImages (cursor->type, theme, size);
358
359   if (!images)
360     return NULL;
361
362   image = images->images[0];
363
364   data = g_malloc (4 * image->width * image->height);
365   memcpy (data, image->pixels, 4 * image->width * image->height);
366
367   for (p = data; p < data + (4 * image->width * image->height); p += 4)
368     {
369       tmp = p[0];
370       p[0] = p[2];
371       p[2] = tmp;
372     }
373
374   pixbuf = gdk_pixbuf_new_from_data (data, GDK_COLORSPACE_RGB, TRUE,
375                                      8, image->width, image->height,
376                                      4 * image->width,
377                                      (GdkPixbufDestroyNotify)g_free, NULL);
378
379   if (private->name)
380     gdk_pixbuf_set_option (pixbuf, "name", private->name);
381   g_snprintf (buf, 32, "%d", image->xhot);
382   gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
383   g_snprintf (buf, 32, "%d", image->yhot);
384   gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
385
386   XcursorImagesDestroy (images);
387
388   return pixbuf;
389 }
390
391 void
392 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
393 {
394   Display *xdisplay;
395   GdkX11Cursor *private;
396   Cursor new_cursor = None;
397   GdkX11Display *display_x11;
398
399   private = (GdkX11Cursor *) cursor;
400   display_x11 = GDK_X11_DISPLAY (gdk_cursor_get_display (cursor));
401   xdisplay = GDK_DISPLAY_XDISPLAY (display_x11);
402
403   if (!display_x11->have_xfixes)
404     return;
405
406   if (private->serial == theme_serial)
407     return;
408
409   private->serial = theme_serial;
410
411   if (private->xcursor != None)
412     {
413       if (cursor->type == GDK_BLANK_CURSOR)
414         return;
415
416       if (cursor->type == GDK_CURSOR_IS_PIXMAP)
417         {
418           if (private->name)
419             new_cursor = XcursorLibraryLoadCursor (xdisplay, private->name);
420         }
421       else 
422         new_cursor = XcursorShapeLoadCursor (xdisplay, cursor->type);
423       
424       if (new_cursor != None)
425         {
426           XFixesChangeCursor (xdisplay, new_cursor, private->xcursor);
427           private->xcursor = new_cursor;
428         }
429     }
430 }
431
432 static void
433 update_cursor (gpointer data,
434                gpointer user_data)
435 {
436   GdkCursor *cursor;
437
438   cursor = (GdkCursor*)(data);
439
440   if (!cursor)
441     return;
442   
443   _gdk_x11_cursor_update_theme (cursor);
444 }
445
446 /**
447  * gdk_x11_display_set_cursor_theme:
448  * @display: a #GdkDisplay
449  * @theme: the name of the cursor theme to use, or %NULL to unset
450  *         a previously set value 
451  * @size: the cursor size to use, or 0 to keep the previous size
452  *
453  * Sets the cursor theme from which the images for cursor
454  * should be taken. 
455  * 
456  * If the windowing system supports it, existing cursors created 
457  * with gdk_cursor_new(), gdk_cursor_new_for_display() and 
458  * gdk_cursor_new_for_name() are updated to reflect the theme 
459  * change. Custom cursors constructed with
460  * gdk_cursor_new_from_pixbuf() will have to be handled
461  * by the application (GTK+ applications can learn about 
462  * cursor theme changes by listening for change notification
463  * for the corresponding #GtkSetting).
464  *
465  * Since: 2.8
466  */
467 void
468 gdk_x11_display_set_cursor_theme (GdkDisplay  *display,
469                                   const gchar *theme,
470                                   const gint   size)
471 {
472   GdkX11Display *display_x11;
473   Display *xdisplay;
474   gchar *old_theme;
475   gint old_size;
476
477   g_return_if_fail (GDK_IS_DISPLAY (display));
478
479   display_x11 = GDK_X11_DISPLAY (display);
480   xdisplay = GDK_DISPLAY_XDISPLAY (display);
481
482   old_theme = XcursorGetTheme (xdisplay);
483   old_size = XcursorGetDefaultSize (xdisplay);
484
485   if (old_size == size &&
486       (old_theme == theme ||
487        (old_theme && theme && strcmp (old_theme, theme) == 0)))
488     return;
489
490   theme_serial++;
491
492   XcursorSetTheme (xdisplay, theme);
493   if (size > 0)
494     XcursorSetDefaultSize (xdisplay, size);
495     
496   g_slist_foreach (cursor_cache, update_cursor, NULL);
497 }
498
499 #else
500
501 static GdkPixbuf*  
502 gdk_x11_cursor_get_image (GdkCursor *cursor)
503 {
504   return NULL;
505 }
506
507 void
508 gdk_x11_display_set_cursor_theme (GdkDisplay  *display,
509                                   const gchar *theme,
510                                   const gint   size)
511 {
512   g_return_if_fail (GDK_IS_DISPLAY (display));
513 }
514
515 void
516 _gdk_x11_cursor_update_theme (GdkCursor *cursor)
517 {
518   g_return_if_fail (cursor != NULL);
519 }
520
521 #endif
522
523 #ifdef HAVE_XCURSOR
524
525 static XcursorImage*
526 create_cursor_image (GdkPixbuf *pixbuf,
527                      gint       x,
528                      gint       y)
529 {
530   guint width, height;
531   XcursorImage *xcimage;
532   cairo_surface_t *surface;
533   cairo_t *cr;
534
535   width = gdk_pixbuf_get_width (pixbuf);
536   height = gdk_pixbuf_get_height (pixbuf);
537
538   xcimage = XcursorImageCreate (width, height);
539
540   xcimage->xhot = x;
541   xcimage->yhot = y;
542
543   surface = cairo_image_surface_create_for_data ((guchar *) xcimage->pixels,
544                                                  CAIRO_FORMAT_ARGB32,
545                                                  width,
546                                                  height,
547                                                  width * 4);
548
549   cr = cairo_create (surface);
550   cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
551   gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
552   cairo_paint (cr);
553   cairo_destroy (cr);
554
555   cairo_surface_destroy (surface);
556
557   return xcimage;
558 }
559
560 GdkCursor *
561 _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
562                                         GdkPixbuf  *pixbuf,
563                                         gint        x,
564                                         gint        y)
565 {
566   XcursorImage *xcimage;
567   Cursor xcursor;
568   GdkX11Cursor *private;
569   const char *option;
570   char *end;
571   gint64 value;
572
573   if (x == -1 && (option = gdk_pixbuf_get_option (pixbuf, "x_hot")))
574     {
575       errno = 0;
576       end = NULL;
577       value = g_ascii_strtoll (option, &end, 10);
578       if (errno == 0 &&
579           end != option &&
580           value >= 0 && value < G_MAXINT)
581         x = (gint) value;
582     }
583   if (y == -1 && (option = gdk_pixbuf_get_option (pixbuf, "y_hot")))
584     {
585       errno = 0;
586       end = NULL;
587       value = g_ascii_strtoll (option, &end, 10);
588       if (errno == 0 &&
589           end != option &&
590           value >= 0 && value < G_MAXINT)
591         y = (gint) value;
592     }
593
594   g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
595   g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
596
597   if (gdk_display_is_closed (display))
598     {
599       xcursor = None;
600     }
601   else
602     {
603       xcimage = create_cursor_image (pixbuf, x, y);
604       xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
605       XcursorImageDestroy (xcimage);
606     }
607
608   private = g_object_new (GDK_TYPE_X11_CURSOR, 
609                           "cursor-type", GDK_CURSOR_IS_PIXMAP,
610                           "display", display,
611                           NULL);
612   private->xcursor = xcursor;
613   private->name = NULL;
614   private->serial = theme_serial;
615
616   return GDK_CURSOR (private);
617 }
618
619 GdkCursor*
620 _gdk_x11_display_get_cursor_for_name (GdkDisplay  *display,
621                                       const gchar *name)
622 {
623   Cursor xcursor;
624   Display *xdisplay;
625   GdkX11Cursor *private;
626
627   if (gdk_display_is_closed (display))
628     {
629       xcursor = None;
630     }
631   else
632     {
633       private = find_in_cache (display, GDK_CURSOR_IS_PIXMAP, name);
634
635       if (private)
636         {
637           /* Cache had it, add a ref for this user */
638           g_object_ref (private);
639
640           return (GdkCursor*) private;
641         }
642
643       xdisplay = GDK_DISPLAY_XDISPLAY (display);
644       xcursor = XcursorLibraryLoadCursor (xdisplay, name);
645       if (xcursor == None)
646         return NULL;
647     }
648
649   private = g_object_new (GDK_TYPE_X11_CURSOR,
650                           "cursor-type", GDK_CURSOR_IS_PIXMAP,
651                           "display", display,
652                           NULL);
653   private->xcursor = xcursor;
654   private->name = g_strdup (name);
655   private->serial = theme_serial;
656
657   add_to_cache (private);
658
659   return GDK_CURSOR (private);
660 }
661
662 gboolean
663 _gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
664 {
665   return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
666 }
667
668 gboolean
669 _gdk_x11_display_supports_cursor_color (GdkDisplay *display)
670 {
671   return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
672 }
673
674 void
675 _gdk_x11_display_get_default_cursor_size (GdkDisplay *display,
676                                           guint      *width,
677                                           guint      *height)
678 {
679   *width = *height = XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
680 }
681
682 #else
683
684 static GdkCursor*
685 gdk_cursor_new_from_pixmap (GdkDisplay     *display,
686                             Pixmap          source_pixmap,
687                             Pixmap          mask_pixmap,
688                             const GdkColor *fg,
689                             const GdkColor *bg,
690                             gint            x,
691                             gint            y)
692 {
693   GdkX11Cursor *private;
694   Cursor xcursor;
695   XColor xfg, xbg;
696
697   g_return_val_if_fail (fg != NULL, NULL);
698   g_return_val_if_fail (bg != NULL, NULL);
699
700   xfg.pixel = fg->pixel;
701   xfg.red = fg->red;
702   xfg.blue = fg->blue;
703   xfg.green = fg->green;
704   xbg.pixel = bg->pixel;
705   xbg.red = bg->red;
706   xbg.blue = bg->blue;
707   xbg.green = bg->green;
708   
709   if (gdk_display_is_closed (display->closed))
710     xcursor = None;
711   else
712     xcursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
713                                    source_pixmap, mask_pixmap, &xfg, &xbg, x, y);
714   private = g_object_new (GDK_TYPE_X11_CURSOR,
715                           "cursor-type", GDK_CURSOR_IS_PIXMAP,
716                           "display", display,
717                           NULL);
718   private->xcursor = xcursor;
719   private->name = NULL;
720   private->serial = theme_serial;
721
722   return GDK_CURSOR (private);
723 }
724
725 GdkCursor *
726 _gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
727                                         GdkPixbuf  *pixbuf,
728                                         gint        x,
729                                         gint        y)
730 {
731   GdkCursor *cursor;
732   cairo_surface_t *pixmap, *mask;
733   guint width, height, n_channels, rowstride, data_stride, i, j;
734   guint8 *data, *mask_data, *pixels;
735   GdkColor fg = { 0, 0, 0, 0 };
736   GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
737   GdkScreen *screen;
738   cairo_surface_t *image;
739   cairo_t *cr;
740
741   width = gdk_pixbuf_get_width (pixbuf);
742   height = gdk_pixbuf_get_height (pixbuf);
743
744   g_return_val_if_fail (0 <= x && x < width, NULL);
745   g_return_val_if_fail (0 <= y && y < height, NULL);
746
747   n_channels = gdk_pixbuf_get_n_channels (pixbuf);
748   rowstride = gdk_pixbuf_get_rowstride (pixbuf);
749   pixels = gdk_pixbuf_get_pixels (pixbuf);
750
751   data_stride = 4 * ((width + 31) / 32);
752   data = g_new0 (guint8, data_stride * height);
753   mask_data = g_new0 (guint8, data_stride * height);
754
755   for (j = 0; j < height; j++)
756     {
757       guint8 *src = pixels + j * rowstride;
758       guint8 *d = data + data_stride * j;
759       guint8 *md = mask_data + data_stride * j;
760
761       for (i = 0; i < width; i++)
762         {
763           if (src[1] < 0x80)
764             *d |= 1 << (i % 8);
765
766           if (n_channels == 3 || src[3] >= 0x80)
767             *md |= 1 << (i % 8);
768
769           src += n_channels;
770           if (i % 8 == 7)
771             {
772               d++;
773               md++;
774             }
775         }
776     }
777
778   screen = gdk_display_get_default_screen (display);
779
780   pixmap = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
781                                                   width, height);
782   cr = cairo_create (pixmap);
783   image = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_A1,
784                                                width, height, data_stride);
785   cairo_set_source_surface (cr, image, 0, 0);
786   cairo_surface_destroy (image);
787   cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
788   cairo_paint (cr);
789   cairo_destroy (cr);
790
791   mask = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
792                                                 width, height);
793   cr = cairo_create (mask);
794   image = cairo_image_surface_create_for_data (mask_data, CAIRO_FORMAT_A1,
795                                                width, height, data_stride);
796   cairo_set_source_surface (cr, image, 0, 0);
797   cairo_surface_destroy (image);
798   cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
799   cairo_paint (cr);
800   cairo_destroy (cr);
801
802   cursor = gdk_cursor_new_from_pixmap (display,
803                                        cairo_xlib_surface_get_drawable (pixmap),
804                                        cairo_xlib_surface_get_drawable (mask),
805                                        &fg, &bg,
806                                        x, y);
807
808   cairo_surface_destroy (pixmap);
809   cairo_surface_destroy (mask);
810
811   g_free (data);
812   g_free (mask_data);
813
814   return cursor;
815 }
816
817 GdkCursor*
818 _gdk_x11_display_get_cursor_for_name (GdkDisplay  *display,
819                                       const gchar *name)
820 {
821   return NULL;
822 }
823
824 gboolean
825 _gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
826 {
827   return FALSE;
828 }
829
830 gboolean
831 _gdk_x11_display_supports_cursor_color (GdkDisplay *display)
832 {
833   return FALSE;
834 }
835
836 void
837 _gdk_x11_display_get_default_cursor_size (GdkDisplay *display)
838 {
839   /* no idea, really */
840   return 20;
841 }
842
843 #endif
844
845 void
846 _gdk_x11_display_get_maximal_cursor_size (GdkDisplay *display,
847                                           guint       *width,
848                                           guint       *height)
849 {
850   GdkScreen *screen;
851   GdkWindow *window;
852
853   g_return_if_fail (GDK_IS_DISPLAY (display));
854
855   screen = gdk_display_get_default_screen (display);
856   window = gdk_screen_get_root_window (screen);
857   XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display),
858                     GDK_WINDOW_XID (window),
859                     128, 128, width, height);
860 }