GitLab repository storage has been migrated to hashed layout. Please contact Infrastructure team if you notice any issues with repositories or hooks.

gdkproperty-x11.c 11 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/. 
 */

Elliot Lee's avatar
Elliot Lee committed
27 28 29
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <string.h>
Owen Taylor's avatar
Started  
Owen Taylor committed
30

31
#include "gdk.h"          /* For gdk_error_trap_push/pop() */
32
#include "gdkx.h"
Owen Taylor's avatar
Started  
Owen Taylor committed
33
#include "gdkproperty.h"
Elliot Lee's avatar
Elliot Lee committed
34 35
#include "gdkprivate.h"

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
static GPtrArray *virtual_atom_array;
static GHashTable *virtual_atom_hash;
static GHashTable *atom_to_virtual;
static GHashTable *atom_from_virtual;

static gchar *XAtomsStrings[] = {
  /* These are all the standard predefined X atoms */
  "NONE",
  "PRIMARY",
  "SECONDARY",
  "ARC",
  "ATOM",
  "BITMAP",
  "CARDINAL",
  "COLORMAP",
  "CURSOR",
  "CUT_BUFFER0",
  "CUT_BUFFER1",
  "CUT_BUFFER2",
  "CUT_BUFFER3",
  "CUT_BUFFER4",
  "CUT_BUFFER5",
  "CUT_BUFFER6",
  "CUT_BUFFER7",
  "DRAWABLE",
  "FONT",
  "INTEGER",
  "PIXMAP",
  "POINT",
  "RECTANGLE",
  "RESOURCE_MANAGER",
  "RGB_COLOR_MAP",
  "RGB_BEST_MAP",
  "RGB_BLUE_MAP",
  "RGB_DEFAULT_MAP",
  "RGB_GRAY_MAP",
  "RGB_GREEN_MAP",
  "RGB_RED_MAP",
  "STRING",
  "VISUALID",
  "WINDOW",
  "WM_COMMAND",
  "WM_HINTS",
  "WM_CLIENT_MACHINE",
  "WM_ICON_NAME",
  "WM_ICON_SIZE",
  "WM_NAME",
  "WM_NORMAL_HINTS",
  "WM_SIZE_HINTS",
  "WM_ZOOM_HINTS",
  "MIN_SPACE",
  "NORM_SPACE",
  "MAX_SPACE",  "END_SPACE",
  "SUPERSCRIPT_X",
  "SUPERSCRIPT_Y",
  "SUBSCRIPT_X",
  "SUBSCRIPT_Y",
  "UNDERLINE_POSITION",
  "UNDERLINE_THICKNESS",
  "STRIKEOUT_ASCENT",
  "STRIKEOUT_DESCENT",
  "ITALIC_ANGLE",
  "X_HEIGHT",
  "QUAD_WIDTH",
  "WEIGHT",
  "POINT_SIZE",
  "RESOLUTION",
  "COPYRIGHT",
  "NOTICE",
  "FONT_NAME",
  "FAMILY_NAME",
  "FULL_NAME",
  "CAP_HEIGHT",
  "WM_CLASS",
  "WM_TRANSIENT_FOR",
  /* Below here, these our our additions. Increment N_CUSTOM_PREDEFINED
   * if you add any.
   */
  "CLIPBOARD"			/* = 69 */
};

#define N_CUSTOM_PREDEFINED 1

#define ATOM_TO_INDEX(atom) (GPOINTER_TO_UINT(atom))
#define INDEX_TO_ATOM(atom) ((GdkAtom)GUINT_TO_POINTER(atom))

void
insert_atom_pair (GdkAtom     virtual_atom,
		  Atom        xatom)
Elliot Lee's avatar
Elliot Lee committed
125
{
126 127 128 129 130
  if (!atom_from_virtual)
    {
      atom_from_virtual = g_hash_table_new (g_direct_hash, NULL);
      atom_to_virtual = g_hash_table_new (g_direct_hash, NULL);
    }
131
  
132 133 134 135 136
  g_hash_table_insert (atom_from_virtual, 
		       GDK_ATOM_TO_POINTER (virtual_atom), GUINT_TO_POINTER (xatom));
  g_hash_table_insert (atom_to_virtual,
		       GUINT_TO_POINTER (xatom), GDK_ATOM_TO_POINTER (virtual_atom));
}
137

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
/**
 * gdk_x11_atom_to_xatom:
 * @atom: A #GdkAtom 
 * 
 * Convert from a #GdkAtom to the X atom for the default GDK display
 * with the same string value.x
 * 
 * Return value: the X atom corresponding to @atom.
 **/
Atom
gdk_x11_atom_to_xatom (GdkAtom atom)
{
  Atom xatom = None;
  
  if (ATOM_TO_INDEX (atom) < G_N_ELEMENTS (XAtomsStrings) - N_CUSTOM_PREDEFINED)
    return ATOM_TO_INDEX (atom);
  
  if (atom_from_virtual)
    xatom = GPOINTER_TO_UINT (g_hash_table_lookup (atom_from_virtual,
						   GDK_ATOM_TO_POINTER (atom)));
  if (!xatom)
159
    {
160 161 162
      char *name;
      
      g_return_val_if_fail (ATOM_TO_INDEX (atom) < virtual_atom_array->len, None);
163

164 165 166 167
      name = g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
      
      xatom = XInternAtom (gdk_display, name, FALSE);
      insert_atom_pair (atom, xatom);
168
    }
169

170
  return xatom;
Elliot Lee's avatar
Elliot Lee committed
171 172
}

173 174 175 176 177 178 179 180 181 182 183
/**
 * gdk_x11_xatom_to_atom:
 * @xatom: an X atom for the default GDK display
 * 
 * Convert from an X atom for the default display to the corresponding
 * #GdkAtom.
 * 
 * Return value: the corresponding G#dkAtom.
 **/
GdkAtom
gdk_x11_xatom_to_atom (Atom xatom)
Elliot Lee's avatar
Elliot Lee committed
184
{
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
  GdkAtom virtual_atom = GDK_NONE;
  
  if (xatom < G_N_ELEMENTS (XAtomsStrings) - N_CUSTOM_PREDEFINED)
    return INDEX_TO_ATOM (xatom);
  
  if (atom_to_virtual)
    virtual_atom = GDK_POINTER_TO_ATOM (g_hash_table_lookup (atom_to_virtual,
							     GUINT_TO_POINTER (xatom)));
  
  if (!virtual_atom)
    {
      /* If this atom doesn't exist, we'll die with an X error unless
       * we take precautions
       */
      char *name;
      gdk_error_trap_push ();
      name = XGetAtomName (gdk_display, xatom);
      if (gdk_error_trap_pop ())
	{
	  g_warning (G_STRLOC " invalid X atom: %ld", xatom);
	}
      else
	{
	  virtual_atom = gdk_atom_intern (name, FALSE);
	  XFree (name);
	  
	  insert_atom_pair (virtual_atom, xatom);
	}
    }
Elliot Lee's avatar
Elliot Lee committed
214

215 216
  return virtual_atom;
}
Elliot Lee's avatar
Elliot Lee committed
217

218 219 220 221
static void
virtual_atom_check_init (void)
{
  if (!virtual_atom_hash)
Elliot Lee's avatar
Elliot Lee committed
222
    {
223 224 225 226 227 228 229 230 231 232 233
      gint i;
      
      virtual_atom_hash = g_hash_table_new (g_str_hash, g_str_equal);
      virtual_atom_array = g_ptr_array_new ();
      
      for (i = 0; i < G_N_ELEMENTS (XAtomsStrings); i++)
	{
	  g_ptr_array_add (virtual_atom_array, XAtomsStrings[i]);
	  g_hash_table_insert (virtual_atom_hash, XAtomsStrings[i],
			       GUINT_TO_POINTER (i));
	}
Elliot Lee's avatar
Elliot Lee committed
234
    }
235 236 237 238 239 240 241 242 243 244 245 246
}

GdkAtom
gdk_atom_intern (const gchar *atom_name, 
		 gboolean     only_if_exists)
{
  GdkAtom result;

  virtual_atom_check_init ();
  
  result = GDK_POINTER_TO_ATOM (g_hash_table_lookup (virtual_atom_hash, atom_name));
  if (!result)
Elliot Lee's avatar
Elliot Lee committed
247
    {
248
      result = INDEX_TO_ATOM (virtual_atom_array->len);
Elliot Lee's avatar
Elliot Lee committed
249
      
250 251 252 253 254
      g_ptr_array_add (virtual_atom_array, g_strdup (atom_name));
      g_hash_table_insert (virtual_atom_hash, 
			   g_ptr_array_index (virtual_atom_array,
					      ATOM_TO_INDEX (result)),
			   GDK_ATOM_TO_POINTER (result));
Elliot Lee's avatar
Elliot Lee committed
255
    }
256 257 258 259 260 261 262 263 264 265

  return result;
}

static G_CONST_RETURN char *
get_atom_name (GdkAtom atom)
{
  virtual_atom_check_init ();

  if (ATOM_TO_INDEX (atom) < virtual_atom_array->len)
266
    return g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
  else
    return NULL;
}

gchar *
gdk_atom_name (GdkAtom atom)
{
  return g_strdup (get_atom_name (atom));
}

/**
 * gdk_x11_get_xatom_by_name:
 * @atom_name: a string
 * 
 * Returns the X atom for GDK's default display corresponding to @atom_name.
 * This function caches the result, so if called repeatedly it is much
 * faster than XInternAtom, which is a round trip to the server each time.
 * 
 * Return value: a X atom for GDK's default display.
 **/
Atom
gdk_x11_get_xatom_by_name (const gchar *atom_name)
{
  return gdk_x11_atom_to_xatom (gdk_atom_intern (atom_name, FALSE));
}

/**
 * gdk_x11_get_xatom_name:
 * @xatom: an X atom for GDK's default display
 * 
 * Returns the name of an X atom for GDK's default display. This
 * function is meant mainly for debugging, so for convenience, unlike
 * XAtomName() and gdk_atom_name(), the result doesn't need to
 * be freed. Also, this function will never return %NULL, even
 * if @xatom is invalid.
 * 
 * Return value: name of the X atom; this string is owned by GTK+,
 *   so it shouldn't be modifed or freed. 
 **/
G_CONST_RETURN gchar *
gdk_x11_get_xatom_name (Atom xatom)
{
  return get_atom_name (gdk_x11_xatom_to_atom (xatom));
Elliot Lee's avatar
Elliot Lee committed
310 311
}

Owen Taylor's avatar
Owen Taylor committed
312
gboolean
Elliot Lee's avatar
Elliot Lee committed
313 314 315 316 317 318 319 320 321 322 323 324 325
gdk_property_get (GdkWindow   *window,
		  GdkAtom      property,
		  GdkAtom      type,
		  gulong       offset,
		  gulong       length,
		  gint         pdelete,
		  GdkAtom     *actual_property_type,
		  gint        *actual_format_type,
		  gint        *actual_length,
		  guchar     **data)
{
  Display *xdisplay;
  Window xwindow;
326 327
  Atom xproperty;
  Atom xtype;
Elliot Lee's avatar
Elliot Lee committed
328 329 330 331 332 333 334
  Atom ret_prop_type;
  gint ret_format;
  gulong ret_nitems;
  gulong ret_bytes_after;
  gulong ret_length;
  guchar *ret_data;

335
  g_return_val_if_fail (!window || GDK_IS_WINDOW (window), FALSE);
336

337 338 339
  xproperty = gdk_x11_atom_to_xatom (property);
  xtype = gdk_x11_atom_to_xatom (type);

Elliot Lee's avatar
Elliot Lee committed
340 341
  if (window)
    {
342
      if (GDK_WINDOW_DESTROYED (window))
343 344
	return FALSE;

345 346
      xdisplay = GDK_WINDOW_XDISPLAY (window);
      xwindow = GDK_WINDOW_XID (window);
Elliot Lee's avatar
Elliot Lee committed
347 348 349 350
    }
  else
    {
      xdisplay = gdk_display;
351
      xwindow = _gdk_root_window;
Elliot Lee's avatar
Elliot Lee committed
352 353
    }

354
  ret_data = NULL;
355
  XGetWindowProperty (xdisplay, xwindow, xproperty,
Elliot Lee's avatar
Elliot Lee committed
356
		      offset, (length + 3) / 4, pdelete,
357
		      xtype, &ret_prop_type, &ret_format,
Elliot Lee's avatar
Elliot Lee committed
358 359 360
		      &ret_nitems, &ret_bytes_after,
		      &ret_data);

361
  if ((ret_prop_type == None) && (ret_format == 0)) {
Elliot Lee's avatar
Elliot Lee committed
362
    return FALSE;
363
  }
Elliot Lee's avatar
Elliot Lee committed
364 365

  if (actual_property_type)
366
    *actual_property_type = gdk_x11_xatom_to_atom (ret_prop_type);
Elliot Lee's avatar
Elliot Lee committed
367 368 369
  if (actual_format_type)
    *actual_format_type = ret_format;

370
  if ((type != AnyPropertyType) && (ret_prop_type != xtype))
Elliot Lee's avatar
Elliot Lee committed
371 372
    {
      XFree (ret_data);
373 374 375 376
      
      g_warning("Couldn't match property type %s to %s\n", 
		gdk_x11_get_xatom_name (ret_prop_type),
		gdk_x11_get_xatom_name (xtype));
Elliot Lee's avatar
Elliot Lee committed
377 378 379 380 381 382 383 384 385 386 387 388 389
      return FALSE;
    }

  /* FIXME: ignoring bytes_after could have very bad effects */

  if (data)
    {
      switch (ret_format)
	{
	case 8:
	  ret_length = ret_nitems;
	  break;
	case 16:
390
	  ret_length = sizeof(short) * ret_nitems;
Elliot Lee's avatar
Elliot Lee committed
391 392
	  break;
	case 32:
393
	  ret_length = sizeof(long) * ret_nitems;
Elliot Lee's avatar
Elliot Lee committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
	  break;
	default:
	  g_warning ("unknown property return format: %d", ret_format);
	  XFree (ret_data);
	  return FALSE;
	}

      *data = g_new (guchar, ret_length);
      memcpy (*data, ret_data, ret_length);
      if (actual_length)
	*actual_length = ret_length;
    }

  XFree (ret_data);

  return TRUE;
}

void
Owen Taylor's avatar
Owen Taylor committed
413 414 415 416 417 418 419
gdk_property_change (GdkWindow    *window,
		     GdkAtom       property,
		     GdkAtom       type,
		     gint          format,
		     GdkPropMode   mode,
		     const guchar *data,
		     gint          nelements)
Elliot Lee's avatar
Elliot Lee committed
420 421 422
{
  Display *xdisplay;
  Window xwindow;
423 424
  Atom xproperty;
  Atom xtype;
Elliot Lee's avatar
Elliot Lee committed
425

426
  g_return_if_fail (!window || GDK_IS_WINDOW (window));
427

428 429 430
  xproperty = gdk_x11_atom_to_xatom (property);
  xtype = gdk_x11_atom_to_xatom (type);

Elliot Lee's avatar
Elliot Lee committed
431 432
  if (window)
    {
433
      if (GDK_WINDOW_DESTROYED (window))
434 435
	return;

436 437
      xdisplay = GDK_WINDOW_XDISPLAY (window);
      xwindow = GDK_WINDOW_XID (window);
Elliot Lee's avatar
Elliot Lee committed
438 439 440 441
    }
  else
    {
      xdisplay = gdk_display;
442
      xwindow = _gdk_root_window;
Elliot Lee's avatar
Elliot Lee committed
443 444
    }

445
  XChangeProperty (xdisplay, xwindow, xproperty, xtype,
Owen Taylor's avatar
Owen Taylor committed
446
		   format, mode, (guchar *)data, nelements);
Elliot Lee's avatar
Elliot Lee committed
447 448 449 450 451 452 453 454 455
}

void
gdk_property_delete (GdkWindow *window,
		     GdkAtom    property)
{
  Display *xdisplay;
  Window xwindow;

456
  g_return_if_fail (!window || GDK_IS_WINDOW (window));
457

Elliot Lee's avatar
Elliot Lee committed
458 459
  if (window)
    {
460
      if (GDK_WINDOW_DESTROYED (window))
461 462
	return;

463 464
      xdisplay = GDK_WINDOW_XDISPLAY (window);
      xwindow = GDK_WINDOW_XID (window);
Elliot Lee's avatar
Elliot Lee committed
465 466 467 468
    }
  else
    {
      xdisplay = gdk_display;
469
      xwindow = _gdk_root_window;
Elliot Lee's avatar
Elliot Lee committed
470 471
    }

472
  XDeleteProperty (xdisplay, xwindow, gdk_x11_atom_to_xatom (property));
Elliot Lee's avatar
Elliot Lee committed
473
}