]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkproperty-x11.c
Initial revision
[~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 Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 #include <X11/Xlib.h>
19 #include <X11/Xatom.h>
20 #include <string.h>
21 #include "gdk.h"
22 #include "gdkprivate.h"
23
24
25 GdkAtom
26 gdk_atom_intern (const gchar *atom_name,
27                  gint         only_if_exists)
28 {
29   return XInternAtom (gdk_display, atom_name, only_if_exists);
30 }
31
32 gchar *
33 gdk_atom_name (GdkAtom atom)
34 {
35   gchar *t;
36   gchar *name;
37
38   /* If this atom doesn't exist, we'll die with an X error unless
39      we take precautions */
40
41   gdk_error_warnings = 0;
42   t = XGetAtomName (gdk_display, atom);
43   gdk_error_warnings = 1;
44
45   if (gdk_error_code == -1)
46     {
47       return NULL;
48     }
49   else
50     {
51       name = g_strdup (t);
52       XFree (t);
53       
54       return name;
55     }
56 }
57
58 gint
59 gdk_property_get (GdkWindow   *window,
60                   GdkAtom      property,
61                   GdkAtom      type,
62                   gulong       offset,
63                   gulong       length,
64                   gint         pdelete,
65                   GdkAtom     *actual_property_type,
66                   gint        *actual_format_type,
67                   gint        *actual_length,
68                   guchar     **data)
69 {
70   GdkWindowPrivate *private;
71   Display *xdisplay;
72   Window xwindow;
73   Atom ret_prop_type;
74   gint ret_format;
75   gulong ret_nitems;
76   gulong ret_bytes_after;
77   gulong ret_length;
78   guchar *ret_data;
79
80   if (window)
81     {
82       private = (GdkWindowPrivate*) window;
83       xdisplay = private->xdisplay;
84       xwindow = private->xwindow;
85     }
86   else
87     {
88       xdisplay = gdk_display;
89       xwindow = gdk_root_window;
90     }
91
92   XGetWindowProperty (xdisplay, xwindow, property,
93                       offset, (length + 3) / 4, pdelete,
94                       type, &ret_prop_type, &ret_format,
95                       &ret_nitems, &ret_bytes_after,
96                       &ret_data);
97
98   if ((ret_prop_type == None) && (ret_format == 0))
99     return FALSE;
100
101   if (actual_property_type)
102     *actual_property_type = ret_prop_type;
103   if (actual_format_type)
104     *actual_format_type = ret_format;
105
106   if (ret_prop_type != property)
107     {
108       XFree (ret_data);
109       return FALSE;
110     }
111
112   /* FIXME: ignoring bytes_after could have very bad effects */
113
114   if (data)
115     {
116       switch (ret_format)
117         {
118         case 8:
119           ret_length = ret_nitems;
120           break;
121         case 16:
122           ret_length = 2 * ret_nitems;
123           break;
124         case 32:
125           ret_length = 4 * ret_nitems;
126           break;
127         default:
128           g_warning ("unknown property return format: %d", ret_format);
129           XFree (ret_data);
130           return FALSE;
131         }
132
133       *data = g_new (guchar, ret_length);
134       memcpy (*data, ret_data, ret_length);
135       if (actual_length)
136         *actual_length = ret_length;
137     }
138
139   XFree (ret_data);
140
141   return TRUE;
142 }
143
144 void
145 gdk_property_change (GdkWindow   *window,
146                      GdkAtom      property,
147                      GdkAtom      type,
148                      gint         format,
149                      GdkPropMode  mode,
150                      guchar      *data,
151                      gint         nelements)
152 {
153   GdkWindowPrivate *private;
154   Display *xdisplay;
155   Window xwindow;
156
157   if (window)
158     {
159       private = (GdkWindowPrivate*) window;
160       xdisplay = private->xdisplay;
161       xwindow = private->xwindow;
162     }
163   else
164     {
165       xdisplay = gdk_display;
166       xwindow = gdk_root_window;
167     }
168
169   XChangeProperty (xdisplay, xwindow, property, type,
170                    format, mode, data, nelements);
171 }
172
173 void
174 gdk_property_delete (GdkWindow *window,
175                      GdkAtom    property)
176 {
177   GdkWindowPrivate *private;
178   Display *xdisplay;
179   Window xwindow;
180
181   if (window)
182     {
183       private = (GdkWindowPrivate*) window;
184       xdisplay = private->xdisplay;
185       xwindow = private->xwindow;
186     }
187   else
188     {
189       xdisplay = gdk_display;
190       xwindow = gdk_root_window;
191     }
192
193   XDeleteProperty (xdisplay, xwindow, property);
194 }