gdkcursor-x11.c 22.7 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19 20

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

27
#include "config.h"
28

29 30 31 32
/* needs to be first because any header might include gdk-pixbuf.h otherwise */
#define GDK_PIXBUF_ENABLE_BACKEND
#include <gdk-pixbuf/gdk-pixbuf.h>

33
#include "gdkcursor.h"
34
#include "gdkcursorprivate.h"
35 36 37
#include "gdkprivate-x11.h"
#include "gdkdisplay-x11.h"

Elliot Lee's avatar
Elliot Lee committed
38 39
#include <X11/Xlib.h>
#include <X11/cursorfont.h>
Matthias Clasen's avatar
Matthias Clasen committed
40 41 42
#ifdef HAVE_XCURSOR
#include <X11/Xcursor/Xcursor.h>
#endif
43 44 45 46
#ifdef HAVE_XFIXES
#include <X11/extensions/Xfixes.h>
#endif
#include <string.h>
47
#include <errno.h>
Owen Taylor's avatar
Started  
Owen Taylor committed
48

Benjamin Otte's avatar
Benjamin Otte committed
49 50 51 52 53 54
#define GDK_TYPE_X11_CURSOR              (gdk_x11_cursor_get_type ())
#define GDK_X11_CURSOR(object)           (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_X11_CURSOR, GdkX11Cursor))
#define GDK_X11_CURSOR_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_X11_CURSOR, GdkX11CursorClass))
#define GDK_IS_X11_CURSOR(object)        (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_X11_CURSOR))
#define GDK_IS_X11_CURSOR_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_X11_CURSOR))
#define GDK_X11_CURSOR_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_X11_CURSOR, GdkX11CursorClass))
55

Benjamin Otte's avatar
Benjamin Otte committed
56 57 58 59
typedef struct _GdkX11Cursor GdkX11Cursor;
typedef struct _GdkX11CursorClass GdkX11CursorClass;

struct _GdkX11Cursor
60 61
{
  GdkCursor cursor;
Benjamin Otte's avatar
Benjamin Otte committed
62

63 64 65 66 67
  Cursor xcursor;
  gchar *name;
  guint serial;
};

Benjamin Otte's avatar
Benjamin Otte committed
68 69 70 71
struct _GdkX11CursorClass
{
  GdkCursorClass cursor_class;
};
Matthias Clasen's avatar
Matthias Clasen committed
72

73
static guint theme_serial = 0;
Elliot Lee's avatar
Elliot Lee committed
74

75
/* cursor_cache holds a cache of non-pixmap cursors to avoid expensive 
Matthias Clasen's avatar
Matthias Clasen committed
76 77 78 79
 * libXcursor searches, cursors are added to it but only removed when
 * their display is closed. We make the assumption that since there are 
 * a small number of display's and a small number of cursor's that this 
 * list will stay small enough not to be a problem.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
 */
static GSList* cursor_cache = NULL;

struct cursor_cache_key
{
  GdkDisplay* display;
  GdkCursorType type;
  const char* name;
};

/* Caller should check if there is already a match first.
 * Cursor MUST be either a typed cursor or a pixmap with 
 * a non-NULL name.
 */
static void
Benjamin Otte's avatar
Benjamin Otte committed
95
add_to_cache (GdkX11Cursor* cursor)
96 97 98 99
{
  cursor_cache = g_slist_prepend (cursor_cache, cursor);

  /* Take a ref so that if the caller frees it we still have it */
100
  g_object_ref (cursor);
101 102 103 104 105 106 107 108
}

/* Returns 0 on a match
 */
static gint
cache_compare_func (gconstpointer listelem, 
                    gconstpointer target)
{
Benjamin Otte's avatar
Benjamin Otte committed
109
  GdkX11Cursor* cursor = (GdkX11Cursor*)listelem;
110 111 112
  struct cursor_cache_key* key = (struct cursor_cache_key*)target;

  if ((cursor->cursor.type != key->type) ||
113
      (gdk_cursor_get_display (GDK_CURSOR (cursor)) != key->display))
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    return 1; /* No match */
  
  /* Elements marked as pixmap must be named cursors 
   * (since we don't store normal pixmap cursors 
   */
  if (key->type == GDK_CURSOR_IS_PIXMAP)
    return strcmp (key->name, cursor->name);

  return 0; /* Match */
}

/* Returns the cursor if there is a match, NULL if not
 * For named cursors type shall be GDK_CURSOR_IS_PIXMAP
 * For unnamed, typed cursors, name shall be NULL
 */
Benjamin Otte's avatar
Benjamin Otte committed
129
static GdkX11Cursor*
130 131 132 133 134 135 136 137 138 139 140 141 142 143
find_in_cache (GdkDisplay    *display, 
               GdkCursorType  type,
               const char    *name)
{
  GSList* res;
  struct cursor_cache_key key;

  key.display = display;
  key.type = type;
  key.name = name;

  res = g_slist_find_custom (cursor_cache, &key, cache_compare_func);

  if (res)
Benjamin Otte's avatar
Benjamin Otte committed
144
    return (GdkX11Cursor *) res->data;
145 146 147 148

  return NULL;
}

149
/* Called by gdk_x11_display_finalize to flush any cached cursors
150 151 152 153 154 155 156 157 158 159 160
 * for a dead display.
 */
void 
_gdk_x11_cursor_display_finalize (GdkDisplay *display)
{
  GSList* item;
  GSList** itemp; /* Pointer to the thing to fix when we delete an item */
  item = cursor_cache;
  itemp = &cursor_cache;
  while (item)
    {
Benjamin Otte's avatar
Benjamin Otte committed
161
      GdkX11Cursor* cursor = (GdkX11Cursor*)(item->data);
162
      if (gdk_cursor_get_display (GDK_CURSOR (cursor)) == display)
163
        {
164
          GSList* olditem;
165
          gdk_cursor_unref ((GdkCursor*) cursor);
166 167 168 169 170
          /* Remove this item from the list */
          *(itemp) = item->next;
          olditem = item;
          item = g_slist_next (item);
          g_slist_free_1 (olditem);
171 172 173
        } 
      else 
        {
174 175 176
          itemp = &(item->next);
          item = g_slist_next (item);
        }
177 178 179
    }
}

Benjamin Otte's avatar
Benjamin Otte committed
180 181 182 183
/*** GdkX11Cursor ***/

G_DEFINE_TYPE (GdkX11Cursor, gdk_x11_cursor, GDK_TYPE_CURSOR)

184 185
static GdkPixbuf* gdk_x11_cursor_get_image (GdkCursor *cursor);

Benjamin Otte's avatar
Benjamin Otte committed
186 187 188 189
void
gdk_x11_cursor_finalize (GObject *object)
{
  GdkX11Cursor *private = GDK_X11_CURSOR (object);
190
  GdkDisplay *display;
Benjamin Otte's avatar
Benjamin Otte committed
191

192 193 194
  display = gdk_cursor_get_display (GDK_CURSOR (object));
  if (private->xcursor && !gdk_display_is_closed (display))
    XFreeCursor (GDK_DISPLAY_XDISPLAY (display), private->xcursor);
Benjamin Otte's avatar
Benjamin Otte committed
195 196 197 198 199 200 201

  g_free (private->name);

  G_OBJECT_CLASS (gdk_x11_cursor_parent_class)->finalize (object);
}

static void
202
gdk_x11_cursor_class_init (GdkX11CursorClass *xcursor_class)
Benjamin Otte's avatar
Benjamin Otte committed
203
{
204 205
  GdkCursorClass *cursor_class = GDK_CURSOR_CLASS (xcursor_class);
  GObjectClass *object_class = G_OBJECT_CLASS (xcursor_class);
Benjamin Otte's avatar
Benjamin Otte committed
206 207

  object_class->finalize = gdk_x11_cursor_finalize;
208 209

  cursor_class->get_image = gdk_x11_cursor_get_image;
Benjamin Otte's avatar
Benjamin Otte committed
210 211 212 213 214 215 216
}

static void
gdk_x11_cursor_init (GdkX11Cursor *cursor)
{
}

217 218 219 220
static Cursor
get_blank_cursor (GdkDisplay *display)
{
  GdkScreen *screen;
221
  Pixmap pixmap;
222 223
  XColor color;
  Cursor cursor;
224
  cairo_surface_t *surface;
225
  cairo_t *cr;
226 227

  screen = gdk_display_get_default_screen (display);
228 229 230
  surface = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen), 1, 1);
  /* Clear surface */
  cr = cairo_create (surface);
231 232 233
  cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
  cairo_paint (cr);
  cairo_destroy (cr);
234
 
235
  pixmap = cairo_xlib_surface_get_drawable (surface);
236 237 238

  color.pixel = 0; 
  color.red = color.blue = color.green = 0;
239 240

  if (gdk_display_is_closed (display))
241 242 243
    cursor = None;
  else
    cursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
244
                                  pixmap, pixmap,
245
                                  &color, &color, 1, 1);
246
  cairo_surface_destroy (surface);
247 248 249 250

  return cursor;
}

Elliot Lee's avatar
Elliot Lee committed
251
GdkCursor*
252 253
_gdk_x11_display_get_cursor_for_type (GdkDisplay    *display,
                                      GdkCursorType  cursor_type)
Elliot Lee's avatar
Elliot Lee committed
254
{
Benjamin Otte's avatar
Benjamin Otte committed
255
  GdkX11Cursor *private;
Elliot Lee's avatar
Elliot Lee committed
256 257
  Cursor xcursor;

258
  if (gdk_display_is_closed (display))
259 260
    {
      xcursor = None;
261 262
    }
  else
263 264 265 266 267 268
    {
      private = find_in_cache (display, cursor_type, NULL);

      if (private)
        {
          /* Cache had it, add a ref for this user */
269
          g_object_ref (private);
270

271
          return (GdkCursor*) private;
272 273
        }
      else
274
        {
275
          if (cursor_type != GDK_BLANK_CURSOR)
276 277
            xcursor = XCreateFontCursor (GDK_DISPLAY_XDISPLAY (display),
                                         cursor_type);
278 279
          else
            xcursor = get_blank_cursor (display);
280 281
       }
    }
282

283 284
  private = g_object_new (GDK_TYPE_X11_CURSOR,
                          "cursor-type", GDK_CURSOR_IS_PIXMAP,
285
                          "display", display,
286
                          NULL);
Elliot Lee's avatar
Elliot Lee committed
287
  private->xcursor = xcursor;
288
  private->name = NULL;
289 290
  private->serial = theme_serial;

291 292 293
  if (xcursor != None)
    add_to_cache (private);

294
  return GDK_CURSOR (private);
Elliot Lee's avatar
Elliot Lee committed
295 296
}

Matthias Clasen's avatar
Matthias Clasen committed
297 298 299 300 301 302 303 304
/**
 * gdk_x11_cursor_get_xdisplay:
 * @cursor: a #GdkCursor.
 * 
 * Returns the display of a #GdkCursor.
 * 
 * Return value: an Xlib <type>Display*</type>.
 **/
305 306 307
Display *
gdk_x11_cursor_get_xdisplay (GdkCursor *cursor)
{
308
  g_return_val_if_fail (cursor != NULL, NULL);
309

310
  return GDK_DISPLAY_XDISPLAY (gdk_cursor_get_display (cursor));
311 312
}

Matthias Clasen's avatar
Matthias Clasen committed
313 314 315 316 317 318 319 320
/**
 * gdk_x11_cursor_get_xcursor:
 * @cursor: a #GdkCursor.
 * 
 * Returns the X cursor belonging to a #GdkCursor.
 * 
 * Return value: an Xlib <type>Cursor</type>.
 **/
321 322 323
Cursor
gdk_x11_cursor_get_xcursor (GdkCursor *cursor)
{
324
  g_return_val_if_fail (cursor != NULL, None);
325

Benjamin Otte's avatar
Benjamin Otte committed
326
  return ((GdkX11Cursor *)cursor)->xcursor;
327
}
328

329 330
#if defined(HAVE_XCURSOR) && defined(HAVE_XFIXES) && XFIXES_MAJOR >= 2

331 332
static GdkPixbuf*  
gdk_x11_cursor_get_image (GdkCursor *cursor)
333 334
{
  Display *xdisplay;
Benjamin Otte's avatar
Benjamin Otte committed
335
  GdkX11Cursor *private;
336 337 338 339
  XcursorImages *images = NULL;
  XcursorImage *image;
  gint size;
  gchar buf[32];
Matthias Clasen's avatar
Matthias Clasen committed
340
  guchar *data, *p, tmp;
341 342
  GdkPixbuf *pixbuf;
  gchar *theme;
343
  
344
  private = GDK_X11_CURSOR (cursor);
345
    
346
  xdisplay = GDK_DISPLAY_XDISPLAY (gdk_cursor_get_display (cursor));
347 348 349 350 351 352 353

  size = XcursorGetDefaultSize (xdisplay);
  theme = XcursorGetTheme (xdisplay);

  if (cursor->type == GDK_CURSOR_IS_PIXMAP)
    {
      if (private->name)
354
        images = XcursorLibraryLoadImages (private->name, theme, size);
355
    }
356
  else
357 358 359 360
    images = XcursorShapeLoadImages (cursor->type, theme, size);

  if (!images)
    return NULL;
361

362 363 364 365 366
  image = images->images[0];

  data = g_malloc (4 * image->width * image->height);
  memcpy (data, image->pixels, 4 * image->width * image->height);

Matthias Clasen's avatar
Matthias Clasen committed
367 368 369 370 371 372 373
  for (p = data; p < data + (4 * image->width * image->height); p += 4)
    {
      tmp = p[0];
      p[0] = p[2];
      p[2] = tmp;
    }

374
  pixbuf = gdk_pixbuf_new_from_data (data, GDK_COLORSPACE_RGB, TRUE,
375 376 377
                                     8, image->width, image->height,
                                     4 * image->width,
                                     (GdkPixbufDestroyNotify)g_free, NULL);
378 379 380 381 382 383 384

  if (private->name)
    gdk_pixbuf_set_option (pixbuf, "name", private->name);
  g_snprintf (buf, 32, "%d", image->xhot);
  gdk_pixbuf_set_option (pixbuf, "x_hot", buf);
  g_snprintf (buf, 32, "%d", image->yhot);
  gdk_pixbuf_set_option (pixbuf, "y_hot", buf);
385

386 387 388 389 390
  XcursorImagesDestroy (images);

  return pixbuf;
}

391 392
void
_gdk_x11_cursor_update_theme (GdkCursor *cursor)
393 394
{
  Display *xdisplay;
Benjamin Otte's avatar
Benjamin Otte committed
395
  GdkX11Cursor *private;
396
  Cursor new_cursor = None;
397
  GdkX11Display *display_x11;
398

Benjamin Otte's avatar
Benjamin Otte committed
399
  private = (GdkX11Cursor *) cursor;
400
  display_x11 = GDK_X11_DISPLAY (gdk_cursor_get_display (cursor));
401
  xdisplay = GDK_DISPLAY_XDISPLAY (display_x11);
402 403 404 405

  if (!display_x11->have_xfixes)
    return;

406
  if (private->serial == theme_serial)
407 408
    return;

409
  private->serial = theme_serial;
410 411

  if (private->xcursor != None)
412
    {
Matthias Clasen's avatar
Matthias Clasen committed
413 414 415
      if (cursor->type == GDK_BLANK_CURSOR)
        return;

416
      if (cursor->type == GDK_CURSOR_IS_PIXMAP)
417 418 419 420
        {
          if (private->name)
            new_cursor = XcursorLibraryLoadCursor (xdisplay, private->name);
        }
421
      else 
422
        new_cursor = XcursorShapeLoadCursor (xdisplay, cursor->type);
423 424
      
      if (new_cursor != None)
425 426 427 428
        {
          XFixesChangeCursor (xdisplay, new_cursor, private->xcursor);
          private->xcursor = new_cursor;
        }
429
    }
430
}
431

432
static void
433
update_cursor (gpointer data,
434
               gpointer user_data)
435 436 437
{
  GdkCursor *cursor;

438
  cursor = (GdkCursor*)(data);
439 440 441 442 443 444 445

  if (!cursor)
    return;
  
  _gdk_x11_cursor_update_theme (cursor);
}

446 447 448
/**
 * gdk_x11_display_set_cursor_theme:
 * @display: a #GdkDisplay
Owen Taylor's avatar
Owen Taylor committed
449
 * @theme: the name of the cursor theme to use, or %NULL to unset
450 451
 *         a previously set value 
 * @size: the cursor size to use, or 0 to keep the previous size
452 453 454 455 456 457 458
 *
 * Sets the cursor theme from which the images for cursor
 * should be taken. 
 * 
 * If the windowing system supports it, existing cursors created 
 * with gdk_cursor_new(), gdk_cursor_new_for_display() and 
 * gdk_cursor_new_for_name() are updated to reflect the theme 
459 460
 * change. Custom cursors constructed with
 * gdk_cursor_new_from_pixbuf() will have to be handled
461 462 463 464 465 466 467 468
 * by the application (GTK+ applications can learn about 
 * cursor theme changes by listening for change notification
 * for the corresponding #GtkSetting).
 *
 * Since: 2.8
 */
void
gdk_x11_display_set_cursor_theme (GdkDisplay  *display,
469 470
                                  const gchar *theme,
                                  const gint   size)
471
{
472
  GdkX11Display *display_x11;
473 474 475
  Display *xdisplay;
  gchar *old_theme;
  gint old_size;
476

477 478
  g_return_if_fail (GDK_IS_DISPLAY (display));

479
  display_x11 = GDK_X11_DISPLAY (display);
480 481 482 483 484
  xdisplay = GDK_DISPLAY_XDISPLAY (display);

  old_theme = XcursorGetTheme (xdisplay);
  old_size = XcursorGetDefaultSize (xdisplay);

Owen Taylor's avatar
Owen Taylor committed
485 486 487
  if (old_size == size &&
      (old_theme == theme ||
       (old_theme && theme && strcmp (old_theme, theme) == 0)))
488 489
    return;

490 491
  theme_serial++;

492
  XcursorSetTheme (xdisplay, theme);
493 494
  if (size > 0)
    XcursorSetDefaultSize (xdisplay, size);
495
    
496
  g_slist_foreach (cursor_cache, update_cursor, NULL);
497 498 499 500
}

#else

501 502
static GdkPixbuf*  
gdk_x11_cursor_get_image (GdkCursor *cursor)
503 504 505 506
{
  return NULL;
}

507 508
void
gdk_x11_display_set_cursor_theme (GdkDisplay  *display,
509 510
                                  const gchar *theme,
                                  const gint   size)
511 512
{
  g_return_if_fail (GDK_IS_DISPLAY (display));
513
}
514

515 516 517
void
_gdk_x11_cursor_update_theme (GdkCursor *cursor)
{
518
  g_return_if_fail (cursor != NULL);
519 520
}

521
#endif
Matthias Clasen's avatar
Matthias Clasen committed
522 523 524 525

#ifdef HAVE_XCURSOR

static XcursorImage*
Michael Natterer's avatar
Michael Natterer committed
526
create_cursor_image (GdkPixbuf *pixbuf,
527 528
                     gint       x,
                     gint       y)
Matthias Clasen's avatar
Matthias Clasen committed
529
{
530
  guint width, height;
Matthias Clasen's avatar
Matthias Clasen committed
531
  XcursorImage *xcimage;
532 533
  cairo_surface_t *surface;
  cairo_t *cr;
Matthias Clasen's avatar
Matthias Clasen committed
534 535 536 537 538

  width = gdk_pixbuf_get_width (pixbuf);
  height = gdk_pixbuf_get_height (pixbuf);

  xcimage = XcursorImageCreate (width, height);
Michael Natterer's avatar
Michael Natterer committed
539

Matthias Clasen's avatar
Matthias Clasen committed
540 541 542
  xcimage->xhot = x;
  xcimage->yhot = y;

543 544 545 546 547
  surface = cairo_image_surface_create_for_data ((guchar *) xcimage->pixels,
                                                 CAIRO_FORMAT_ARGB32,
                                                 width,
                                                 height,
                                                 width * 4);
Matthias Clasen's avatar
Matthias Clasen committed
548

549 550 551 552 553
  cr = cairo_create (surface);
  cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
  gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
  cairo_paint (cr);
  cairo_destroy (cr);
Michael Natterer's avatar
Michael Natterer committed
554

555
  cairo_surface_destroy (surface);
Matthias Clasen's avatar
Matthias Clasen committed
556 557 558 559 560

  return xcimage;
}

GdkCursor *
561 562 563 564
_gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
                                        GdkPixbuf  *pixbuf,
                                        gint        x,
                                        gint        y)
Matthias Clasen's avatar
Matthias Clasen committed
565 566 567
{
  XcursorImage *xcimage;
  Cursor xcursor;
Benjamin Otte's avatar
Benjamin Otte committed
568
  GdkX11Cursor *private;
569 570 571
  const char *option;
  char *end;
  gint64 value;
Matthias Clasen's avatar
Matthias Clasen committed
572

573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
  if (x == -1 && (option = gdk_pixbuf_get_option (pixbuf, "x_hot")))
    {
      errno = 0;
      end = NULL;
      value = g_ascii_strtoll (option, &end, 10);
      if (errno == 0 &&
          end != option &&
          value >= 0 && value < G_MAXINT)
        x = (gint) value;
    }
  if (y == -1 && (option = gdk_pixbuf_get_option (pixbuf, "y_hot")))
    {
      errno = 0;
      end = NULL;
      value = g_ascii_strtoll (option, &end, 10);
      if (errno == 0 &&
          end != option &&
          value >= 0 && value < G_MAXINT)
        y = (gint) value;
    }

Matthias Clasen's avatar
Matthias Clasen committed
594 595 596
  g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL);
  g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL);

597
  if (gdk_display_is_closed (display))
598 599 600 601
    {
      xcursor = None;
    }
  else
Matthias Clasen's avatar
Matthias Clasen committed
602 603 604 605 606 607
    {
      xcimage = create_cursor_image (pixbuf, x, y);
      xcursor = XcursorImageLoadCursor (GDK_DISPLAY_XDISPLAY (display), xcimage);
      XcursorImageDestroy (xcimage);
    }

608 609
  private = g_object_new (GDK_TYPE_X11_CURSOR, 
                          "cursor-type", GDK_CURSOR_IS_PIXMAP,
610
                          "display", display,
611
                          NULL);
Matthias Clasen's avatar
Matthias Clasen committed
612
  private->xcursor = xcursor;
613
  private->name = NULL;
614 615
  private->serial = theme_serial;

616
  return GDK_CURSOR (private);
617 618
}

619 620 621
GdkCursor*
_gdk_x11_display_get_cursor_for_name (GdkDisplay  *display,
                                      const gchar *name)
622 623 624
{
  Cursor xcursor;
  Display *xdisplay;
Benjamin Otte's avatar
Benjamin Otte committed
625
  GdkX11Cursor *private;
626

627
  if (gdk_display_is_closed (display))
628 629 630 631
    {
      xcursor = None;
    }
  else
632
    {
633 634 635 636 637
      private = find_in_cache (display, GDK_CURSOR_IS_PIXMAP, name);

      if (private)
        {
          /* Cache had it, add a ref for this user */
638
          g_object_ref (private);
639 640 641 642

          return (GdkCursor*) private;
        }

643 644 645
      xdisplay = GDK_DISPLAY_XDISPLAY (display);
      xcursor = XcursorLibraryLoadCursor (xdisplay, name);
      if (xcursor == None)
646
        return NULL;
647 648
    }

649 650
  private = g_object_new (GDK_TYPE_X11_CURSOR,
                          "cursor-type", GDK_CURSOR_IS_PIXMAP,
651
                          "display", display,
652
                          NULL);
653 654
  private->xcursor = xcursor;
  private->name = g_strdup (name);
655 656
  private->serial = theme_serial;

657 658
  add_to_cache (private);

659
  return GDK_CURSOR (private);
Matthias Clasen's avatar
Matthias Clasen committed
660 661
}

662 663
gboolean
_gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
Matthias Clasen's avatar
Matthias Clasen committed
664 665 666 667
{
  return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
}

668 669
gboolean
_gdk_x11_display_supports_cursor_color (GdkDisplay *display)
Matthias Clasen's avatar
Matthias Clasen committed
670 671 672 673
{
  return XcursorSupportsARGB (GDK_DISPLAY_XDISPLAY (display));
}

674 675 676 677
void
_gdk_x11_display_get_default_cursor_size (GdkDisplay *display,
                                          guint      *width,
                                          guint      *height)
Matthias Clasen's avatar
Matthias Clasen committed
678
{
679
  *width = *height = XcursorGetDefaultSize (GDK_DISPLAY_XDISPLAY (display));
Matthias Clasen's avatar
Matthias Clasen committed
680 681 682 683
}

#else

684
static GdkCursor*
685 686
gdk_cursor_new_from_pixmap (GdkDisplay     *display,
                            Pixmap          source_pixmap,
687 688 689 690 691
                            Pixmap          mask_pixmap,
                            const GdkColor *fg,
                            const GdkColor *bg,
                            gint            x,
                            gint            y)
692
{
Benjamin Otte's avatar
Benjamin Otte committed
693
  GdkX11Cursor *private;
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
  Cursor xcursor;
  XColor xfg, xbg;

  g_return_val_if_fail (fg != NULL, NULL);
  g_return_val_if_fail (bg != NULL, NULL);

  xfg.pixel = fg->pixel;
  xfg.red = fg->red;
  xfg.blue = fg->blue;
  xfg.green = fg->green;
  xbg.pixel = bg->pixel;
  xbg.red = bg->red;
  xbg.blue = bg->blue;
  xbg.green = bg->green;
  
709
  if (gdk_display_is_closed (display->closed))
710 711 712
    xcursor = None;
  else
    xcursor = XCreatePixmapCursor (GDK_DISPLAY_XDISPLAY (display),
713
                                   source_pixmap, mask_pixmap, &xfg, &xbg, x, y);
714 715
  private = g_object_new (GDK_TYPE_X11_CURSOR,
                          "cursor-type", GDK_CURSOR_IS_PIXMAP,
716
                          "display", display,
717
                          NULL);
718 719 720 721
  private->xcursor = xcursor;
  private->name = NULL;
  private->serial = theme_serial;

722
  return GDK_CURSOR (private);
723 724
}

Matthias Clasen's avatar
Matthias Clasen committed
725
GdkCursor *
726 727 728 729
_gdk_x11_display_get_cursor_for_pixbuf (GdkDisplay *display,
                                        GdkPixbuf  *pixbuf,
                                        gint        x,
                                        gint        y)
Matthias Clasen's avatar
Matthias Clasen committed
730 731
{
  GdkCursor *cursor;
732
  cairo_surface_t *pixmap, *mask;
733
  guint width, height, n_channels, rowstride, data_stride, i, j;
Matthias Clasen's avatar
Matthias Clasen committed
734 735 736 737
  guint8 *data, *mask_data, *pixels;
  GdkColor fg = { 0, 0, 0, 0 };
  GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
  GdkScreen *screen;
738 739
  cairo_surface_t *image;
  cairo_t *cr;
Matthias Clasen's avatar
Matthias Clasen committed
740 741 742 743 744 745 746 747 748 749 750

  width = gdk_pixbuf_get_width (pixbuf);
  height = gdk_pixbuf_get_height (pixbuf);

  g_return_val_if_fail (0 <= x && x < width, NULL);
  g_return_val_if_fail (0 <= y && y < height, NULL);

  n_channels = gdk_pixbuf_get_n_channels (pixbuf);
  rowstride = gdk_pixbuf_get_rowstride (pixbuf);
  pixels = gdk_pixbuf_get_pixels (pixbuf);

751 752 753
  data_stride = 4 * ((width + 31) / 32);
  data = g_new0 (guint8, data_stride * height);
  mask_data = g_new0 (guint8, data_stride * height);
Matthias Clasen's avatar
Matthias Clasen committed
754 755 756 757

  for (j = 0; j < height; j++)
    {
      guint8 *src = pixels + j * rowstride;
758 759
      guint8 *d = data + data_stride * j;
      guint8 *md = mask_data + data_stride * j;
760

Matthias Clasen's avatar
Matthias Clasen committed
761
      for (i = 0; i < width; i++)
762 763 764 765 766 767 768 769 770 771 772 773 774 775
        {
          if (src[1] < 0x80)
            *d |= 1 << (i % 8);

          if (n_channels == 3 || src[3] >= 0x80)
            *md |= 1 << (i % 8);

          src += n_channels;
          if (i % 8 == 7)
            {
              d++;
              md++;
            }
        }
Matthias Clasen's avatar
Matthias Clasen committed
776
    }
777

Matthias Clasen's avatar
Matthias Clasen committed
778
  screen = gdk_display_get_default_screen (display);
779

780 781
  pixmap = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
                                                  width, height);
782
  cr = cairo_create (pixmap);
783 784 785 786 787 788 789
  image = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_A1,
                                               width, height, data_stride);
  cairo_set_source_surface (cr, image, 0, 0);
  cairo_surface_destroy (image);
  cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
  cairo_paint (cr);
  cairo_destroy (cr);
790 791 792

  mask = _gdk_x11_window_create_bitmap_surface (gdk_screen_get_root_window (screen),
                                                width, height);
793
  cr = cairo_create (mask);
794 795 796 797 798 799 800
  image = cairo_image_surface_create_for_data (mask_data, CAIRO_FORMAT_A1,
                                               width, height, data_stride);
  cairo_set_source_surface (cr, image, 0, 0);
  cairo_surface_destroy (image);
  cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
  cairo_paint (cr);
  cairo_destroy (cr);
801

802 803 804 805 806
  cursor = gdk_cursor_new_from_pixmap (display,
                                       cairo_xlib_surface_get_drawable (pixmap),
                                       cairo_xlib_surface_get_drawable (mask),
                                       &fg, &bg,
                                       x, y);
807

808 809
  cairo_surface_destroy (pixmap);
  cairo_surface_destroy (mask);
Matthias Clasen's avatar
Matthias Clasen committed
810 811 812

  g_free (data);
  g_free (mask_data);
813

Matthias Clasen's avatar
Matthias Clasen committed
814 815 816
  return cursor;
}

817 818 819
GdkCursor*
_gdk_x11_display_get_cursor_for_name (GdkDisplay  *display,
                                      const gchar *name)
820 821 822 823
{
  return NULL;
}

824 825
gboolean
_gdk_x11_display_supports_cursor_alpha (GdkDisplay *display)
Matthias Clasen's avatar
Matthias Clasen committed
826 827 828 829
{
  return FALSE;
}

830 831
gboolean
_gdk_x11_display_supports_cursor_color (GdkDisplay *display)
Matthias Clasen's avatar
Matthias Clasen committed
832 833 834 835
{
  return FALSE;
}

836 837
void
_gdk_x11_display_get_default_cursor_size (GdkDisplay *display)
Matthias Clasen's avatar
Matthias Clasen committed
838 839
{
  /* no idea, really */
840
  return 20;
Matthias Clasen's avatar
Matthias Clasen committed
841 842 843 844
}

#endif

845 846 847 848
void
_gdk_x11_display_get_maximal_cursor_size (GdkDisplay *display,
                                          guint       *width,
                                          guint       *height)
Matthias Clasen's avatar
Matthias Clasen committed
849 850 851 852
{
  GdkScreen *screen;
  GdkWindow *window;

853
  g_return_if_fail (GDK_IS_DISPLAY (display));
854

Matthias Clasen's avatar
Matthias Clasen committed
855 856
  screen = gdk_display_get_default_screen (display);
  window = gdk_screen_get_root_window (screen);
857 858 859
  XQueryBestCursor (GDK_DISPLAY_XDISPLAY (display),
                    GDK_WINDOW_XID (window),
                    128, 128, width, height);
Matthias Clasen's avatar
Matthias Clasen committed
860
}