]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkproperty-x11.c
Added notice to look in AUTHORS and ChangeLog files for a list of changes.
[~andy/gtk] / gdk / x11 / gdkproperty-x11.c
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 Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-1999.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include <X11/Xlib.h>
28 #include <X11/Xatom.h>
29 #include <string.h>
30 #include "gdk.h"
31 #include "gdkprivate.h"
32
33 GdkAtom
34 gdk_atom_intern (const gchar *atom_name,
35                  gint         only_if_exists)
36 {
37   GdkAtom retval;
38   static GHashTable *atom_hash = NULL;
39   
40   if (!atom_hash)
41     atom_hash = g_hash_table_new (g_str_hash, g_str_equal);
42
43   retval = GPOINTER_TO_UINT (g_hash_table_lookup (atom_hash, atom_name));
44   if (!retval)
45     {
46       retval = XInternAtom (gdk_display, atom_name, only_if_exists);
47
48       g_hash_table_insert (atom_hash, 
49                            g_strdup (atom_name), 
50                            GUINT_TO_POINTER (retval));
51     }
52
53   return retval;
54 }
55
56 gchar *
57 gdk_atom_name (GdkAtom atom)
58 {
59   gchar *t;
60   gchar *name;
61   gint old_error_warnings;
62
63   /* If this atom doesn't exist, we'll die with an X error unless
64      we take precautions */
65
66   old_error_warnings = gdk_error_warnings;
67   gdk_error_warnings = 0;
68   gdk_error_code = 0;
69   t = XGetAtomName (gdk_display, atom);
70   gdk_error_warnings = old_error_warnings;
71
72   if (gdk_error_code == -1)
73     {
74       if (t)
75         XFree (t);
76
77       return NULL;
78     }
79   else
80     {
81       name = g_strdup (t);
82       if (t)
83         XFree (t);
84       
85       return name;
86     }
87 }
88
89 gint
90 gdk_property_get (GdkWindow   *window,
91                   GdkAtom      property,
92                   GdkAtom      type,
93                   gulong       offset,
94                   gulong       length,
95                   gint         pdelete,
96                   GdkAtom     *actual_property_type,
97                   gint        *actual_format_type,
98                   gint        *actual_length,
99                   guchar     **data)
100 {
101   Display *xdisplay;
102   Window xwindow;
103   Atom ret_prop_type;
104   gint ret_format;
105   gulong ret_nitems;
106   gulong ret_bytes_after;
107   gulong ret_length;
108   guchar *ret_data;
109
110   if (window)
111     {
112       GdkWindowPrivate *private;
113
114       private = (GdkWindowPrivate*) window;
115       if (private->destroyed)
116         return FALSE;
117
118       xdisplay = private->xdisplay;
119       xwindow = private->xwindow;
120     }
121   else
122     {
123       xdisplay = gdk_display;
124       xwindow = gdk_root_window;
125     }
126
127   ret_data = NULL;
128   XGetWindowProperty (xdisplay, xwindow, property,
129                       offset, (length + 3) / 4, pdelete,
130                       type, &ret_prop_type, &ret_format,
131                       &ret_nitems, &ret_bytes_after,
132                       &ret_data);
133
134   if ((ret_prop_type == None) && (ret_format == 0)) {
135     return FALSE;
136   }
137
138   if (actual_property_type)
139     *actual_property_type = ret_prop_type;
140   if (actual_format_type)
141     *actual_format_type = ret_format;
142
143   if ((type != AnyPropertyType) && (ret_prop_type != type))
144     {
145       gchar *rn, *pn;
146
147       XFree (ret_data);
148       rn = gdk_atom_name(ret_prop_type);
149       pn = gdk_atom_name(type);
150       g_warning("Couldn't match property type %s to %s\n", rn, pn);
151       g_free(rn); g_free(pn);
152       return FALSE;
153     }
154
155   /* FIXME: ignoring bytes_after could have very bad effects */
156
157   if (data)
158     {
159       switch (ret_format)
160         {
161         case 8:
162           ret_length = ret_nitems;
163           break;
164         case 16:
165           ret_length = 2 * ret_nitems;
166           break;
167         case 32:
168           ret_length = 4 * ret_nitems;
169           break;
170         default:
171           g_warning ("unknown property return format: %d", ret_format);
172           XFree (ret_data);
173           return FALSE;
174         }
175
176       *data = g_new (guchar, ret_length);
177       memcpy (*data, ret_data, ret_length);
178       if (actual_length)
179         *actual_length = ret_length;
180     }
181
182   XFree (ret_data);
183
184   return TRUE;
185 }
186
187 void
188 gdk_property_change (GdkWindow   *window,
189                      GdkAtom      property,
190                      GdkAtom      type,
191                      gint         format,
192                      GdkPropMode  mode,
193                      guchar      *data,
194                      gint         nelements)
195 {
196   Display *xdisplay;
197   Window xwindow;
198
199   if (window)
200     {
201       GdkWindowPrivate *private;
202
203       private = (GdkWindowPrivate*) window;
204       if (private->destroyed)
205         return;
206
207       xdisplay = private->xdisplay;
208       xwindow = private->xwindow;
209     }
210   else
211     {
212       xdisplay = gdk_display;
213       xwindow = gdk_root_window;
214     }
215
216   XChangeProperty (xdisplay, xwindow, property, type,
217                    format, mode, data, nelements);
218 }
219
220 void
221 gdk_property_delete (GdkWindow *window,
222                      GdkAtom    property)
223 {
224   Display *xdisplay;
225   Window xwindow;
226
227   if (window)
228     {
229       GdkWindowPrivate *private;
230
231       private = (GdkWindowPrivate*) window;
232       if (private->destroyed)
233         return;
234
235       xdisplay = private->xdisplay;
236       xwindow = private->xwindow;
237     }
238   else
239     {
240       xdisplay = gdk_display;
241       xwindow = gdk_root_window;
242     }
243
244   XDeleteProperty (xdisplay, xwindow, property);
245 }