chiark / gitweb /
New program to display messages and get answers.
[xtoys] / xatom.c
1 /* -*-c-*-
2  *
3  * $Id: xatom.c,v 1.1 1999/08/20 07:28:44 mdw Exp $
4  *
5  * Set and fetch X atom properties
6  *
7  * (c) 1999 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of the Edgeware X tools collection.
13  *
14  * X tools is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  * 
19  * X tools is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * 
24  * You should have received a copy of the GNU General Public License
25  * along with X tools; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 /*----- Revision history --------------------------------------------------* 
30  *
31  * $Log: xatom.c,v $
32  * Revision 1.1  1999/08/20 07:28:44  mdw
33  * New source file for manipulating atom-valued window properties.
34  *
35  */
36
37 /*----- Header files ------------------------------------------------------*/
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #include <X11/Xatom.h>
44 #include <X11/Xlib.h>
45 #include <X11/Xutil.h>
46
47 /*----- Main code ---------------------------------------------------------*/
48
49 /* --- @xatom_set@ --- *
50  *
51  * Arguments:   @Display *d@ = pointer to display
52  *              @Window w@ = window to set
53  *              @Atom p@ = property to set
54  *              @Atom a@ = atom property value
55  *
56  * Returns:     ---
57  *
58  * Use:         Sets an atom property on a particular window.
59  */
60
61 void xatom_set(Display *d, Window w, Atom p, Atom a)
62 {
63   XChangeProperty(d, w, p, XA_ATOM, 32, PropModeReplace,
64                   (unsigned char *)&a, 1);
65 }
66
67 /* --- @xatom_get@ --- *
68  *
69  * Arguments:   @Display *d@ = pointer to display
70  *              @Window w@ = window to set
71  *              @Atom p@ = property to read
72  *
73  * Returns:     Atom which is the value of the property.
74  *
75  * Use:         Reads an atom property from a particular window.  The value
76  *              @None@ is returned if there is no atom value.
77  */
78
79 Atom xatom_get(Display *d, Window w, Atom p)
80 {
81   Atom type, v;
82   unsigned long n, left;
83   int fmt;
84   unsigned char *buf;
85
86   /* --- Fetch the property value --- */
87
88   if (XGetWindowProperty(d, w, p,       /* Display, window, property */
89                          0, 64,         /* Offset, length (both in words) */
90                          False,         /* Delete after return? */
91                          XA_ATOM,       /* Data format type */
92                          &type, &fmt,   /* Actual type and format */
93                          &n, &left,     /* Amount read, and bytes left */
94                          &buf)          /* Where to put the buffer */
95         != Success ||
96       type != XA_ATOM ||
97       n < 1 || fmt < 32)
98     return (None);
99
100   /* --- OK, get the atom and return --- *
101    *
102    * This assumes that atoms are 32-bit things.  This may not be the case.
103    * That's a right pain, actually.  It looks as if Xlib is trying to do the
104    * right thing, so I'll go with that rather than trying to do anything
105    * clever.  This is actually a bit of a poor interface.
106    */
107
108   v = *(Atom *)buf;
109   XFree(buf);
110   return (v);
111 }
112
113 /*----- That's all, folks -------------------------------------------------*/