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