Blame gdk/x11/gdkcursor-x11.c

Packit 98cdb6
/* GDK - The GIMP Drawing Kit
Packit 98cdb6
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
Packit 98cdb6
 *
Packit 98cdb6
 * This library is free software; you can redistribute it and/or
Packit 98cdb6
 * modify it under the terms of the GNU Lesser General Public
Packit 98cdb6
 * License as published by the Free Software Foundation; either
Packit 98cdb6
 * version 2 of the License, or (at your option) any later version.
Packit 98cdb6
 *
Packit 98cdb6
 * This library is distributed in the hope that it will be useful,
Packit 98cdb6
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 98cdb6
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 98cdb6
 * Lesser General Public License for more details.
Packit 98cdb6
 *
Packit 98cdb6
 * You should have received a copy of the GNU Lesser General Public
Packit 98cdb6
 * License along with this library; if not, write to the
Packit 98cdb6
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit 98cdb6
 * Boston, MA 02111-1307, USA.
Packit 98cdb6
 */
Packit 98cdb6
Packit 98cdb6
/*
Packit 98cdb6
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
Packit 98cdb6
 * file for a list of people on the GTK+ Team.  See the ChangeLog
Packit 98cdb6
 * files for a list of changes.  These files are distributed with
Packit 98cdb6
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
Packit 98cdb6
 */
Packit 98cdb6
Packit 98cdb6
#include "config.h"
Packit 98cdb6
Packit 98cdb6
#define GDK_PIXBUF_ENABLE_BACKEND
Packit 98cdb6
Packit 98cdb6
#include <X11/Xlib.h>
Packit 98cdb6
#include <X11/cursorfont.h>
Packit 98cdb6
#ifdef HAVE_XCURSOR
Packit 98cdb6
#include <X11/Xcursor/Xcursor.h>
Packit 98cdb6
#endif
Packit 98cdb6
#ifdef HAVE_XFIXES
Packit 98cdb6
#include <X11/extensions/Xfixes.h>
Packit 98cdb6
#endif
Packit 98cdb6
#include <string.h>
Packit 98cdb6
#include <errno.h>
Packit 98cdb6
Packit 98cdb6
#include "gdkprivate-x11.h"
Packit 98cdb6
#include "gdkcursor.h"
Packit 98cdb6
#include "gdkdisplay-x11.h"
Packit 98cdb6
#include "gdkpixmap-x11.h"
Packit 98cdb6
#include "gdkx.h"
Packit 98cdb6
#include <gdk/gdkpixmap.h>
Packit 98cdb6
#include <gdk-pixbuf/gdk-pixbuf.h>
Packit 98cdb6
#include "gdkalias.h"
Packit 98cdb6
Packit 98cdb6
static guint theme_serial = 0;
Packit 98cdb6
Packit 98cdb6
/* cursor_cache holds a cache of non-pixmap cursors to avoid expensive 
Packit 98cdb6
 * libXcursor searches, cursors are added to it but only removed when
Packit 98cdb6
 * their display is closed. We make the assumption that since there are 
Packit 98cdb6
 * a small number of display's and a small number of cursor's that this 
Packit 98cdb6
 * list will stay small enough not to be a problem.
Packit 98cdb6
 */
Packit 98cdb6
static GSList* cursor_cache = NULL;
Packit 98cdb6
Packit 98cdb6
struct cursor_cache_key
Packit 98cdb6
{
Packit 98cdb6
  GdkDisplay* display;
Packit 98cdb6
  GdkCursorType type;
Packit 98cdb6
  const char* name;
Packit 98cdb6
};
Packit 98cdb6
Packit 98cdb6
/* Caller should check if there is already a match first.
Packit 98cdb6
 * Cursor MUST be either a typed cursor or a pixmap with 
Packit 98cdb6
 * a non-NULL name.
Packit 98cdb6
 */
Packit 98cdb6
static void
Packit 98cdb6
add_to_cache (GdkCursorPrivate* cursor)
Packit 98cdb6
{
Packit 98cdb6
  cursor_cache = g_slist_prepend (cursor_cache, cursor);
Packit 98cdb6
Packit 98cdb6
  /* Take a ref so that if the caller frees it we still have it */
Packit 98cdb6
  gdk_cursor_ref ((GdkCursor*) cursor);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/* Returns 0 on a match
Packit 98cdb6
 */
Packit 98cdb6
static gint
Packit 98cdb6
cache_compare_func (gconstpointer listelem, 
Packit 98cdb6
                    gconstpointer target)
Packit 98cdb6
{
Packit 98cdb6
  GdkCursorPrivate* cursor = (GdkCursorPrivate*)listelem;
Packit 98cdb6
  struct cursor_cache_key* key = (struct cursor_cache_key*)target;
Packit 98cdb6
Packit 98cdb6
  if ((cursor->cursor.type != key->type) ||
Packit 98cdb6
      (cursor->display != key->display))
Packit 98cdb6
    return 1; /* No match */
Packit 98cdb6
  
Packit 98cdb6
  /* Elements marked as pixmap must be named cursors 
Packit 98cdb6
   * (since we don't store normal pixmap cursors 
Packit 98cdb6
   */
Packit 98cdb6
  if (key->type == GDK_CURSOR_IS_PIXMAP)
Packit 98cdb6
    return strcmp (key->name, cursor->name);
Packit 98cdb6
Packit 98cdb6
  return 0; /* Match */
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/* Returns the cursor if there is a match, NULL if not
Packit 98cdb6
 * For named cursors type shall be GDK_CURSOR_IS_PIXMAP
Packit 98cdb6
 * For unnamed, typed cursors, name shall be NULL
Packit 98cdb6
 */
Packit 98cdb6
static GdkCursorPrivate*
Packit 98cdb6
find_in_cache (GdkDisplay    *display, 
Packit 98cdb6
               GdkCursorType  type,
Packit 98cdb6
               const char    *name)
Packit 98cdb6
{
Packit 98cdb6
  GSList* res;
Packit 98cdb6
  struct cursor_cache_key key;
Packit 98cdb6
Packit 98cdb6
  key.display = display;
Packit 98cdb6
  key.type = type;
Packit 98cdb6
  key.name = name;
Packit 98cdb6
Packit 98cdb6
  res = g_slist_find_custom (cursor_cache, &key, cache_compare_func);
Packit 98cdb6
Packit 98cdb6
  if (res)
Packit 98cdb6
    return (GdkCursorPrivate *) res->data;
Packit 98cdb6
Packit 98cdb6
  return NULL;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/* Called by gdk_display_x11_finalize to flush any cached cursors
Packit 98cdb6
 * for a dead display.
Packit 98cdb6
 */
Packit 98cdb6
void 
Packit 98cdb6
_gdk_x11_cursor_display_finalize (GdkDisplay *display)
Packit 98cdb6
{
Packit 98cdb6
  GSList* item;
Packit 98cdb6
  GSList** itemp; /* Pointer to the thing to fix when we delete an item */
Packit 98cdb6
  item = cursor_cache;
Packit 98cdb6
  itemp = &cursor_cache;
Packit 98cdb6
  while (item)
Packit 98cdb6
    {
Packit 98cdb6
      GdkCursorPrivate* cursor = (GdkCursorPrivate*)(item->data);
Packit 98cdb6
      if (cursor->display == display)
Packit 98cdb6
        {
Packit 98cdb6
	  GSList* olditem;
Packit 98cdb6
          gdk_cursor_unref ((GdkCursor*) cursor);
Packit 98cdb6
	  /* Remove this item from the list */
Packit 98cdb6
	  *(itemp) = item->next;
Packit 98cdb6
	  olditem = item;
Packit 98cdb6
	  item = g_slist_next (item);
Packit 98cdb6
	  g_slist_free_1 (olditem);
Packit 98cdb6
        } 
Packit 98cdb6
      else 
Packit 98cdb6
        {
Packit 98cdb6
	  itemp = &(item->next);
Packit 98cdb6
	  item = g_slist_next (item);
Packit 98cdb6
	}
Packit 98cdb6
    }
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static Cursor
Packit 98cdb6
get_blank_cursor (GdkDisplay *display)
Packit 98cdb6
{
Packit 98cdb6
  GdkScreen *screen;
Packit 98cdb6
  GdkPixmap *pixmap;
Packit 98cdb6
  Pixmap source_pixmap;
Packit 98cdb6
  XColor color;
Packit 98cdb6
  Cursor cursor;
Packit 98cdb6
Packit 98cdb6
  screen = gdk_display_get_default_screen (display);
Packit 98cdb6
  pixmap = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen), 
Packit 98cdb6
					"\0\0\0\0\0\0\0\0", 1, 1);
Packit 98cdb6
 
Packit 98cdb6
  source_pixmap = GDK_PIXMAP_XID (pixmap);
Packit 98cdb6
Packit 98cdb6
  color.pixel = 0; 
Packit 98cdb6
  color.red = color.blue = color.green = 0;
Packit 98cdb6
  
Packit 98cdb6
  if (display->closed)
Packit 98cdb6
    cursor = None;
Packit 98cdb6
  else
Packit 98cdb6
    cursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
Packit 98cdb6
                                  source_pixmap, source_pixmap,
Packit 98cdb6
                                  &color, &color, 1, 1);
Packit 98cdb6
  g_object_unref (pixmap);
Packit 98cdb6
Packit 98cdb6
  return cursor;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_cursor_new_for_display:
Packit 98cdb6
 * @display: the #GdkDisplay for which the cursor will be created
Packit 98cdb6
 * @cursor_type: cursor to create
Packit 98cdb6
 * 
Packit 98cdb6
 * Creates a new cursor from the set of builtin cursors.
Packit 98cdb6
 * Some useful ones are:
Packit 98cdb6
 * <itemizedlist>
Packit 98cdb6
 * <listitem><para>
Packit 98cdb6
 *  <inlinegraphic format="PNG" fileref="right_ptr.png"></inlinegraphic> #GDK_RIGHT_PTR (right-facing arrow)
Packit 98cdb6
 * </para></listitem>
Packit 98cdb6
 * <listitem><para>
Packit 98cdb6
 *  <inlinegraphic format="PNG" fileref="crosshair.png"></inlinegraphic> #GDK_CROSSHAIR (crosshair)
Packit 98cdb6
 * </para></listitem>
Packit 98cdb6
 * <listitem><para>
Packit 98cdb6
 *  <inlinegraphic format="PNG" fileref="xterm.png"></inlinegraphic> #GDK_XTERM (I-beam)
Packit 98cdb6
 * </para></listitem>
Packit 98cdb6
 * <listitem><para>
Packit 98cdb6
 * <inlinegraphic format="PNG" fileref="watch.png"></inlinegraphic> #GDK_WATCH (busy)
Packit 98cdb6
 * </para></listitem>
Packit 98cdb6
 * <listitem><para>
Packit 98cdb6
 * <inlinegraphic format="PNG" fileref="fleur.png"></inlinegraphic> #GDK_FLEUR (for moving objects)
Packit 98cdb6
 * </para></listitem>
Packit 98cdb6
 * <listitem><para>
Packit 98cdb6
 * <inlinegraphic format="PNG" fileref="hand1.png"></inlinegraphic> #GDK_HAND1 (a right-pointing hand)
Packit 98cdb6
 * </para></listitem>
Packit 98cdb6
 * <listitem><para>
Packit 98cdb6
 * <inlinegraphic format="PNG" fileref="hand2.png"></inlinegraphic> #GDK_HAND2 (a left-pointing hand)
Packit 98cdb6
 * </para></listitem>
Packit 98cdb6
 * <listitem><para>
Packit 98cdb6
 * <inlinegraphic format="PNG" fileref="left_side.png"></inlinegraphic> #GDK_LEFT_SIDE (resize left side)
Packit 98cdb6
 * </para></listitem>
Packit 98cdb6
 * <listitem><para>
Packit 98cdb6
 * <inlinegraphic format="PNG" fileref="right_side.png"></inlinegraphic> #GDK_RIGHT_SIDE (resize right side)
Packit 98cdb6
 * </para></listitem>
Packit 98cdb6
 * <listitem><para>
Packit 98cdb6
 * <inlinegraphic format="PNG" fileref="top_left_corner.png"></inlinegraphic> #GDK_TOP_LEFT_CORNER (resize northwest corner)
Packit 98cdb6
 * </para></listitem>
Packit 98cdb6
 * <listitem><para>
Packit 98cdb6
 * <inlinegraphic format="PNG" fileref="top_right_corner.png"></inlinegraphic> #GDK_TOP_RIGHT_CORNER (resize northeast corner)
Packit 98cdb6
 * </para></listitem>
Packit 98cdb6
 * <listitem><para>
Packit 98cdb6
 * <inlinegraphic format="PNG" fileref="bottom_left_corner.png"></inlinegraphic> #GDK_BOTTOM_LEFT_CORNER (resize southwest corner)
Packit 98cdb6
 * </para></listitem>
Packit 98cdb6
 * <listitem><para>
Packit 98cdb6
 * <inlinegraphic format="PNG" fileref="bottom_right_corner.png"></inlinegraphic> #GDK_BOTTOM_RIGHT_CORNER (resize southeast corner)
Packit 98cdb6
 * </para></listitem>
Packit 98cdb6
 * <listitem><para>
Packit 98cdb6
 * <inlinegraphic format="PNG" fileref="top_side.png"></inlinegraphic> #GDK_TOP_SIDE (resize top side)
Packit 98cdb6
 * </para></listitem>
Packit 98cdb6
 * <listitem><para>
Packit 98cdb6
 * <inlinegraphic format="PNG" fileref="bottom_side.png"></inlinegraphic> #GDK_BOTTOM_SIDE (resize bottom side)
Packit 98cdb6
 * </para></listitem>
Packit 98cdb6
 * <listitem><para>
Packit 98cdb6
 * <inlinegraphic format="PNG" fileref="sb_h_double_arrow.png"></inlinegraphic> #GDK_SB_H_DOUBLE_ARROW (move vertical splitter)
Packit 98cdb6
 * </para></listitem>
Packit 98cdb6
 * <listitem><para>
Packit 98cdb6
 * <inlinegraphic format="PNG" fileref="sb_v_double_arrow.png"></inlinegraphic> #GDK_SB_V_DOUBLE_ARROW (move horizontal splitter)
Packit 98cdb6
 * </para></listitem>
Packit 98cdb6
 * <listitem><para>
Packit 98cdb6
 * #GDK_BLANK_CURSOR (Blank cursor). Since 2.16
Packit 98cdb6
 * </para></listitem>
Packit 98cdb6
 * </itemizedlist>
Packit 98cdb6
 *
Packit 98cdb6
 * Return value: a new #GdkCursor
Packit 98cdb6
 *
Packit 98cdb6
 * Since: 2.2
Packit 98cdb6
 **/
Packit 98cdb6
GdkCursor*
Packit 98cdb6
gdk_cursor_new_for_display (GdkDisplay    *display,
Packit 98cdb6
			    GdkCursorType  cursor_type)
Packit 98cdb6
{
Packit 98cdb6
  GdkCursorPrivate *private;
Packit 98cdb6
  GdkCursor *cursor;
Packit 98cdb6
  Cursor xcursor;
Packit 98cdb6
Packit 98cdb6
  g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
Packit 98cdb6
Packit 98cdb6
  if (display->closed)
Packit 98cdb6
    {
Packit 98cdb6
      xcursor = None;
Packit 98cdb6
    } 
Packit 98cdb6
  else 
Packit 98cdb6
    {
Packit 98cdb6
      private = find_in_cache (display, cursor_type, NULL);
Packit 98cdb6
Packit 98cdb6
      if (private)
Packit 98cdb6
        {
Packit 98cdb6
          /* Cache had it, add a ref for this user */
Packit 98cdb6
          gdk_cursor_ref ((GdkCursor*) private);
Packit 98cdb6
       
Packit 98cdb6
          return (GdkCursor*) private;
Packit 98cdb6
        } 
Packit 98cdb6
      else 
Packit 98cdb6
        {
Packit 98cdb6
	  if (cursor_type != GDK_BLANK_CURSOR)
Packit 98cdb6
            xcursor = XCreateFontCursor (GDK_DISPLAY_XDISPLAY (display),
Packit 98cdb6
                                         cursor_type);
Packit 98cdb6
	  else
Packit 98cdb6
	    xcursor = get_blank_cursor (display);
Packit 98cdb6
       }
Packit 98cdb6
    }
Packit 98cdb6
  
Packit 98cdb6
  private = g_new (GdkCursorPrivate, 1);
Packit 98cdb6
  private->display = display;
Packit 98cdb6
  private->xcursor = xcursor;
Packit 98cdb6
  private->name = NULL;
Packit 98cdb6
  private->serial = theme_serial;
Packit 98cdb6
Packit 98cdb6
  cursor = (GdkCursor *) private;
Packit 98cdb6
  cursor->type = cursor_type;
Packit 98cdb6
  cursor->ref_count = 1;
Packit 98cdb6
  
Packit 98cdb6
  if (xcursor != None)
Packit 98cdb6
    add_to_cache (private);
Packit 98cdb6
Packit 98cdb6
  return cursor;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_cursor_new_from_pixmap:
Packit 98cdb6
 * @source: the pixmap specifying the cursor.
Packit 98cdb6
 * @mask: the pixmap specifying the mask, which must be the same size as 
Packit 98cdb6
 *    @source.
Packit 98cdb6
 * @fg: the foreground color, used for the bits in the source which are 1.
Packit 98cdb6
 *    The color does not have to be allocated first. 
Packit 98cdb6
 * @bg: the background color, used for the bits in the source which are 0.
Packit 98cdb6
 *    The color does not have to be allocated first.
Packit 98cdb6
 * @x: the horizontal offset of the 'hotspot' of the cursor. 
Packit 98cdb6
 * @y: the vertical offset of the 'hotspot' of the cursor.
Packit 98cdb6
 * 
Packit 98cdb6
 * Creates a new cursor from a given pixmap and mask. Both the pixmap and mask
Packit 98cdb6
 * must have a depth of 1 (i.e. each pixel has only 2 values - on or off).
Packit 98cdb6
 * The standard cursor size is 16 by 16 pixels. You can create a bitmap 
Packit 98cdb6
 * from inline data as in the below example.
Packit 98cdb6
 * 
Packit 98cdb6
 * <example><title>Creating a custom cursor</title>
Packit 98cdb6
 * <programlisting>
Packit 98cdb6
 * /* This data is in X bitmap format, and can be created with the 'bitmap'
Packit 98cdb6
 *    utility. */
Packit 98cdb6
 * #define cursor1_width 16
Packit 98cdb6
 * #define cursor1_height 16
Packit 98cdb6
 * static unsigned char cursor1_bits[] = {
Packit 98cdb6
 *   0x80, 0x01, 0x40, 0x02, 0x20, 0x04, 0x10, 0x08, 0x08, 0x10, 0x04, 0x20,
Packit 98cdb6
 *   0x82, 0x41, 0x41, 0x82, 0x41, 0x82, 0x82, 0x41, 0x04, 0x20, 0x08, 0x10,
Packit 98cdb6
 *   0x10, 0x08, 0x20, 0x04, 0x40, 0x02, 0x80, 0x01};
Packit 98cdb6
 *  
Packit 98cdb6
 * static unsigned char cursor1mask_bits[] = {
Packit 98cdb6
 *   0x80, 0x01, 0xc0, 0x03, 0x60, 0x06, 0x30, 0x0c, 0x18, 0x18, 0x8c, 0x31,
Packit 98cdb6
 *   0xc6, 0x63, 0x63, 0xc6, 0x63, 0xc6, 0xc6, 0x63, 0x8c, 0x31, 0x18, 0x18,
Packit 98cdb6
 *   0x30, 0x0c, 0x60, 0x06, 0xc0, 0x03, 0x80, 0x01};
Packit 98cdb6
 *  
Packit 98cdb6
 *  
Packit 98cdb6
 *  GdkCursor *cursor;
Packit 98cdb6
 *  GdkPixmap *source, *mask;
Packit 98cdb6
 *  GdkColor fg = { 0, 65535, 0, 0 }; /* Red. */
Packit 98cdb6
 *  GdkColor bg = { 0, 0, 0, 65535 }; /* Blue. */
Packit 98cdb6
 *  
Packit 98cdb6
 *  
Packit 98cdb6
 *  source = gdk_bitmap_create_from_data (NULL, cursor1_bits,
Packit 98cdb6
 *                                        cursor1_width, cursor1_height);
Packit 98cdb6
 *  mask = gdk_bitmap_create_from_data (NULL, cursor1mask_bits,
Packit 98cdb6
 *                                      cursor1_width, cursor1_height);
Packit 98cdb6
 *  cursor = gdk_cursor_new_from_pixmap (source, mask, &fg, &bg, 8, 8);
Packit 98cdb6
 *  g_object_unref (source);
Packit 98cdb6
 *  g_object_unref (mask);
Packit 98cdb6
 *  
Packit 98cdb6
 *  
Packit 98cdb6
 *  gdk_window_set_cursor (widget->window, cursor);
Packit 98cdb6
 * </programlisting>
Packit 98cdb6
 * </example>
Packit 98cdb6
 *
Packit 98cdb6
 * Return value: a new #GdkCursor.
Packit 98cdb6
 **/
Packit 98cdb6
GdkCursor*
Packit 98cdb6
gdk_cursor_new_from_pixmap (GdkPixmap      *source,
Packit 98cdb6
			    GdkPixmap      *mask,
Packit 98cdb6
			    const GdkColor *fg,
Packit 98cdb6
			    const GdkColor *bg,
Packit 98cdb6
			    gint            x,
Packit 98cdb6
			    gint            y)
Packit 98cdb6
{
Packit 98cdb6
  GdkCursorPrivate *private;
Packit 98cdb6
  GdkCursor *cursor;
Packit 98cdb6
  Pixmap source_pixmap, mask_pixmap;
Packit 98cdb6
  Cursor xcursor;
Packit 98cdb6
  XColor xfg, xbg;
Packit 98cdb6
  GdkDisplay *display;
Packit 98cdb6
Packit 98cdb6
  g_return_val_if_fail (GDK_IS_PIXMAP (source), NULL);
Packit 98cdb6
  g_return_val_if_fail (GDK_IS_PIXMAP (mask), NULL);
Packit 98cdb6
  g_return_val_if_fail (fg != NULL, NULL);
Packit 98cdb6
  g_return_val_if_fail (bg != NULL, NULL);
Packit 98cdb6
Packit 98cdb6
  source_pixmap = GDK_PIXMAP_XID (source);
Packit 98cdb6
  mask_pixmap   = GDK_PIXMAP_XID (mask);
Packit 98cdb6
  display = GDK_PIXMAP_DISPLAY (source);
Packit 98cdb6
Packit 98cdb6
  xfg.pixel = fg->pixel;
Packit 98cdb6
  xfg.red = fg->red;
Packit 98cdb6
  xfg.blue = fg->blue;
Packit 98cdb6
  xfg.green = fg->green;
Packit 98cdb6
  xbg.pixel = bg->pixel;
Packit 98cdb6
  xbg.red = bg->red;
Packit 98cdb6
  xbg.blue = bg->blue;
Packit 98cdb6
  xbg.green = bg->green;
Packit 98cdb6
  
Packit 98cdb6
  if (display->closed)
Packit 98cdb6
    xcursor = None;
Packit 98cdb6
  else
Packit 98cdb6
    xcursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
Packit 98cdb6
				   source_pixmap, mask_pixmap, &xfg, &xbg, x, y);
Packit 98cdb6
  private = g_new (GdkCursorPrivate, 1);
Packit 98cdb6
  private->display = display;
Packit 98cdb6
  private->xcursor = xcursor;
Packit 98cdb6
  private->name = NULL;
Packit 98cdb6
  private->serial = theme_serial;
Packit 98cdb6
Packit 98cdb6
  cursor = (GdkCursor *) private;
Packit 98cdb6
  cursor->type = GDK_CURSOR_IS_PIXMAP;
Packit 98cdb6
  cursor->ref_count = 1;
Packit 98cdb6
  
Packit 98cdb6
  return cursor;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
void
Packit 98cdb6
_gdk_cursor_destroy (GdkCursor *cursor)
Packit 98cdb6
{
Packit 98cdb6
  GdkCursorPrivate *private;
Packit 98cdb6
Packit 98cdb6
  g_return_if_fail (cursor != NULL);
Packit 98cdb6
  g_return_if_fail (cursor->ref_count == 0);
Packit 98cdb6
Packit 98cdb6
  private = (GdkCursorPrivate *) cursor;
Packit 98cdb6
  if (!private->display->closed && private->xcursor)
Packit 98cdb6
    XFreeCursor (GDK_DISPLAY_XDISPLAY (private->display), private->xcursor);
Packit 98cdb6
Packit 98cdb6
  g_free (private->name);
Packit 98cdb6
  g_free (private);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_x11_cursor_get_xdisplay:
Packit 98cdb6
 * @cursor: a #GdkCursor.
Packit 98cdb6
 * 
Packit 98cdb6
 * Returns the display of a #GdkCursor.
Packit 98cdb6
 * 
Packit 98cdb6
 * Return value: an Xlib <type>Display*</type>.
Packit 98cdb6
 **/
Packit 98cdb6
Display *
Packit 98cdb6
gdk_x11_cursor_get_xdisplay (GdkCursor *cursor)
Packit 98cdb6
{
Packit 98cdb6
  g_return_val_if_fail (cursor != NULL, NULL);
Packit 98cdb6
Packit 98cdb6
  return GDK_DISPLAY_XDISPLAY(((GdkCursorPrivate *)cursor)->display);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_x11_cursor_get_xcursor:
Packit 98cdb6
 * @cursor: a #GdkCursor.
Packit 98cdb6
 * 
Packit 98cdb6
 * Returns the X cursor belonging to a #GdkCursor.
Packit 98cdb6
 * 
Packit 98cdb6
 * Return value: an Xlib <type>Cursor</type>.
Packit 98cdb6
 **/
Packit 98cdb6
Cursor
Packit 98cdb6
gdk_x11_cursor_get_xcursor (GdkCursor *cursor)
Packit 98cdb6
{
Packit 98cdb6
  g_return_val_if_fail (cursor != NULL, None);
Packit 98cdb6
Packit 98cdb6
  return ((GdkCursorPrivate *)cursor)->xcursor;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/** 
Packit 98cdb6
 * gdk_cursor_get_display:
Packit 98cdb6
 * @cursor: a #GdkCursor.
Packit 98cdb6
 *
Packit 98cdb6
 * Returns the display on which the #GdkCursor is defined.
Packit 98cdb6
 *
Packit 98cdb6
 * Returns: the #GdkDisplay associated to @cursor
Packit 98cdb6
 *
Packit 98cdb6
 * Since: 2.2
Packit 98cdb6
 */
Packit 98cdb6
Packit 98cdb6
GdkDisplay *
Packit 98cdb6
gdk_cursor_get_display (GdkCursor *cursor)
Packit 98cdb6
{
Packit 98cdb6
  g_return_val_if_fail (cursor != NULL, NULL);
Packit 98cdb6
Packit 98cdb6
  return ((GdkCursorPrivate *)cursor)->display;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
#if defined(HAVE_XCURSOR) && defined(HAVE_XFIXES) && XFIXES_MAJOR >= 2
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_cursor_get_image:
Packit 98cdb6
 * @cursor: a #GdkCursor
Packit 98cdb6
 *
Packit 98cdb6
 * Returns a #GdkPixbuf with the image used to display the cursor.
Packit 98cdb6
 *
Packit 98cdb6
 * Note that depending on the capabilities of the windowing system and 
Packit 98cdb6
 * on the cursor, GDK may not be able to obtain the image data. In this 
Packit 98cdb6
 * case, %NULL is returned.
Packit 98cdb6
 *
Packit 98cdb6
 * Returns: a #GdkPixbuf representing @cursor, or %NULL
Packit 98cdb6
 *
Packit 98cdb6
 * Since: 2.8
Packit 98cdb6
 */
Packit 98cdb6
GdkPixbuf*  
Packit 98cdb6
gdk_cursor_get_image (GdkCursor *cursor)
Packit 98cdb6
{
Packit 98cdb6
  Display *xdisplay;
Packit 98cdb6
  GdkCursorPrivate *private;
Packit 98cdb6
  XcursorImages *images = NULL;
Packit 98cdb6
  XcursorImage *image;
Packit 98cdb6
  gint size;
Packit 98cdb6
  gchar buf[32];
Packit 98cdb6
  guchar *data, *p, tmp;
Packit 98cdb6
  GdkPixbuf *pixbuf;
Packit 98cdb6
  gchar *theme;
Packit 98cdb6
  
Packit 98cdb6
  g_return_val_if_fail (cursor != NULL, NULL);
Packit 98cdb6
Packit 98cdb6
  private = (GdkCursorPrivate *) cursor;
Packit 98cdb6
    
Packit 98cdb6
  xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
Packit 98cdb6
Packit 98cdb6
  size = XcursorGetDefaultSize (xdisplay);
Packit 98cdb6
  theme = XcursorGetTheme (xdisplay);
Packit 98cdb6
Packit 98cdb6
  if (cursor->type == GDK_CURSOR_IS_PIXMAP)
Packit 98cdb6
    {
Packit 98cdb6
      if (private->name)
Packit 98cdb6
	images = XcursorLibraryLoadImages (private->name, theme, size);
Packit 98cdb6
    }
Packit 98cdb6
  else 
Packit 98cdb6
    images = XcursorShapeLoadImages (cursor->type, theme, size);
Packit 98cdb6
Packit 98cdb6
  if (!images)
Packit 98cdb6
    return NULL;
Packit 98cdb6
  
Packit 98cdb6
  image = images->images[0];
Packit 98cdb6
Packit 98cdb6
  data = g_malloc (4 * image->width * image->height);
Packit 98cdb6
  memcpy (data, image->pixels, 4 * image->width * image->height);
Packit 98cdb6
Packit 98cdb6
  for (p = data; p < data + (4 * image->width * image->height); p += 4)
Packit 98cdb6
    {
Packit 98cdb6
      tmp = p[0];
Packit 98cdb6
      p[0] = p[2];
Packit 98cdb6
      p[2] = tmp;
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  pixbuf = gdk_pixbuf_new_from_data (data, GDK_COLORSPACE_RGB, TRUE,
Packit 98cdb6
				     8, image->width, image->height,
Packit 98cdb6
				     4 * image->width, 
Packit 98cdb6
				     (GdkPixbufDestroyNotify)g_free, NULL);
Packit 98cdb6
Packit 98cdb6
  if (private->name)
Packit 98cdb6
    gdk_pixbuf_set_option (pixbuf, "name", private->name);
Packit 98cdb6
  g_snprintf (buf, 32, "%d", image->xhot);
Packit 98cdb6
  gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
Packit 98cdb6
  g_snprintf (buf, 32, "%d", image->yhot);
Packit 98cdb6
  gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
Packit 98cdb6
Packit 98cdb6
  XcursorImagesDestroy (images);
Packit 98cdb6
Packit 98cdb6
  return pixbuf;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
void
Packit 98cdb6
_gdk_x11_cursor_update_theme (GdkCursor *cursor)
Packit 98cdb6
{
Packit 98cdb6
  Display *xdisplay;
Packit 98cdb6
  GdkCursorPrivate *private;
Packit 98cdb6
  Cursor new_cursor = None;
Packit 98cdb6
  GdkDisplayX11 *display_x11;
Packit 98cdb6
Packit 98cdb6
  private = (GdkCursorPrivate *) cursor;
Packit 98cdb6
  xdisplay = GDK_DISPLAY_XDISPLAY (private->display);
Packit 98cdb6
  display_x11 = GDK_DISPLAY_X11 (private->display);
Packit 98cdb6
Packit 98cdb6
  if (!display_x11->have_xfixes)
Packit 98cdb6
    return;
Packit 98cdb6
Packit 98cdb6
  if (private->serial == theme_serial)
Packit 98cdb6
    return;
Packit 98cdb6
Packit 98cdb6
  private->serial = theme_serial;
Packit 98cdb6
Packit 98cdb6
  if (private->xcursor != None)
Packit 98cdb6
    {
Packit 98cdb6
      if (cursor->type == GDK_BLANK_CURSOR)
Packit 98cdb6
        return;
Packit 98cdb6
Packit 98cdb6
      if (cursor->type == GDK_CURSOR_IS_PIXMAP)
Packit 98cdb6
	{
Packit 98cdb6
	  if (private->name)
Packit 98cdb6
	    new_cursor = XcursorLibraryLoadCursor (xdisplay, private->name);
Packit 98cdb6
	}
Packit 98cdb6
      else 
Packit 98cdb6
	new_cursor = XcursorShapeLoadCursor (xdisplay, cursor->type);
Packit 98cdb6
      
Packit 98cdb6
      if (new_cursor != None)
Packit 98cdb6
	{
Packit 98cdb6
	  XFixesChangeCursor (xdisplay, new_cursor, private->xcursor);
Packit 98cdb6
 	  private->xcursor = new_cursor;
Packit 98cdb6
	}
Packit 98cdb6
    }
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
update_cursor (gpointer data,
Packit 98cdb6
	       gpointer user_data)
Packit 98cdb6
{
Packit 98cdb6
  GdkCursor *cursor;
Packit 98cdb6
Packit 98cdb6
  cursor = (GdkCursor*)(data);
Packit 98cdb6
Packit 98cdb6
  if (!cursor)
Packit 98cdb6
    return;
Packit 98cdb6
  
Packit 98cdb6
  _gdk_x11_cursor_update_theme (cursor);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_x11_display_set_cursor_theme:
Packit 98cdb6
 * @display: a #GdkDisplay
Packit 98cdb6
 * @theme: the name of the cursor theme to use, or %NULL to unset
Packit 98cdb6
 *         a previously set value 
Packit 98cdb6
 * @size: the cursor size to use, or 0 to keep the previous size
Packit 98cdb6
 *
Packit 98cdb6
 * Sets the cursor theme from which the images for cursor
Packit 98cdb6
 * should be taken. 
Packit 98cdb6
 * 
Packit 98cdb6
 * If the windowing system supports it, existing cursors created 
Packit 98cdb6
 * with gdk_cursor_new(), gdk_cursor_new_for_display() and 
Packit 98cdb6
 * gdk_cursor_new_for_name() are updated to reflect the theme 
Packit 98cdb6
 * change. Custom cursors constructed with gdk_cursor_new_from_pixmap() 
Packit 98cdb6
 * or gdk_cursor_new_from_pixbuf() will have to be handled
Packit 98cdb6
 * by the application (GTK+ applications can learn about 
Packit 98cdb6
 * cursor theme changes by listening for change notification
Packit 98cdb6
 * for the corresponding #GtkSetting).
Packit 98cdb6
 *
Packit 98cdb6
 * Since: 2.8
Packit 98cdb6
 */
Packit 98cdb6
void
Packit 98cdb6
gdk_x11_display_set_cursor_theme (GdkDisplay  *display,
Packit 98cdb6
				  const gchar *theme,
Packit 98cdb6
				  const gint   size)
Packit 98cdb6
{
Packit 98cdb6
  GdkDisplayX11 *display_x11;
Packit 98cdb6
  Display *xdisplay;
Packit 98cdb6
  gchar *old_theme;
Packit 98cdb6
  gint old_size;
Packit 98cdb6
Packit 98cdb6
  g_return_if_fail (GDK_IS_DISPLAY (display));
Packit 98cdb6
Packit 98cdb6
  display_x11 = GDK_DISPLAY_X11 (display);
Packit 98cdb6
  xdisplay = GDK_DISPLAY_XDISPLAY (display);
Packit 98cdb6
Packit 98cdb6
  old_theme = XcursorGetTheme (xdisplay);
Packit 98cdb6
  old_size = XcursorGetDefaultSize (xdisplay);
Packit 98cdb6
Packit 98cdb6
  if (old_size == size &&
Packit 98cdb6
      (old_theme == theme ||
Packit 98cdb6
       (old_theme && theme && strcmp (old_theme, theme) == 0)))
Packit 98cdb6
    return;
Packit 98cdb6
Packit 98cdb6
  theme_serial++;
Packit 98cdb6
Packit 98cdb6
  XcursorSetTheme (xdisplay, theme);
Packit 98cdb6
  if (size > 0)
Packit 98cdb6
    XcursorSetDefaultSize (xdisplay, size);
Packit 98cdb6
    
Packit 98cdb6
  g_slist_foreach (cursor_cache, update_cursor, NULL);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
#else
Packit 98cdb6
Packit 98cdb6
GdkPixbuf*  
Packit 98cdb6
gdk_cursor_get_image (GdkCursor *cursor)
Packit 98cdb6
{
Packit 98cdb6
  g_return_val_if_fail (cursor != NULL, NULL);
Packit 98cdb6
  
Packit 98cdb6
  return NULL;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
void
Packit 98cdb6
gdk_x11_display_set_cursor_theme (GdkDisplay  *display,
Packit 98cdb6
				  const gchar *theme,
Packit 98cdb6
				  const gint   size)
Packit 98cdb6
{
Packit 98cdb6
  g_return_if_fail (GDK_IS_DISPLAY (display));
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
void
Packit 98cdb6
_gdk_x11_cursor_update_theme (GdkCursor *cursor)
Packit 98cdb6
{
Packit 98cdb6
  g_return_if_fail (cursor != NULL);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
#endif
Packit 98cdb6
Packit 98cdb6
#ifdef HAVE_XCURSOR
Packit 98cdb6
Packit 98cdb6
static XcursorImage*
Packit 98cdb6
create_cursor_image (GdkPixbuf *pixbuf,
Packit 98cdb6
		     gint       x,
Packit 98cdb6
		     gint       y)
Packit 98cdb6
{
Packit 98cdb6
  guint width, height, rowstride, n_channels;
Packit 98cdb6
  guchar *pixels, *src;
Packit 98cdb6
  XcursorImage *xcimage;
Packit 98cdb6
  XcursorPixel *dest;
Packit 98cdb6
Packit 98cdb6
  width = gdk_pixbuf_get_width (pixbuf);
Packit 98cdb6
  height = gdk_pixbuf_get_height (pixbuf);
Packit 98cdb6
Packit 98cdb6
  n_channels = gdk_pixbuf_get_n_channels (pixbuf);
Packit 98cdb6
  rowstride = gdk_pixbuf_get_rowstride (pixbuf);
Packit 98cdb6
  pixels = gdk_pixbuf_get_pixels (pixbuf);
Packit 98cdb6
Packit 98cdb6
  xcimage = XcursorImageCreate (width, height);
Packit 98cdb6
Packit 98cdb6
  xcimage->xhot = x;
Packit 98cdb6
  xcimage->yhot = y;
Packit 98cdb6
Packit 98cdb6
  dest = xcimage->pixels;
Packit 98cdb6
Packit 98cdb6
  if (n_channels == 3)
Packit 98cdb6
    {
Packit 98cdb6
      gint i, j;
Packit 98cdb6
Packit 98cdb6
      for (j = 0; j < height; j++)
Packit 98cdb6
        {
Packit 98cdb6
          src = pixels + j * rowstride;
Packit 98cdb6
          for (i = 0; i < width; i++)
Packit 98cdb6
            {
Packit 98cdb6
              *dest = (0xff << 24) | (src[0] << 16) | (src[1] << 8) | src[2];
Packit 98cdb6
            }
Packit 98cdb6
Packit 98cdb6
	  src += n_channels;
Packit 98cdb6
	  dest++;
Packit 98cdb6
	}
Packit 98cdb6
    }
Packit 98cdb6
  else
Packit 98cdb6
    {
Packit 98cdb6
      _gdk_x11_convert_to_format (pixels, rowstride,
Packit 98cdb6
                                  (guchar *) dest, 4 * width,
Packit 98cdb6
                                  GDK_X11_FORMAT_ARGB,
Packit 98cdb6
                                  (G_BYTE_ORDER == G_BIG_ENDIAN) ?
Packit 98cdb6
                                  GDK_MSB_FIRST : GDK_LSB_FIRST,
Packit 98cdb6
                                  width, height);
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  return xcimage;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_cursor_new_from_pixbuf:
Packit 98cdb6
 * @display: the #GdkDisplay for which the cursor will be created
Packit 98cdb6
 * @pixbuf: the #GdkPixbuf containing the cursor image
Packit 98cdb6
 * @x: the horizontal offset of the 'hotspot' of the cursor. 
Packit 98cdb6
 * @y: the vertical offset of the 'hotspot' of the cursor.
Packit 98cdb6
 *
Packit 98cdb6
 * Creates a new cursor from a pixbuf. 
Packit 98cdb6
 *
Packit 98cdb6
 * Not all GDK backends support RGBA cursors. If they are not 
Packit 98cdb6
 * supported, a monochrome approximation will be displayed. 
Packit 98cdb6
 * The functions gdk_display_supports_cursor_alpha() and 
Packit 98cdb6
 * gdk_display_supports_cursor_color() can be used to determine
Packit 98cdb6
 * whether RGBA cursors are supported; 
Packit 98cdb6
 * gdk_display_get_default_cursor_size() and 
Packit 98cdb6
 * gdk_display_get_maximal_cursor_size() give information about 
Packit 98cdb6
 * cursor sizes.
Packit 98cdb6
 *
Packit 98cdb6
 * If @x or @y are <literal>-1</literal>, the pixbuf must have
Packit 98cdb6
 * options named "x_hot" and "y_hot", resp., containing
Packit 98cdb6
 * integer values between %0 and the width resp. height of
Packit 98cdb6
 * the pixbuf. (Since: 3.0)
Packit 98cdb6
 *
Packit 98cdb6
 * On the X backend, support for RGBA cursors requires a
Packit 98cdb6
 * sufficently new version of the X Render extension. 
Packit 98cdb6
 *
Packit 98cdb6
 * Returns: a new #GdkCursor.
Packit 98cdb6
 * 
Packit 98cdb6
 * Since: 2.4
Packit 98cdb6
 */
Packit 98cdb6
GdkCursor *
Packit 98cdb6
gdk_cursor_new_from_pixbuf (GdkDisplay *display, 
Packit 98cdb6
			    GdkPixbuf  *pixbuf,
Packit 98cdb6
			    gint        x,
Packit 98cdb6
			    gint        y)
Packit 98cdb6
{
Packit 98cdb6
  XcursorImage *xcimage;
Packit 98cdb6
  Cursor xcursor;
Packit 98cdb6
  GdkCursorPrivate *private;
Packit 98cdb6
  GdkCursor *cursor;
Packit 98cdb6
  const char *option;
Packit 98cdb6
  char *end;
Packit 98cdb6
  gint64 value;
Packit 98cdb6
Packit 98cdb6
  g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
Packit 98cdb6
  g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
Packit 98cdb6
Packit 98cdb6
  if (x == -1 && (option = gdk_pixbuf_get_option (pixbuf, "x_hot")))
Packit 98cdb6
    {
Packit 98cdb6
      errno = 0;
Packit 98cdb6
      end = NULL;
Packit 98cdb6
      value = g_ascii_strtoll (option, &end, 10);
Packit 98cdb6
      if (errno == 0 &&
Packit 98cdb6
          end != option &&
Packit 98cdb6
          value >= 0 && value < G_MAXINT)
Packit 98cdb6
        x = (gint) value;
Packit 98cdb6
    }
Packit 98cdb6
  if (y == -1 && (option = gdk_pixbuf_get_option (pixbuf, "y_hot")))
Packit 98cdb6
    {
Packit 98cdb6
      errno = 0;
Packit 98cdb6
      end = NULL;
Packit 98cdb6
      value = g_ascii_strtoll (option, &end, 10);
Packit 98cdb6
      if (errno == 0 &&
Packit 98cdb6
          end != option &&
Packit 98cdb6
          value >= 0 && value < G_MAXINT)
Packit 98cdb6
        y = (gint) value;
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
Packit 98cdb6
  g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);
Packit 98cdb6
Packit 98cdb6
  if (display->closed)
Packit 98cdb6
    xcursor = None;
Packit 98cdb6
  else 
Packit 98cdb6
    {
Packit 98cdb6
      xcimage = create_cursor_image (pixbuf, x, y);
Packit 98cdb6
      xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
Packit 98cdb6
      XcursorImageDestroy (xcimage);
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  private = g_new (GdkCursorPrivate, 1);
Packit 98cdb6
  private->display = display;
Packit 98cdb6
  private->xcursor = xcursor;
Packit 98cdb6
  private->name = NULL;
Packit 98cdb6
  private->serial = theme_serial;
Packit 98cdb6
Packit 98cdb6
  cursor = (GdkCursor *) private;
Packit 98cdb6
  cursor->type = GDK_CURSOR_IS_PIXMAP;
Packit 98cdb6
  cursor->ref_count = 1;
Packit 98cdb6
  
Packit 98cdb6
  return cursor;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_cursor_new_from_name:
Packit 98cdb6
 * @display: the #GdkDisplay for which the cursor will be created
Packit 98cdb6
 * @name: the name of the cursor
Packit 98cdb6
 *
Packit 98cdb6
 * Creates a new cursor by looking up @name in the current cursor
Packit 98cdb6
 * theme. 
Packit 98cdb6
 * 
Packit 98cdb6
 * Returns: a new #GdkCursor, or %NULL if there is no cursor with 
Packit 98cdb6
 *   the given name 
Packit 98cdb6
 *
Packit 98cdb6
 * Since: 2.8
Packit 98cdb6
 */
Packit 98cdb6
GdkCursor*  
Packit 98cdb6
gdk_cursor_new_from_name (GdkDisplay  *display,
Packit 98cdb6
			  const gchar *name)
Packit 98cdb6
{
Packit 98cdb6
  Cursor xcursor;
Packit 98cdb6
  Display *xdisplay;
Packit 98cdb6
  GdkCursorPrivate *private;
Packit 98cdb6
  GdkCursor *cursor;
Packit 98cdb6
Packit 98cdb6
  g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
Packit 98cdb6
Packit 98cdb6
  if (display->closed)
Packit 98cdb6
    xcursor = None;
Packit 98cdb6
  else 
Packit 98cdb6
    {
Packit 98cdb6
      private = find_in_cache (display, GDK_CURSOR_IS_PIXMAP, name);
Packit 98cdb6
Packit 98cdb6
      if (private)
Packit 98cdb6
        {
Packit 98cdb6
          /* Cache had it, add a ref for this user */
Packit 98cdb6
          gdk_cursor_ref ((GdkCursor*) private);
Packit 98cdb6
Packit 98cdb6
          return (GdkCursor*) private;
Packit 98cdb6
        }
Packit 98cdb6
Packit 98cdb6
      xdisplay = GDK_DISPLAY_XDISPLAY (display);
Packit 98cdb6
      xcursor = XcursorLibraryLoadCursor (xdisplay, name);
Packit 98cdb6
      if (xcursor == None)
Packit 98cdb6
	return NULL;
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  private = g_new (GdkCursorPrivate, 1);
Packit 98cdb6
  private->display = display;
Packit 98cdb6
  private->xcursor = xcursor;
Packit 98cdb6
  private->name = g_strdup (name);
Packit 98cdb6
  private->serial = theme_serial;
Packit 98cdb6
Packit 98cdb6
  cursor = (GdkCursor *) private;
Packit 98cdb6
  cursor->type = GDK_CURSOR_IS_PIXMAP;
Packit 98cdb6
  cursor->ref_count = 1;
Packit 98cdb6
  add_to_cache (private);
Packit 98cdb6
Packit 98cdb6
  return cursor;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_display_supports_cursor_alpha:
Packit 98cdb6
 * @display: a #GdkDisplay
Packit 98cdb6
 *
Packit 98cdb6
 * Returns %TRUE if cursors can use an 8bit alpha channel 
Packit 98cdb6
 * on @display. Otherwise, cursors are restricted to bilevel 
Packit 98cdb6
 * alpha (i.e. a mask).
Packit 98cdb6
 *
Packit 98cdb6
 * Returns: whether cursors can have alpha channels.
Packit 98cdb6
 *
Packit 98cdb6
 * Since: 2.4
Packit 98cdb6
 */
Packit 98cdb6
gboolean 
Packit 98cdb6
gdk_display_supports_cursor_alpha (GdkDisplay *display)
Packit 98cdb6
{
Packit 98cdb6
  g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
Packit 98cdb6
Packit 98cdb6
  return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_display_supports_cursor_color:
Packit 98cdb6
 * @display: a #GdkDisplay
Packit 98cdb6
 *
Packit 98cdb6
 * Returns %TRUE if multicolored cursors are supported
Packit 98cdb6
 * on @display. Otherwise, cursors have only a forground
Packit 98cdb6
 * and a background color.
Packit 98cdb6
 *
Packit 98cdb6
 * Returns: whether cursors can have multiple colors.
Packit 98cdb6
 *
Packit 98cdb6
 * Since: 2.4
Packit 98cdb6
 */
Packit 98cdb6
gboolean 
Packit 98cdb6
gdk_display_supports_cursor_color (GdkDisplay *display)
Packit 98cdb6
{
Packit 98cdb6
  g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
Packit 98cdb6
Packit 98cdb6
  return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_display_get_default_cursor_size:
Packit 98cdb6
 * @display: a #GdkDisplay
Packit 98cdb6
 *
Packit 98cdb6
 * Returns the default size to use for cursors on @display.
Packit 98cdb6
 *
Packit 98cdb6
 * Returns: the default cursor size.
Packit 98cdb6
 *
Packit 98cdb6
 * Since: 2.4
Packit 98cdb6
 */
Packit 98cdb6
guint     
Packit 98cdb6
gdk_display_get_default_cursor_size (GdkDisplay *display)
Packit 98cdb6
{
Packit 98cdb6
  g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
Packit 98cdb6
Packit 98cdb6
  return XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
#else
Packit 98cdb6
Packit 98cdb6
GdkCursor *
Packit 98cdb6
gdk_cursor_new_from_pixbuf (GdkDisplay *display, 
Packit 98cdb6
			    GdkPixbuf  *pixbuf,
Packit 98cdb6
			    gint        x,
Packit 98cdb6
			    gint        y)
Packit 98cdb6
{
Packit 98cdb6
  GdkCursor *cursor;
Packit 98cdb6
  GdkPixmap *pixmap, *mask;
Packit 98cdb6
  guint width, height, n_channels, rowstride, i, j;
Packit 98cdb6
  guint8 *data, *mask_data, *pixels;
Packit 98cdb6
  GdkColor fg = { 0, 0, 0, 0 };
Packit 98cdb6
  GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
Packit 98cdb6
  GdkScreen *screen;
Packit 98cdb6
Packit 98cdb6
  g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
Packit 98cdb6
  g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
Packit 98cdb6
Packit 98cdb6
  width = gdk_pixbuf_get_width (pixbuf);
Packit 98cdb6
  height = gdk_pixbuf_get_height (pixbuf);
Packit 98cdb6
Packit 98cdb6
  g_return_val_if_fail (0 <= x && x < width, NULL);
Packit 98cdb6
  g_return_val_if_fail (0 <= y && y < height, NULL);
Packit 98cdb6
Packit 98cdb6
  n_channels = gdk_pixbuf_get_n_channels (pixbuf);
Packit 98cdb6
  rowstride = gdk_pixbuf_get_rowstride (pixbuf);
Packit 98cdb6
  pixels = gdk_pixbuf_get_pixels (pixbuf);
Packit 98cdb6
Packit 98cdb6
  data = g_new0 (guint8, (width + 7) / 8 * height);
Packit 98cdb6
  mask_data = g_new0 (guint8, (width + 7) / 8 * height);
Packit 98cdb6
Packit 98cdb6
  for (j = 0; j < height; j++)
Packit 98cdb6
    {
Packit 98cdb6
      guint8 *src = pixels + j * rowstride;
Packit 98cdb6
      guint8 *d = data + (width + 7) / 8 * j;
Packit 98cdb6
      guint8 *md = mask_data + (width + 7) / 8 * j;
Packit 98cdb6
	
Packit 98cdb6
      for (i = 0; i < width; i++)
Packit 98cdb6
	{
Packit 98cdb6
	  if (src[1] < 0x80)
Packit 98cdb6
	    *d |= 1 << (i % 8);
Packit 98cdb6
	  
Packit 98cdb6
	  if (n_channels == 3 || src[3] >= 0x80)
Packit 98cdb6
	    *md |= 1 << (i % 8);
Packit 98cdb6
	  
Packit 98cdb6
	  src += n_channels;
Packit 98cdb6
	  if (i % 8 == 7)
Packit 98cdb6
	    {
Packit 98cdb6
	      d++; 
Packit 98cdb6
	      md++;
Packit 98cdb6
	    }
Packit 98cdb6
	}
Packit 98cdb6
    }
Packit 98cdb6
      
Packit 98cdb6
  screen = gdk_display_get_default_screen (display);
Packit 98cdb6
  pixmap = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen), 
Packit 98cdb6
					data, width, height);
Packit 98cdb6
 
Packit 98cdb6
  mask = gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
Packit 98cdb6
				      mask_data, width, height);
Packit 98cdb6
   
Packit 98cdb6
  cursor = gdk_cursor_new_from_pixmap (pixmap, mask, &fg, &bg, x, y);
Packit 98cdb6
   
Packit 98cdb6
  g_object_unref (pixmap);
Packit 98cdb6
  g_object_unref (mask);
Packit 98cdb6
Packit 98cdb6
  g_free (data);
Packit 98cdb6
  g_free (mask_data);
Packit 98cdb6
  
Packit 98cdb6
  return cursor;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
GdkCursor*  
Packit 98cdb6
gdk_cursor_new_from_name (GdkDisplay  *display,
Packit 98cdb6
			  const gchar *name)
Packit 98cdb6
{
Packit 98cdb6
  g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
Packit 98cdb6
Packit 98cdb6
  return NULL;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
gboolean 
Packit 98cdb6
gdk_display_supports_cursor_alpha (GdkDisplay    *display)
Packit 98cdb6
{
Packit 98cdb6
  g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
Packit 98cdb6
Packit 98cdb6
  return FALSE;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
gboolean 
Packit 98cdb6
gdk_display_supports_cursor_color (GdkDisplay    *display)
Packit 98cdb6
{
Packit 98cdb6
  g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
Packit 98cdb6
Packit 98cdb6
  return FALSE;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
guint     
Packit 98cdb6
gdk_display_get_default_cursor_size (GdkDisplay    *display)
Packit 98cdb6
{
Packit 98cdb6
  g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
Packit 98cdb6
  
Packit 98cdb6
  /* no idea, really */
Packit 98cdb6
  return 20; 
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
#endif
Packit 98cdb6
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_display_get_maximal_cursor_size:
Packit 98cdb6
 * @display: a #GdkDisplay
Packit 98cdb6
 * @width: (out): the return location for the maximal cursor width
Packit 98cdb6
 * @height: (out): the return location for the maximal cursor height
Packit 98cdb6
 *
Packit 98cdb6
 * Gets the maximal size to use for cursors on @display.
Packit 98cdb6
 *
Packit 98cdb6
 * Since: 2.4
Packit 98cdb6
 */
Packit 98cdb6
void     
Packit 98cdb6
gdk_display_get_maximal_cursor_size (GdkDisplay *display,
Packit 98cdb6
				     guint       *width,
Packit 98cdb6
				     guint       *height)
Packit 98cdb6
{
Packit 98cdb6
  GdkScreen *screen;
Packit 98cdb6
  GdkWindow *window;
Packit 98cdb6
Packit 98cdb6
  g_return_if_fail (GDK_IS_DISPLAY (display));
Packit 98cdb6
  
Packit 98cdb6
  screen = gdk_display_get_default_screen (display);
Packit 98cdb6
  window = gdk_screen_get_root_window (screen);
Packit 98cdb6
  XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display), 
Packit 98cdb6
		    GDK_WINDOW_XWINDOW (window), 
Packit 98cdb6
		    128, 128, width, height);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
#define __GDK_CURSOR_X11_C__
Packit 98cdb6
#include "gdkaliasdef.c"