srvmisc.c revision 54cef2dd
1/************************************************************
2 Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3
4 Permission to use, copy, modify, and distribute this
5 software and its documentation for any purpose and without
6 fee is hereby granted, provided that the above copyright
7 notice appear in all copies and that both that copyright
8 notice and this permission notice appear in supporting
9 documentation, and that the name of Silicon Graphics not be
10 used in advertising or publicity pertaining to distribution
11 of the software without specific prior written permission.
12 Silicon Graphics makes no representation about the suitability
13 of this software for any purpose. It is provided "as is"
14 without any express or implied warranty.
15
16 SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17 SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18 AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
23 THE USE OR PERFORMANCE OF THIS SOFTWARE.
24
25 ********************************************************/
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30#include <stdio.h>
31#include <ctype.h>
32#include <stdlib.h>
33#include <X11/Xlib.h>
34#include <X11/XKBlib.h>
35#include <X11/extensions/XKBgeom.h>
36
37#include "XKMformat.h"
38#include "XKBfileInt.h"
39
40Bool
41XkbWriteToServer(XkbFileInfo *result)
42{
43    XkbDescPtr xkb;
44    Display *dpy;
45
46    if ((result == NULL) || (result->xkb == NULL) || (result->xkb->dpy == NULL))
47        return False;
48    xkb = result->xkb;
49    dpy = xkb->dpy;
50    if (!XkbSetMap(dpy, XkbAllMapComponentsMask, xkb))
51        return False;
52    if (!XkbSetIndicatorMap(dpy, ~0, xkb))
53        return False;
54    if (!XkbSetCompatMap(dpy, XkbAllCompatMask, xkb, True))
55        return False;
56    if (!XkbSetNames(dpy, XkbAllNamesMask, 0, xkb->map->num_types, xkb))
57        return False;
58    if (xkb->geom) {
59        if (XkbSetGeometry(dpy, xkb->device_spec, xkb->geom) != Success)
60            return False;
61    }
62    return True;
63}
64
65unsigned
66XkbReadFromServer(Display *dpy, unsigned need, unsigned want,
67                  XkbFileInfo *result)
68{
69    unsigned which = need | want;
70    unsigned tmp = 0;
71
72    if ((result == NULL) || (dpy == NULL))
73        return which;
74
75    if (which & XkmSymbolsMask)
76        tmp = XkbAllMapComponentsMask;
77    else if (which & XkmTypesMask)
78        tmp = XkbKeyTypesMask;
79    if (result->xkb == NULL) {
80        result->xkb = XkbGetMap(dpy, tmp, XkbUseCoreKbd);
81        if (!result->xkb)
82            return which;
83        else
84            which &= ~(XkmSymbolsMask | XkmTypesMask | XkmVirtualModsMask);
85    }
86    else if ((tmp) && (XkbGetUpdatedMap(dpy, tmp, result->xkb) == Success))
87        which &= ~(XkmSymbolsMask | XkmTypesMask | XkmVirtualModsMask);
88
89    if (which & XkmIndicatorsMask) {
90        if (XkbGetIndicatorMap(dpy, XkbAllIndicatorsMask, result->xkb) ==
91            Success)
92            which &= ~XkmIndicatorsMask;
93    }
94
95    if (which & XkmCompatMapMask) {
96        if (XkbGetCompatMap(dpy, XkbAllCompatMask, result->xkb) == Success)
97            which &= ~XkmCompatMapMask;
98    }
99    if (which & XkmGeometryMask) {
100        if (XkbGetGeometry(dpy, result->xkb) == Success)
101            which &= ~XkmGeometryMask;
102    }
103    XkbGetNames(dpy, XkbAllNamesMask, result->xkb);
104    return which;
105}
106
107Status
108XkbChangeKbdDisplay(Display *newDpy, XkbFileInfo *result)
109{
110    register int i;
111    XkbDescPtr xkb;
112    Display *oldDpy;
113    Atom *atm;
114
115    if ((result->xkb == NULL) || (result->xkb->dpy == newDpy))
116        return Success;
117    xkb = result->xkb;
118    oldDpy = xkb->dpy;
119    if (xkb->names) {
120        XkbNamesPtr names = xkb->names;
121
122        names->keycodes = XkbChangeAtomDisplay(oldDpy, newDpy, names->keycodes);
123        names->geometry = XkbChangeAtomDisplay(oldDpy, newDpy, names->geometry);
124        names->symbols = XkbChangeAtomDisplay(oldDpy, newDpy, names->symbols);
125        names->types = XkbChangeAtomDisplay(oldDpy, newDpy, names->types);
126        names->compat = XkbChangeAtomDisplay(oldDpy, newDpy, names->compat);
127        names->phys_symbols = XkbChangeAtomDisplay(oldDpy, newDpy,
128                                                   names->phys_symbols);
129        for (i = 0, atm = names->vmods; i < XkbNumVirtualMods; i++, atm++) {
130            *atm = XkbChangeAtomDisplay(oldDpy, newDpy, *atm);
131        }
132        for (i = 0, atm = names->indicators; i < XkbNumIndicators; i++, atm++) {
133            *atm = XkbChangeAtomDisplay(oldDpy, newDpy, *atm);
134        }
135        for (i = 0, atm = names->groups; i < XkbNumKbdGroups; i++, atm++) {
136            *atm = XkbChangeAtomDisplay(oldDpy, newDpy, *atm);
137        }
138        for (i = 0, atm = names->radio_groups; i < names->num_rg; i++, atm++) {
139            *atm = XkbChangeAtomDisplay(oldDpy, newDpy, *atm);
140        }
141    }
142    if (xkb->map) {
143        register int t;
144        XkbKeyTypePtr type;
145
146        for (t = 0, type = xkb->map->types; t < xkb->map->num_types;
147             t++, type++) {
148            type->name = XkbChangeAtomDisplay(oldDpy, newDpy, type->name);
149            if (type->level_names != NULL) {
150                for (i = 0, atm = type->level_names; i < type->num_levels;
151                     i++, atm++) {
152                    *atm = XkbChangeAtomDisplay(oldDpy, newDpy, *atm);
153                }
154            }
155        }
156    }
157    if (xkb->geom) {
158        XkbGeometryPtr geom = xkb->geom;
159
160        geom->name = XkbChangeAtomDisplay(oldDpy, newDpy, geom->name);
161        if (geom->shapes) {
162            register int s;
163            XkbShapePtr shape;
164
165            for (s = 0, shape = geom->shapes; s < geom->num_shapes;
166                 s++, shape++) {
167                shape->name = XkbChangeAtomDisplay(oldDpy, newDpy, shape->name);
168            }
169        }
170        if (geom->sections) {
171            register int s;
172            XkbSectionPtr section;
173
174            for (s = 0, section = geom->sections; s < geom->num_sections;
175                 s++, section++) {
176                section->name =
177                    XkbChangeAtomDisplay(oldDpy, newDpy, section->name);
178                if (section->doodads) {
179                    register int d;
180                    XkbDoodadPtr doodad;
181
182                    for (d = 0, doodad = section->doodads;
183                         d < section->num_doodads; d++, doodad++) {
184                        doodad->any.name =
185                            XkbChangeAtomDisplay(oldDpy, newDpy,
186                                                 doodad->any.name);
187                    }
188                }
189                if (section->overlays) {
190                    register int o;
191                    register XkbOverlayPtr ol;
192
193                    for (o = 0, ol = section->overlays;
194                         o < section->num_overlays; o++, ol++) {
195                        ol->name =
196                            XkbChangeAtomDisplay(oldDpy, newDpy, ol->name);
197                    }
198                }
199            }
200        }
201        if (geom->doodads) {
202            register int d;
203            XkbDoodadPtr doodad;
204
205            for (d = 0, doodad = geom->doodads; d < geom->num_doodads;
206                 d++, doodad++) {
207                doodad->any.name =
208                    XkbChangeAtomDisplay(oldDpy, newDpy, doodad->any.name);
209            }
210        }
211    }
212    xkb->dpy = newDpy;
213    return Success;
214}
215