18c9fbc29Smrg/************************************************************
28c9fbc29Smrg Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
38c9fbc29Smrg
48c9fbc29Smrg Permission to use, copy, modify, and distribute this
58c9fbc29Smrg software and its documentation for any purpose and without
68c9fbc29Smrg fee is hereby granted, provided that the above copyright
78c9fbc29Smrg notice appear in all copies and that both that copyright
88c9fbc29Smrg notice and this permission notice appear in supporting
94cd6a3aeSmrg documentation, and that the name of Silicon Graphics not be
104cd6a3aeSmrg used in advertising or publicity pertaining to distribution
118c9fbc29Smrg of the software without specific prior written permission.
124cd6a3aeSmrg Silicon Graphics makes no representation about the suitability
138c9fbc29Smrg of this software for any purpose. It is provided "as is"
148c9fbc29Smrg without any express or implied warranty.
154cd6a3aeSmrg
164cd6a3aeSmrg SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
174cd6a3aeSmrg SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
188c9fbc29Smrg AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
194cd6a3aeSmrg GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
204cd6a3aeSmrg DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
214cd6a3aeSmrg DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
228c9fbc29Smrg OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
238c9fbc29Smrg THE USE OR PERFORMANCE OF THIS SOFTWARE.
248c9fbc29Smrg
258c9fbc29Smrg ********************************************************/
268c9fbc29Smrg
278c9fbc29Smrg#ifdef HAVE_CONFIG_H
288c9fbc29Smrg#include <config.h>
298c9fbc29Smrg#endif
308c9fbc29Smrg#include <stdio.h>
318c9fbc29Smrg#include <ctype.h>
328c9fbc29Smrg#include <stdlib.h>
338c9fbc29Smrg#include <X11/Xlib.h>
348c9fbc29Smrg#include <X11/XKBlib.h>
358c9fbc29Smrg#include <X11/extensions/XKBgeom.h>
368c9fbc29Smrg
378c9fbc29Smrg#include "XKMformat.h"
388c9fbc29Smrg#include "XKBfileInt.h"
398c9fbc29Smrg
408c9fbc29SmrgBool
418c9fbc29SmrgXkbWriteToServer(XkbFileInfo *result)
428c9fbc29Smrg{
4370728a38Smrg    XkbDescPtr xkb;
4470728a38Smrg    Display *dpy;
4570728a38Smrg
4670728a38Smrg    if ((result == NULL) || (result->xkb == NULL) || (result->xkb->dpy == NULL))
4770728a38Smrg        return False;
4870728a38Smrg    xkb = result->xkb;
4970728a38Smrg    dpy = xkb->dpy;
5070728a38Smrg    if (!XkbSetMap(dpy, XkbAllMapComponentsMask, xkb))
5170728a38Smrg        return False;
5270728a38Smrg    if (!XkbSetIndicatorMap(dpy, ~0, xkb))
5370728a38Smrg        return False;
5470728a38Smrg    if (!XkbSetCompatMap(dpy, XkbAllCompatMask, xkb, True))
5570728a38Smrg        return False;
5670728a38Smrg    if (!XkbSetNames(dpy, XkbAllNamesMask, 0, xkb->map->num_types, xkb))
5770728a38Smrg        return False;
588c9fbc29Smrg    if (xkb->geom) {
5970728a38Smrg        if (XkbSetGeometry(dpy, xkb->device_spec, xkb->geom) != Success)
6070728a38Smrg            return False;
618c9fbc29Smrg    }
628c9fbc29Smrg    return True;
638c9fbc29Smrg}
648c9fbc29Smrg
658c9fbc29Smrgunsigned
6670728a38SmrgXkbReadFromServer(Display *dpy, unsigned need, unsigned want,
6770728a38Smrg                  XkbFileInfo *result)
688c9fbc29Smrg{
6970728a38Smrg    unsigned which = need | want;
7070728a38Smrg    unsigned tmp = 0;
7170728a38Smrg
7270728a38Smrg    if ((result == NULL) || (dpy == NULL))
7370728a38Smrg        return which;
7470728a38Smrg
7570728a38Smrg    if (which & XkmSymbolsMask)
7670728a38Smrg        tmp = XkbAllMapComponentsMask;
7770728a38Smrg    else if (which & XkmTypesMask)
7870728a38Smrg        tmp = XkbKeyTypesMask;
7970728a38Smrg    if (result->xkb == NULL) {
8070728a38Smrg        result->xkb = XkbGetMap(dpy, tmp, XkbUseCoreKbd);
8170728a38Smrg        if (!result->xkb)
8270728a38Smrg            return which;
8370728a38Smrg        else
8470728a38Smrg            which &= ~(XkmSymbolsMask | XkmTypesMask | XkmVirtualModsMask);
858c9fbc29Smrg    }
8670728a38Smrg    else if ((tmp) && (XkbGetUpdatedMap(dpy, tmp, result->xkb) == Success))
8770728a38Smrg        which &= ~(XkmSymbolsMask | XkmTypesMask | XkmVirtualModsMask);
888c9fbc29Smrg
8970728a38Smrg    if (which & XkmIndicatorsMask) {
9070728a38Smrg        if (XkbGetIndicatorMap(dpy, XkbAllIndicatorsMask, result->xkb) ==
9170728a38Smrg            Success)
9270728a38Smrg            which &= ~XkmIndicatorsMask;
938c9fbc29Smrg    }
948c9fbc29Smrg
9570728a38Smrg    if (which & XkmCompatMapMask) {
9670728a38Smrg        if (XkbGetCompatMap(dpy, XkbAllCompatMask, result->xkb) == Success)
9770728a38Smrg            which &= ~XkmCompatMapMask;
988c9fbc29Smrg    }
9970728a38Smrg    if (which & XkmGeometryMask) {
10070728a38Smrg        if (XkbGetGeometry(dpy, result->xkb) == Success)
10170728a38Smrg            which &= ~XkmGeometryMask;
1028c9fbc29Smrg    }
10370728a38Smrg    XkbGetNames(dpy, XkbAllNamesMask, result->xkb);
1048c9fbc29Smrg    return which;
1058c9fbc29Smrg}
1068c9fbc29Smrg
1078c9fbc29SmrgStatus
10870728a38SmrgXkbChangeKbdDisplay(Display *newDpy, XkbFileInfo *result)
1098c9fbc29Smrg{
11070728a38Smrg    register int i;
11170728a38Smrg    XkbDescPtr xkb;
11270728a38Smrg    Display *oldDpy;
11370728a38Smrg    Atom *atm;
11470728a38Smrg
11570728a38Smrg    if ((result->xkb == NULL) || (result->xkb->dpy == newDpy))
11670728a38Smrg        return Success;
11770728a38Smrg    xkb = result->xkb;
11870728a38Smrg    oldDpy = xkb->dpy;
1198c9fbc29Smrg    if (xkb->names) {
12070728a38Smrg        XkbNamesPtr names = xkb->names;
12170728a38Smrg
12270728a38Smrg        names->keycodes = XkbChangeAtomDisplay(oldDpy, newDpy, names->keycodes);
12370728a38Smrg        names->geometry = XkbChangeAtomDisplay(oldDpy, newDpy, names->geometry);
12470728a38Smrg        names->symbols = XkbChangeAtomDisplay(oldDpy, newDpy, names->symbols);
12570728a38Smrg        names->types = XkbChangeAtomDisplay(oldDpy, newDpy, names->types);
12670728a38Smrg        names->compat = XkbChangeAtomDisplay(oldDpy, newDpy, names->compat);
12770728a38Smrg        names->phys_symbols = XkbChangeAtomDisplay(oldDpy, newDpy,
12870728a38Smrg                                                   names->phys_symbols);
12970728a38Smrg        for (i = 0, atm = names->vmods; i < XkbNumVirtualMods; i++, atm++) {
13070728a38Smrg            *atm = XkbChangeAtomDisplay(oldDpy, newDpy, *atm);
13170728a38Smrg        }
13270728a38Smrg        for (i = 0, atm = names->indicators; i < XkbNumIndicators; i++, atm++) {
13370728a38Smrg            *atm = XkbChangeAtomDisplay(oldDpy, newDpy, *atm);
13470728a38Smrg        }
13570728a38Smrg        for (i = 0, atm = names->groups; i < XkbNumKbdGroups; i++, atm++) {
13670728a38Smrg            *atm = XkbChangeAtomDisplay(oldDpy, newDpy, *atm);
13770728a38Smrg        }
13870728a38Smrg        for (i = 0, atm = names->radio_groups; i < names->num_rg; i++, atm++) {
13970728a38Smrg            *atm = XkbChangeAtomDisplay(oldDpy, newDpy, *atm);
14070728a38Smrg        }
1418c9fbc29Smrg    }
1428c9fbc29Smrg    if (xkb->map) {
14370728a38Smrg        register int t;
14470728a38Smrg        XkbKeyTypePtr type;
14570728a38Smrg
14670728a38Smrg        for (t = 0, type = xkb->map->types; t < xkb->map->num_types;
14770728a38Smrg             t++, type++) {
14870728a38Smrg            type->name = XkbChangeAtomDisplay(oldDpy, newDpy, type->name);
14970728a38Smrg            if (type->level_names != NULL) {
15070728a38Smrg                for (i = 0, atm = type->level_names; i < type->num_levels;
15170728a38Smrg                     i++, atm++) {
15270728a38Smrg                    *atm = XkbChangeAtomDisplay(oldDpy, newDpy, *atm);
15370728a38Smrg                }
15470728a38Smrg            }
15570728a38Smrg        }
1568c9fbc29Smrg    }
1578c9fbc29Smrg    if (xkb->geom) {
15870728a38Smrg        XkbGeometryPtr geom = xkb->geom;
15970728a38Smrg
16070728a38Smrg        geom->name = XkbChangeAtomDisplay(oldDpy, newDpy, geom->name);
16170728a38Smrg        if (geom->shapes) {
16270728a38Smrg            register int s;
16370728a38Smrg            XkbShapePtr shape;
16470728a38Smrg
16570728a38Smrg            for (s = 0, shape = geom->shapes; s < geom->num_shapes;
16670728a38Smrg                 s++, shape++) {
16770728a38Smrg                shape->name = XkbChangeAtomDisplay(oldDpy, newDpy, shape->name);
16870728a38Smrg            }
16970728a38Smrg        }
17070728a38Smrg        if (geom->sections) {
17170728a38Smrg            register int s;
17270728a38Smrg            XkbSectionPtr section;
17370728a38Smrg
17470728a38Smrg            for (s = 0, section = geom->sections; s < geom->num_sections;
17570728a38Smrg                 s++, section++) {
17670728a38Smrg                section->name =
17770728a38Smrg                    XkbChangeAtomDisplay(oldDpy, newDpy, section->name);
17870728a38Smrg                if (section->doodads) {
17970728a38Smrg                    register int d;
18070728a38Smrg                    XkbDoodadPtr doodad;
18170728a38Smrg
18270728a38Smrg                    for (d = 0, doodad = section->doodads;
18370728a38Smrg                         d < section->num_doodads; d++, doodad++) {
18470728a38Smrg                        doodad->any.name =
18570728a38Smrg                            XkbChangeAtomDisplay(oldDpy, newDpy,
18670728a38Smrg                                                 doodad->any.name);
18770728a38Smrg                    }
18870728a38Smrg                }
18970728a38Smrg                if (section->overlays) {
19070728a38Smrg                    register int o;
19170728a38Smrg                    register XkbOverlayPtr ol;
19270728a38Smrg
19370728a38Smrg                    for (o = 0, ol = section->overlays;
19470728a38Smrg                         o < section->num_overlays; o++, ol++) {
19570728a38Smrg                        ol->name =
19670728a38Smrg                            XkbChangeAtomDisplay(oldDpy, newDpy, ol->name);
19770728a38Smrg                    }
19870728a38Smrg                }
19970728a38Smrg            }
20070728a38Smrg        }
20170728a38Smrg        if (geom->doodads) {
20270728a38Smrg            register int d;
20370728a38Smrg            XkbDoodadPtr doodad;
20470728a38Smrg
20570728a38Smrg            for (d = 0, doodad = geom->doodads; d < geom->num_doodads;
20670728a38Smrg                 d++, doodad++) {
20770728a38Smrg                doodad->any.name =
20870728a38Smrg                    XkbChangeAtomDisplay(oldDpy, newDpy, doodad->any.name);
20970728a38Smrg            }
21070728a38Smrg        }
2118c9fbc29Smrg    }
21270728a38Smrg    xkb->dpy = newDpy;
2138c9fbc29Smrg    return Success;
2148c9fbc29Smrg}
215