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