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 | if (retval != None) |
---|
49 | g_hash_table_insert (atom_hash, |
---|
50 | g_strdup (atom_name), |
---|
51 | GUINT_TO_POINTER (retval)); |
---|
52 | } |
---|
53 | |
---|
54 | return retval; |
---|
55 | } |
---|
56 | |
---|
57 | gchar* |
---|
58 | gdk_atom_name (GdkAtom atom) |
---|
59 | { |
---|
60 | gchar *t; |
---|
61 | gchar *name; |
---|
62 | gint old_error_warnings; |
---|
63 | |
---|
64 | /* If this atom doesn't exist, we'll die with an X error unless |
---|
65 | we take precautions */ |
---|
66 | |
---|
67 | old_error_warnings = gdk_error_warnings; |
---|
68 | gdk_error_warnings = 0; |
---|
69 | gdk_error_code = 0; |
---|
70 | t = XGetAtomName (gdk_display, atom); |
---|
71 | gdk_error_warnings = old_error_warnings; |
---|
72 | |
---|
73 | if (gdk_error_code) |
---|
74 | { |
---|
75 | if (t) |
---|
76 | XFree (t); |
---|
77 | |
---|
78 | return NULL; |
---|
79 | } |
---|
80 | else |
---|
81 | { |
---|
82 | name = g_strdup (t); |
---|
83 | if (t) |
---|
84 | XFree (t); |
---|
85 | |
---|
86 | return name; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | gboolean |
---|
91 | gdk_property_get (GdkWindow *window, |
---|
92 | GdkAtom property, |
---|
93 | GdkAtom type, |
---|
94 | gulong offset, |
---|
95 | gulong length, |
---|
96 | gint pdelete, |
---|
97 | GdkAtom *actual_property_type, |
---|
98 | gint *actual_format_type, |
---|
99 | gint *actual_length, |
---|
100 | guchar **data) |
---|
101 | { |
---|
102 | Display *xdisplay; |
---|
103 | Window xwindow; |
---|
104 | Atom ret_prop_type; |
---|
105 | gint ret_format; |
---|
106 | gulong ret_nitems; |
---|
107 | gulong ret_bytes_after; |
---|
108 | gulong ret_length; |
---|
109 | guchar *ret_data; |
---|
110 | |
---|
111 | if (window) |
---|
112 | { |
---|
113 | GdkWindowPrivate *private; |
---|
114 | |
---|
115 | private = (GdkWindowPrivate*) window; |
---|
116 | if (private->destroyed) |
---|
117 | return FALSE; |
---|
118 | |
---|
119 | xdisplay = private->xdisplay; |
---|
120 | xwindow = private->xwindow; |
---|
121 | } |
---|
122 | else |
---|
123 | { |
---|
124 | xdisplay = gdk_display; |
---|
125 | xwindow = gdk_root_window; |
---|
126 | } |
---|
127 | |
---|
128 | ret_data = NULL; |
---|
129 | XGetWindowProperty (xdisplay, xwindow, property, |
---|
130 | offset, (length + 3) / 4, pdelete, |
---|
131 | type, &ret_prop_type, &ret_format, |
---|
132 | &ret_nitems, &ret_bytes_after, |
---|
133 | &ret_data); |
---|
134 | |
---|
135 | if ((ret_prop_type == None) && (ret_format == 0)) { |
---|
136 | return FALSE; |
---|
137 | } |
---|
138 | |
---|
139 | if (actual_property_type) |
---|
140 | *actual_property_type = ret_prop_type; |
---|
141 | if (actual_format_type) |
---|
142 | *actual_format_type = ret_format; |
---|
143 | |
---|
144 | if ((type != AnyPropertyType) && (ret_prop_type != type)) |
---|
145 | { |
---|
146 | gchar *rn, *pn; |
---|
147 | |
---|
148 | XFree (ret_data); |
---|
149 | rn = gdk_atom_name(ret_prop_type); |
---|
150 | pn = gdk_atom_name(type); |
---|
151 | g_warning("Couldn't match property type %s to %s\n", rn, pn); |
---|
152 | g_free(rn); g_free(pn); |
---|
153 | return FALSE; |
---|
154 | } |
---|
155 | |
---|
156 | /* FIXME: ignoring bytes_after could have very bad effects */ |
---|
157 | |
---|
158 | if (data) |
---|
159 | { |
---|
160 | switch (ret_format) |
---|
161 | { |
---|
162 | case 8: |
---|
163 | ret_length = ret_nitems; |
---|
164 | break; |
---|
165 | case 16: |
---|
166 | ret_length = sizeof(short) * ret_nitems; |
---|
167 | break; |
---|
168 | case 32: |
---|
169 | ret_length = sizeof(long) * ret_nitems; |
---|
170 | break; |
---|
171 | default: |
---|
172 | g_warning ("unknown property return format: %d", ret_format); |
---|
173 | XFree (ret_data); |
---|
174 | return FALSE; |
---|
175 | } |
---|
176 | |
---|
177 | *data = g_new (guchar, ret_length); |
---|
178 | memcpy (*data, ret_data, ret_length); |
---|
179 | if (actual_length) |
---|
180 | *actual_length = ret_length; |
---|
181 | } |
---|
182 | |
---|
183 | XFree (ret_data); |
---|
184 | |
---|
185 | return TRUE; |
---|
186 | } |
---|
187 | |
---|
188 | void |
---|
189 | gdk_property_change (GdkWindow *window, |
---|
190 | GdkAtom property, |
---|
191 | GdkAtom type, |
---|
192 | gint format, |
---|
193 | GdkPropMode mode, |
---|
194 | guchar *data, |
---|
195 | gint nelements) |
---|
196 | { |
---|
197 | Display *xdisplay; |
---|
198 | Window xwindow; |
---|
199 | |
---|
200 | if (window) |
---|
201 | { |
---|
202 | GdkWindowPrivate *private; |
---|
203 | |
---|
204 | private = (GdkWindowPrivate*) window; |
---|
205 | if (private->destroyed) |
---|
206 | return; |
---|
207 | |
---|
208 | xdisplay = private->xdisplay; |
---|
209 | xwindow = private->xwindow; |
---|
210 | } |
---|
211 | else |
---|
212 | { |
---|
213 | xdisplay = gdk_display; |
---|
214 | xwindow = gdk_root_window; |
---|
215 | } |
---|
216 | |
---|
217 | XChangeProperty (xdisplay, xwindow, property, type, |
---|
218 | format, mode, data, nelements); |
---|
219 | } |
---|
220 | |
---|
221 | void |
---|
222 | gdk_property_delete (GdkWindow *window, |
---|
223 | GdkAtom property) |
---|
224 | { |
---|
225 | Display *xdisplay; |
---|
226 | Window xwindow; |
---|
227 | |
---|
228 | if (window) |
---|
229 | { |
---|
230 | GdkWindowPrivate *private; |
---|
231 | |
---|
232 | private = (GdkWindowPrivate*) window; |
---|
233 | if (private->destroyed) |
---|
234 | return; |
---|
235 | |
---|
236 | xdisplay = private->xdisplay; |
---|
237 | xwindow = private->xwindow; |
---|
238 | } |
---|
239 | else |
---|
240 | { |
---|
241 | xdisplay = gdk_display; |
---|
242 | xwindow = gdk_root_window; |
---|
243 | } |
---|
244 | |
---|
245 | XDeleteProperty (xdisplay, xwindow, property); |
---|
246 | } |
---|