Atoms.c revision e120bd27
1/*
2
3Copyright 1988, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24
25*/
26
27/*
28 * This file contains routines to cache atoms, avoiding multiple
29 * server round-trips.  Not so useful now that Xlib caches them.
30 *
31 * Public entry points:
32 *
33 *	XmuMakeAtom		creates & initializes an opaque AtomRec
34 *	XmuInternAtom		fetches an Atom from cache or Display
35 *	XmuInternStrings	fetches multiple Atoms as strings
36 *	XmuGetAtomName		returns name of an Atom
37 *	XmuNameOfAtom		returns name from an AtomPtr
38 */
39
40#ifdef HAVE_CONFIG_H
41#include <config.h>
42#endif
43#include <X11/Intrinsic.h>
44#include "Atoms.h"
45
46typedef struct _DisplayRec {
47    struct _DisplayRec* next;
48    Display *dpy;
49    Atom atom;
50} DisplayRec;
51
52struct _AtomRec {
53    char *name;
54    DisplayRec* head;
55};
56
57#ifdef SUNSHLIB
58#define STATIC
59#else
60#define STATIC static
61#endif
62
63#if !defined(UNIXCPP) || defined(ANSICPP)
64#define DeclareAtom(atom,text) \
65STATIC struct _AtomRec __##atom = { text, NULL }; \
66AtomPtr _##atom = &__##atom;
67#else
68#define DeclareAtom(atom,text) \
69STATIC struct _AtomRec __/**/atom = { text, NULL }; \
70AtomPtr _/**/atom = &__/**/atom;
71#endif
72
73DeclareAtom(XA_ATOM_PAIR,		"ATOM_PAIR"		)
74DeclareAtom(XA_CHARACTER_POSITION,	"CHARACTER_POSITION"	)
75DeclareAtom(XA_CLASS,			"CLASS"			)
76DeclareAtom(XA_CLIENT_WINDOW,		"CLIENT_WINDOW"		)
77DeclareAtom(XA_CLIPBOARD,		"CLIPBOARD"		)
78DeclareAtom(XA_COMPOUND_TEXT,		"COMPOUND_TEXT"		)
79DeclareAtom(XA_DECNET_ADDRESS,		"DECNET_ADDRESS"	)
80DeclareAtom(XA_DELETE,			"DELETE"		)
81DeclareAtom(XA_FILENAME,		"FILENAME"		)
82DeclareAtom(XA_HOSTNAME,		"HOSTNAME"		)
83DeclareAtom(XA_IP_ADDRESS,		"IP_ADDRESS"		)
84DeclareAtom(XA_LENGTH,			"LENGTH"		)
85DeclareAtom(XA_LIST_LENGTH,		"LIST_LENGTH"		)
86DeclareAtom(XA_NAME,			"NAME"			)
87DeclareAtom(XA_NET_ADDRESS,		"NET_ADDRESS"		)
88DeclareAtom(XA_NULL,			"NULL"			)
89DeclareAtom(XA_OWNER_OS,		"OWNER_OS"		)
90DeclareAtom(XA_SPAN,			"SPAN"			)
91DeclareAtom(XA_TARGETS,			"TARGETS"		)
92DeclareAtom(XA_TEXT,			"TEXT"			)
93DeclareAtom(XA_TIMESTAMP,		"TIMESTAMP"		)
94DeclareAtom(XA_USER,			"USER"			)
95DeclareAtom(XA_UTF8_STRING,		"UTF8_STRING"		)
96
97/******************************************************************
98
99  Public procedures
100
101 ******************************************************************/
102
103
104AtomPtr
105XmuMakeAtom(_Xconst char *name)
106{
107    AtomPtr ptr = XtNew(struct _AtomRec);
108    ptr->name = (char *) name;
109    ptr->head = NULL;
110    return ptr;
111}
112
113char *
114XmuNameOfAtom(AtomPtr atom_ptr)
115{
116    return atom_ptr->name;
117}
118
119
120Atom
121XmuInternAtom(Display *d, AtomPtr atom_ptr)
122{
123    DisplayRec* display_rec;
124    for (display_rec = atom_ptr->head; display_rec != NULL;
125	 display_rec = display_rec->next) {
126	if (display_rec->dpy == d)
127	    return display_rec->atom;
128    }
129    display_rec = XtNew(DisplayRec);
130    display_rec->next = atom_ptr->head;
131    atom_ptr->head = display_rec;
132    display_rec->dpy = d;
133    display_rec->atom = XInternAtom(d, atom_ptr->name, False);
134    return display_rec->atom;
135}
136
137
138char *
139XmuGetAtomName(Display *d, Atom atom)
140{
141    if (atom == 0) return (NULL);
142    return XGetAtomName(d, atom);
143}
144
145/* convert (names, count) to a list of atoms. Caller allocates list */
146void
147XmuInternStrings(Display *d, register String *names,
148		 register Cardinal count, register Atom *atoms)
149{
150    (void) XInternAtoms(d, (char**)names, (int)count, FALSE, atoms);
151}
152