* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
-#include <X11/Xlib.h>
-#include <X11/Xatom.h>
-#include <string.h>
+#include "config.h"
-#include "gdk.h" /* For gdk_error_trap_push/pop() */
-#include "gdkx.h"
#include "gdkproperty.h"
+#include "gdkmain.h"
#include "gdkprivate.h"
#include "gdkinternals.h"
+#include "gdkselection.h"
+#include "gdkprivate-x11.h"
#include "gdkdisplay-x11.h"
#include "gdkscreen-x11.h"
-#include "gdkselection.h" /* only from predefined atom */
+
+#include <X11/Xlib.h>
+#include <X11/Xatom.h>
+#include <string.h>
+
+
+/**
+ * SECTION:properties
+ * @Short_description: Functions to manipulate properties on windows
+ * @Title: Properties and Atoms
+ *
+ * Each window under X can have any number of associated
+ * <firstterm>properties</firstterm> attached to it.
+ * Properties are arbitrary chunks of data identified by
+ * <firstterm>atom</firstterm>s. (An <firstterm>atom</firstterm>
+ * is a numeric index into a string table on the X server. They are used
+ * to transfer strings efficiently between clients without
+ * having to transfer the entire string.) A property
+ * has an associated type, which is also identified
+ * using an atom.
+ *
+ * A property has an associated <firstterm>format</firstterm>,
+ * an integer describing how many bits are in each unit
+ * of data inside the property. It must be 8, 16, or 32.
+ * When data is transferred between the server and client,
+ * if they are of different endianesses it will be byteswapped
+ * as necessary according to the format of the property.
+ * Note that on the client side, properties of format 32
+ * will be stored with one unit per <emphasis>long</emphasis>,
+ * even if a long integer has more than 32 bits on the platform.
+ * (This decision was apparently made for Xlib to maintain
+ * compatibility with programs that assumed longs were 32
+ * bits, at the expense of programs that knew better.)
+ *
+ * The functions in this section are used to add, remove
+ * and change properties on windows, to convert atoms
+ * to and from strings and to manipulate some types of
+ * data commonly stored in X window properties.
+ */
+
static GPtrArray *virtual_atom_array;
static GHashTable *virtual_atom_hash;
-static gchar *XAtomsStrings[] = {
+static const gchar xatoms_string[] =
/* 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",
+ "\0" /* leave a space for None, even though it is not a predefined atom */
+ "PRIMARY\0"
+ "SECONDARY\0"
+ "ARC\0"
+ "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_TRANSIENT_FOR\0"
/* Below here, these are our additions. Increment N_CUSTOM_PREDEFINED
* if you add any.
*/
- "CLIPBOARD" /* = 69 */
+ "CLIPBOARD\0" /* = 69 */
+;
+
+static const gint xatoms_offset[] = {
+ 0, 1, 9, 19, 23, 28, 35, 44, 53, 60, 72, 84,
+ 96, 108, 120, 132, 144, 156, 165, 170, 178, 185, 189, 201,
+ 218, 232, 245, 258, 274, 287, 301, 313, 320, 329, 336, 347,
+ 356, 374, 387, 400, 408, 424, 438, 452, 462, 473, 483, 493,
+ 507, 521, 533, 545, 564, 584, 601, 619, 632, 641, 652, 659,
+ 670, 681, 691, 698, 708, 720, 730, 741, 750, 767
};
#define N_CUSTOM_PREDEFINED 1
GdkAtom virtual_atom,
Atom xatom)
{
- GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
+ GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
if (!display_x11->atom_from_virtual)
{
GUINT_TO_POINTER (xatom),
GDK_ATOM_TO_POINTER (virtual_atom));
}
+
+static Atom
+lookup_cached_xatom (GdkDisplay *display,
+ GdkAtom atom)
+{
+ GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
+
+ if (ATOM_TO_INDEX (atom) < G_N_ELEMENTS (xatoms_offset) - N_CUSTOM_PREDEFINED)
+ return ATOM_TO_INDEX (atom);
+
+ if (display_x11->atom_from_virtual)
+ return GPOINTER_TO_UINT (g_hash_table_lookup (display_x11->atom_from_virtual,
+ GDK_ATOM_TO_POINTER (atom)));
+
+ return None;
+}
+
/**
* gdk_x11_atom_to_xatom_for_display:
* @display: A #GdkDisplay
- * @atom: A #GdkAtom
- *
+ * @atom: A #GdkAtom, or %GDK_NONE
+ *
* Converts from a #GdkAtom to the X atom for a #GdkDisplay
- * with the same string value.
- *
- * Return value: the X atom corresponding to @atom.
+ * with the same string value. The special value %GDK_NONE
+ * is converted to %None.
+ *
+ * Return value: the X atom corresponding to @atom, or %None
*
* Since: 2.2
**/
Atom
-gdk_x11_atom_to_xatom_for_display (GdkDisplay *display,
- GdkAtom atom)
+gdk_x11_atom_to_xatom_for_display (GdkDisplay *display,
+ GdkAtom atom)
{
- GdkDisplayX11 *display_x11;
Atom xatom = None;
-
+
g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
- if (display->closed)
+ if (atom == GDK_NONE)
return None;
-
- display_x11 = GDK_DISPLAY_X11 (display);
-
- if (ATOM_TO_INDEX (atom) < G_N_ELEMENTS (XAtomsStrings) - N_CUSTOM_PREDEFINED)
- return ATOM_TO_INDEX (atom);
-
- if (display_x11->atom_from_virtual)
- xatom = GPOINTER_TO_UINT (g_hash_table_lookup (display_x11->atom_from_virtual,
- GDK_ATOM_TO_POINTER (atom)));
+
+ if (gdk_display_is_closed (display))
+ return None;
+
+ xatom = lookup_cached_xatom (display, atom);
+
if (!xatom)
{
char *name;
-
+
g_return_val_if_fail (ATOM_TO_INDEX (atom) < virtual_atom_array->len, None);
name = g_ptr_array_index (virtual_atom_array, ATOM_TO_INDEX (atom));
-
+
xatom = XInternAtom (GDK_DISPLAY_XDISPLAY (display), name, FALSE);
insert_atom_pair (display, atom, xatom);
}
return xatom;
}
+void
+_gdk_x11_precache_atoms (GdkDisplay *display,
+ const gchar * const *atom_names,
+ gint n_atoms)
+{
+ Atom *xatoms;
+ GdkAtom *atoms;
+ const gchar **xatom_names;
+ gint n_xatoms;
+ gint i;
+
+ xatoms = g_new (Atom, n_atoms);
+ xatom_names = g_new (const gchar *, n_atoms);
+ atoms = g_new (GdkAtom, n_atoms);
+
+ n_xatoms = 0;
+ for (i = 0; i < n_atoms; i++)
+ {
+ GdkAtom atom = gdk_atom_intern_static_string (atom_names[i]);
+ if (lookup_cached_xatom (display, atom) == None)
+ {
+ atoms[n_xatoms] = atom;
+ xatom_names[n_xatoms] = atom_names[i];
+ n_xatoms++;
+ }
+ }
+
+ if (n_xatoms)
+ XInternAtoms (GDK_DISPLAY_XDISPLAY (display),
+ (char **)xatom_names, n_xatoms, False, xatoms);
+
+ for (i = 0; i < n_xatoms; i++)
+ insert_atom_pair (display, atoms[i], xatoms[i]);
+
+ g_free (xatoms);
+ g_free (xatom_names);
+ g_free (atoms);
+}
+
/**
* gdk_x11_atom_to_xatom:
* @atom: A #GdkAtom
* Convert from an X atom for a #GdkDisplay to the corresponding
* #GdkAtom.
*
- * Return value: the corresponding #GdkAtom.
+ * Return value: (transfer none): the corresponding #GdkAtom.
*
* Since: 2.2
**/
gdk_x11_xatom_to_atom_for_display (GdkDisplay *display,
Atom xatom)
{
- GdkDisplayX11 *display_x11;
+ GdkX11Display *display_x11;
GdkAtom virtual_atom = GDK_NONE;
g_return_val_if_fail (GDK_IS_DISPLAY (display), GDK_NONE);
- if (display->closed)
+ if (xatom == None)
+ return GDK_NONE;
+
+ if (gdk_display_is_closed (display))
return GDK_NONE;
- display_x11 = GDK_DISPLAY_X11 (display);
+ display_x11 = GDK_X11_DISPLAY (display);
- if (xatom < G_N_ELEMENTS (XAtomsStrings) - N_CUSTOM_PREDEFINED)
+ if (xatom < G_N_ELEMENTS (xatoms_offset) - N_CUSTOM_PREDEFINED)
return INDEX_TO_ATOM (xatom);
if (display_x11->atom_to_virtual)
* we take precautions
*/
char *name;
- gdk_error_trap_push ();
+ gdk_x11_display_error_trap_push (display);
name = XGetAtomName (GDK_DISPLAY_XDISPLAY (display), xatom);
- if (gdk_error_trap_pop ())
+ if (gdk_x11_display_error_trap_pop (display))
{
g_warning (G_STRLOC " invalid X atom: %ld", xatom);
}
* Convert from an X atom for the default display to the corresponding
* #GdkAtom.
*
- * Return value: the corresponding G#dkAtom.
+ * Return value: (transfer none): the corresponding G#dkAtom.
**/
GdkAtom
gdk_x11_xatom_to_atom (Atom xatom)
if (!virtual_atom_hash)
{
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));
- }
+
+ for (i = 0; i < G_N_ELEMENTS (xatoms_offset); i++)
+ {
+ g_ptr_array_add (virtual_atom_array, (gchar *)(xatoms_string + xatoms_offset[i]));
+ g_hash_table_insert (virtual_atom_hash, (gchar *)(xatoms_string + xatoms_offset[i]),
+ GUINT_TO_POINTER (i));
+ }
}
}
GdkAtom
-gdk_atom_intern (const gchar *atom_name,
- gboolean only_if_exists)
+_gdk_x11_display_manager_atom_intern (GdkDisplayManager *manager,
+ const gchar *atom_name,
+ gboolean dup)
{
GdkAtom result;
virtual_atom_check_init ();
-
+
result = GDK_POINTER_TO_ATOM (g_hash_table_lookup (virtual_atom_hash, atom_name));
if (!result)
{
result = INDEX_TO_ATOM (virtual_atom_array->len);
-
- 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));
+
+ g_ptr_array_add (virtual_atom_array, dup ? g_strdup (atom_name) : (gchar *)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));
}
return result;
}
-static G_CONST_RETURN char *
+static const gchar *
get_atom_name (GdkAtom atom)
{
virtual_atom_check_init ();
return NULL;
}
+
gchar *
-gdk_atom_name (GdkAtom atom)
+_gdk_x11_display_manager_get_atom_name (GdkDisplayManager *manager,
+ GdkAtom atom)
{
return g_strdup (get_atom_name (atom));
}
*
* 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 <function>XInternAtom()</function>, which is a round trip to
- * the server each time.
+ * faster than XInternAtom(), which is a round trip to the server each time.
*
* Return value: a X atom for GDK's default display.
**/
*
* Since: 2.2
**/
-G_CONST_RETURN gchar *
+const gchar *
gdk_x11_get_xatom_name_for_display (GdkDisplay *display,
Atom xatom)
{
* 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 *
+const gchar *
gdk_x11_get_xatom_name (Atom xatom)
{
return get_atom_name (gdk_x11_xatom_to_atom (xatom));
}
gboolean
-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)
+_gdk_x11_window_get_property (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)
{
GdkDisplay *display;
Atom ret_prop_type;
gint ret_format;
gulong ret_nitems;
gulong ret_bytes_after;
+ gulong get_length;
gulong ret_length;
guchar *ret_data;
Atom xproperty;
Atom xtype;
int res;
- g_return_val_if_fail (!window || GDK_IS_WINDOW (window), FALSE);
+ g_return_val_if_fail (!window || GDK_WINDOW_IS_X11 (window), FALSE);
if (!window)
{
GDK_NOTE (MULTIHEAD, g_message ("gdk_property_get(): window is NULL\n"));
}
+ else if (!GDK_WINDOW_IS_X11 (window))
+ return FALSE;
if (GDK_WINDOW_DESTROYED (window))
return FALSE;
- display = gdk_drawable_get_display (window);
+ display = gdk_window_get_display (window);
xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
- xtype = gdk_x11_atom_to_xatom_for_display (display, type);
+ if (type == GDK_NONE)
+ xtype = AnyPropertyType;
+ else
+ xtype = gdk_x11_atom_to_xatom_for_display (display, type);
ret_data = NULL;
+ /*
+ * Round up length to next 4 byte value. Some code is in the (bad?)
+ * habit of passing G_MAXLONG as the length argument, causing an
+ * overflow to negative on the add. In this case, we clamp the
+ * value to G_MAXLONG.
+ */
+ get_length = length + 3;
+ if (get_length > G_MAXLONG)
+ get_length = G_MAXLONG;
+
+ /* To fail, either the user passed 0 or G_MAXULONG */
+ get_length = get_length / 4;
+ if (get_length == 0)
+ {
+ g_warning ("gdk_propery-get(): invalid length 0");
+ return FALSE;
+ }
+
res = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
- GDK_WINDOW_XWINDOW (window), xproperty,
- offset, (length + 3) / 4, pdelete,
+ GDK_WINDOW_XID (window), xproperty,
+ offset, get_length, pdelete,
xtype, &ret_prop_type, &ret_format,
&ret_nitems, &ret_bytes_after,
&ret_data);
}
void
-gdk_property_change (GdkWindow *window,
- GdkAtom property,
- GdkAtom type,
- gint format,
- GdkPropMode mode,
- const guchar *data,
- gint nelements)
+_gdk_x11_window_change_property (GdkWindow *window,
+ GdkAtom property,
+ GdkAtom type,
+ gint format,
+ GdkPropMode mode,
+ const guchar *data,
+ gint nelements)
{
GdkDisplay *display;
Window xwindow;
Atom xproperty;
Atom xtype;
- g_return_if_fail (!window || GDK_IS_WINDOW (window));
+ g_return_if_fail (!window || GDK_WINDOW_IS_X11 (window));
if (!window)
{
screen = gdk_screen_get_default ();
window = gdk_screen_get_root_window (screen);
- GDK_NOTE (MULTIHEAD, g_message ("gdk_property_delete(): window is NULL\n"));
+ GDK_NOTE (MULTIHEAD, g_message ("gdk_property_change(): window is NULL\n"));
}
-
+ else if (!GDK_WINDOW_IS_X11 (window))
+ return;
if (GDK_WINDOW_DESTROYED (window))
return;
- display = gdk_drawable_get_display (window);
-
+ gdk_window_ensure_native (window);
+
+ display = gdk_window_get_display (window);
xproperty = gdk_x11_atom_to_xatom_for_display (display, property);
xtype = gdk_x11_atom_to_xatom_for_display (display, type);
xwindow = GDK_WINDOW_XID (window);
}
void
-gdk_property_delete (GdkWindow *window,
- GdkAtom property)
+_gdk_x11_window_delete_property (GdkWindow *window,
+ GdkAtom property)
{
- g_return_if_fail (!window || GDK_IS_WINDOW (window));
+ g_return_if_fail (!window || GDK_WINDOW_IS_X11 (window));
if (!window)
{
GDK_NOTE (MULTIHEAD,
g_message ("gdk_property_delete(): window is NULL\n"));
}
+ else if (!GDK_WINDOW_IS_X11 (window))
+ return;
if (GDK_WINDOW_DESTROYED (window))
return;
- XDeleteProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XWINDOW (window),
+ XDeleteProperty (GDK_WINDOW_XDISPLAY (window), GDK_WINDOW_XID (window),
gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (window),
property));
}