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