summaryrefslogtreecommitdiff
path: root/xc/lib/X11/XKBGetMap.c
blob: 12c4f1f75974e497f2e4708a5d37efb68f67a620 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/* $XConsortium: XKB.c,v 1.11 94/02/05 18:13:51 rws Exp $ *
/************************************************************
Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.

Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies and that both that copyright
notice and this permission notice appear in supporting
documentation, and that the name of Silicon Graphics not be 
used in advertising or publicity pertaining to distribution 
of the software without specific prior written permission.
Silicon Graphics makes no representation about the suitability 
of this software for any purpose. It is provided "as is"
without any express or implied warranty.

SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS 
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 
AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL 
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 
DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
THE USE OR PERFORMANCE OF THIS SOFTWARE.

********************************************************/

#include <stdio.h>
#define NEED_REPLIES
#define NEED_EVENTS
#include "Xlibint.h"
#include <X11/extensions/XKBproto.h>
#include "XKBlibint.h"

/***====================================================================***/

Status
XkbAllocClientMap(xkb,which,nTypes)
    XkbDescPtr		xkb;
    unsigned		which;
    unsigned		nTypes;
{
register int	i;
XkbClientMapPtr map;

    if ((xkb==NULL)||((nTypes>0)&&(nTypes<XkbNumRequiredTypes)))
	return False;
    if ((which&XkbKeySymsMask)&&
	((xkb->min_key_code<8)||(xkb->max_key_code<xkb->min_key_code)))
	return False;

    if (xkb->map==NULL) {
	map= (XkbClientMapPtr)Xcalloc(1,sizeof(XkbClientMapRec));
	if (map==NULL)
	    return False;
	xkb->map= map;
    }
    else map= xkb->map;

    if ((which&XkbKeyTypesMask)&&(nTypes>0)) {
	if (map->types==NULL) {
	    map->types= (XkbKeyTypePtr)Xcalloc(nTypes,sizeof(XkbKeyTypeRec));
	    if (map->types==NULL)
		return False;
	    map->num_types= 0;
	    /* 3/21/94 (ef) -- XXX! should create canonical types here */
	    map->size_types= nTypes;
	}
	else if (map->size_types<nTypes) {
	    map->types= (XkbKeyTypePtr)Xrealloc(map->types,
						nTypes*sizeof(XkbKeyTypeRec));
	    if (map->types==NULL) {
		map->num_types= map->size_types= 0;
		return False;
	    }
	    map->size_types= nTypes;
	    bzero(&map->types[map->num_types], 
		  ((map->size_types-map->num_types)*sizeof(XkbKeyTypeRec)));
	}
    }
    if (which&XkbKeySymsMask) {
	if (map->syms==NULL) {
	    map->syms= (KeySym *)Xcalloc(100,sizeof(KeySym));
	    if (!map->syms)
		return False;
	    map->size_syms= 100;
	    map->num_syms= 1;
	    map->syms[0]= NoSymbol;
	}
	if (map->key_sym_map==NULL) {
	    i= xkb->max_key_code+1;
	    map->key_sym_map=(XkbSymMapPtr)Xcalloc(i,sizeof(XkbSymMapRec));
	    if (map->key_sym_map==NULL)
		return False;
	    /* default is no groups, symbol at offset 0 */
	}
    }
    return True;
}

Status
XkbAllocServerMap(xkb,which,nActions)
    XkbDescPtr		xkb;
    unsigned		which;
    unsigned		nActions;
{
register int	i;
XkbServerMapPtr map;

    if (xkb==NULL)
	return False;
    if (xkb->server==NULL) {
	map= (XkbServerMapPtr)Xcalloc(1,sizeof(XkbServerMapRec));
	if (map==NULL)
	    return False;
	for (i=0;i<XkbNumVirtualMods;i++) {
	    map->vmods[i]= XkbNoModifier;
	}
	xkb->server= map;
    }
    else map= xkb->server;
    if ((which&XkbKeyActionsMask)&&(nActions>0)) {
	if ((xkb->min_key_code<8)||(xkb->max_key_code<xkb->min_key_code))
	    return False;
	if (map->acts==NULL) {
	    map->acts= (XkbAction *)Xcalloc((nActions+1),sizeof(XkbAction));
	    if (map->acts==NULL)
		return False;
	    map->num_acts= 1;
	    map->size_acts= nActions+1;
	}
	else if ((map->size_acts-map->num_acts)<nActions) {
	    unsigned need;
	    need= map->num_acts+nActions;
	    map->acts= (XkbAction *)Xrealloc(map->acts,
						need*sizeof(XkbAction));
	    if (map->acts==NULL) {
		map->num_acts= map->size_acts= 0;
		return False;
	    }
	    map->size_acts= need;
	    bzero(&map->acts[map->num_acts], 
		  ((map->size_acts-map->num_acts)*sizeof(XkbAction)));
	}
	if (map->key_acts==NULL) {
	    i= xkb->max_key_code+1;
	    map->key_acts=(unsigned short*)Xcalloc(i,sizeof(unsigned short));
	    if (map->key_acts==NULL)
		return False;
	}
    }
    if (which&XkbKeyBehaviorsMask) {
	if ((xkb->min_key_code<8)||(xkb->max_key_code<xkb->min_key_code))
	    return False;
	if (map->behaviors==NULL) {
	    i= xkb->max_key_code+1;
	    map->behaviors= (XkbBehavior *)Xcalloc(i,sizeof(XkbBehavior));
	    if (map->behaviors==NULL)
		return False;
	}
    }
    if (which&XkbExplicitComponentsMask) {
	if ((xkb->min_key_code<8)||(xkb->max_key_code<xkb->min_key_code))
	    return False;
	if (map->explicit==NULL) {
	    i= xkb->max_key_code+1;
	    map->explicit= (unsigned char *)Xcalloc(i,sizeof(unsigned char));
	    if (map->explicit==NULL)
		return False;
	}
    }
    return True;
}

static Bool
_XkbInitServerMap(xkb)
    XkbDescRec *xkb;
{
int	size;
XkbServerMapRec *map;

    if ((!xkb)||(xkb->server)||(xkb->max_key_code<8))
	return True;
    size= sizeof(XkbServerMapRec)+((xkb->max_key_code+1)*sizeof(XkbBehavior));
    size+= ((xkb->max_key_code+1)*sizeof(unsigned short));/* key acts */
    size+= ((xkb->max_key_code+1)*sizeof(unsigned char)); /* explicit info */
    xkb->server= map=(XkbServerMapRec *)Xmalloc(size);
    if (map) {
	register int i;
	bzero((char *)&map[1],size-sizeof(XkbServerMapRec));
	map->num_acts = 1;
	map->size_acts = 32;
	map->acts= (XkbAction *)Xmalloc(map->size_acts*sizeof(XkbAction));
	map->acts[0].type = XkbSA_NoAction;

	map->behaviors= (XkbBehavior *)&map[1];
	map->key_acts= (unsigned short *)&map->behaviors[xkb->max_key_code];
	map->explicit= (unsigned char *)&map->key_acts[xkb->max_key_code];
	for (i=0;i<XkbNumVirtualMods;i++) {
	    map->vmods[i]= XkbNoModifier;
	}
	return True;
    }
    return False;
}

/***====================================================================***/

static Bool
_XkbReadKeyTypes(dpy,buf,xkb,rep,replace)
    Display *		dpy;
    XkbReadBufferPtr	buf;
    XkbDescPtr 		xkb;
    xkbGetMapReply *	rep;
    int 		replace;
{
int		 i,n,last;
XkbKeyTypeRec	*map;

    if ( rep->nTypes>0 ) {
	last = rep->firstType+rep->nTypes-1;
	if (!XkbAllocClientMap(xkb,XkbKeyTypesMask,rep->totalTypes))
	    return False;

	map = &xkb->map->types[rep->firstType];
	for (i=0;i<(int)rep->nTypes;i++,map++) {
	    xkbKeyTypeWireDesc	*desc;
	    register int ndx;

	    ndx= i+rep->firstType;
	    if (ndx>=xkb->map->num_types)
		xkb->map->num_types= ndx+1;

	    desc= (xkbKeyTypeWireDesc *)_XkbGetReadBufferPtr(buf,
						SIZEOF(xkbKeyTypeWireDesc));
	    if (desc==NULL)
		return False;

	    if ( desc->nMapEntries>0 ) {
		if ((map->map!=NULL)&&(map->free&XkbNoFreeKTMap)) {
		    map->map_count= 0;
		    map->map= NULL;
		    map->free&= ~XkbNoFreeKTMap;
		}
		if ((map->map==NULL)||(desc->nMapEntries>map->map_count)){
		    register int size;
		    size= sizeof(XkbKTMapEntryRec)*desc->nMapEntries;
		    if ( map->map )
			 map->map= (XkbKTMapEntryPtr)Xrealloc(map->map,size);
		    else map->map = (XkbKTMapEntryPtr)Xmalloc(size);
		    if (map->map==NULL)
			return False;
		}
	    }
	    else if (map->map!=NULL) {
		Xfree(map->map);
		map->map_count= 0;
		map->map= NULL;
	    }

	    if ( desc->preserve ) {
		if ((map->preserve!=NULL)&&(map->free&XkbNoFreeKTPreserve)) {
		    map->preserve= NULL;
		    map->free&= ~XkbNoFreeKTPreserve;
		}
		if ((!map->preserve)||
		    (desc->nMapEntries>map->map_count)) {
		    register int size;
		    size= sizeof(XkbKTPreserveRec)*desc->nMapEntries;
		    if ( map->preserve )
			 map->preserve= (XkbKTPreservePtr)
					Xrealloc(map->preserve,size);
		    else map->preserve= (XkbKTPreservePtr)Xmalloc(size);
		    if (map->preserve==NULL)
			return False;
		}
	    }
	    else if (map->preserve!=NULL) {
		Xfree(map->preserve);
		map->preserve= NULL;
	    }

	    map->mask = desc->mask;
	    map->real_mods = desc->realMods;
	    map->vmods = desc->virtualMods;
	    map->group_width = desc->groupWidth;
	    map->free = 0;
	    map->map_count = desc->nMapEntries;
	    if (desc->nMapEntries>0) {
		register xkbKTMapEntryWireDesc *wire; 
		register XkbKTMapEntryPtr	entry;
		register int size;

		size= map->map_count*SIZEOF(xkbKTMapEntryWireDesc);
		wire= (xkbKTMapEntryWireDesc *)_XkbGetReadBufferPtr(buf,size);
		if (wire==NULL)
		    return False;
		entry= map->map;
		for (n=0;n<map->map_count;n++,wire++,entry++) {
		    entry->active= wire->active;
		    entry->mask= wire->mask;
		    entry->level= wire->level;
		    entry->real_mods= wire->realMods;
		    entry->vmods= wire->virtualMods;
		}

	 	if (desc->preserve) {
		    register xkbKTPreserveWireDesc *	pwire; 
		    register XkbKTPreservePtr		preserve;
		    register int sz;

		    sz= desc->nMapEntries*SIZEOF(xkbKTPreserveWireDesc);
		    pwire=(xkbKTPreserveWireDesc *)_XkbGetReadBufferPtr(buf,sz);
		    if (pwire==NULL)
			return False;
		    preserve= map->preserve;
		    for (n=0;n<desc->nMapEntries;n++,pwire++,preserve++) {
			preserve->mask=  pwire->mask;
			preserve->vmods=  pwire->virtualMods;
			preserve->real_mods= pwire->realMods;
		    }
		}
	    }
	}
    }
    return True;
}

static Bool
_XkbReadKeySyms(dpy,buf,xkb,rep,replace)
    Display *		dpy;
    XkbReadBufferPtr	buf;
    XkbDescPtr 		xkb;
    xkbGetMapReply *	rep;
    int 		replace;
{
register int i;
XkbClientMapPtr	map;

    map= xkb->map;
    if (map->key_sym_map==NULL) {
	register int offset;
	XkbSymMapRec *oldMap;
	xkbSymMapWireDesc *newMap;
	map->key_sym_map= (XkbSymMapRec *)Xmalloc((xkb->max_key_code+1)*
						   sizeof(XkbSymMapRec));
	if (map->key_sym_map==NULL)
	    return False;
	if (map->syms==NULL) {
	    int sz;
	    sz= (rep->totalSyms*12)/10;
	    sz= ((sz+(unsigned)128)/128)*128;
	    map->syms = (KeySym *)Xmalloc(sz*sizeof(KeySym));
	    if (map->syms==NULL)
		return False;
	    map->size_syms = sz;
	}
	offset = 1;
	oldMap = &map->key_sym_map[rep->firstKeySym];
	for (i=0;i<(int)rep->nKeySyms;i++,oldMap++) {
	    register int tmp;
	    newMap= (xkbSymMapWireDesc *)
			_XkbGetReadBufferPtr(buf,SIZEOF(xkbSymMapWireDesc));
	    oldMap->kt_index= newMap->ktIndex;
	    oldMap->group_info= newMap->groupInfo;
	    oldMap->offset= offset;
	    if (offset+newMap->nSyms>=map->size_syms) {
		register int sz;
		sz= map->size_syms+128;
		map->syms= (KeySym *)Xrealloc(map->syms,sz*sizeof(KeySym));
		if (map->syms==NULL) {
		    map->size_syms= 0;
		    return False;
		}
		map->size_syms= sz;
	    }
	    if (newMap->nSyms>0) {
		_XkbReadBufferCopy32(buf,(long *)&map->syms[offset],
							newMap->nSyms);
		offset+= newMap->nSyms;
	    }
	    else {
		map->syms[offset]= 0;
	    }
	}
	map->num_syms= offset;
    }
    else {
	xkbSymMapWireDesc *newMap;
	XkbSymMapRec *oldMap;
	KeySym *newSyms;
	int	tmp;

	oldMap = &map->key_sym_map[rep->firstKeySym];
	for (i=0;i<(int)rep->nKeySyms;i++,oldMap++) {
	    newMap= (xkbSymMapWireDesc *)
			_XkbGetReadBufferPtr(buf,SIZEOF(xkbSymMapWireDesc));
	    if (newMap->nSyms>0)
		 tmp= newMap->nSyms;
	    else tmp= 1;

	    newSyms = XkbEnlargeKeySyms(xkb,i+rep->firstKeySym,tmp);
	    if (newMap->nSyms>0)
		_XkbReadBufferCopy32(buf,(long *)newSyms,newMap->nSyms);
	    else newSyms[0]= NoSymbol;
	    oldMap->kt_index = newMap->ktIndex;
	    oldMap->group_info = newMap->groupInfo;
	}
    }
    return True;
}

static Bool
_XkbReadKeyActions(dpy,buf,info,rep,replace)
    Display *		dpy;
    XkbDescPtr		info;
    XkbReadBufferPtr	buf;
    xkbGetMapReply *	rep;
    int 		replace;
{
int		 i,last;
CARD8		 numDesc[248];
XkbAction	*actDesc;

    if ( rep->nKeyActions>0 ) {
	XkbSymMapRec *symMap;
	_XkbCopyFromReadBuffer(buf, (char *)numDesc, rep->nKeyActions);
	i= XkbPaddedSize(rep->nKeyActions)-rep->nKeyActions;
	if (i>0)
	    _XkbSkipReadBufferData(buf,i);
	symMap = &info->map->key_sym_map[rep->firstKeyAction];
	for (i=0;i<(int)rep->nKeyActions;i++,symMap++) {
	    if (numDesc[i]==0) {
		info->server->key_acts[i+rep->firstKeyAction]= 0;
	    }
	    else {
		XkbAction *newActs;
		/* 8/16/93 (ef) -- XXX! Verify size here (numdesc must be */
		/*                 either zero or XkbKeyNumSyms(info,key) */
		newActs=XkbEnlargeKeyActions(info,i+rep->firstKeyAction,
								numDesc[i]);
		_XkbCopyFromReadBuffer(buf,(char *)newActs,
						numDesc[i]*sizeof(XkbAction));
	    }
	}
	return True;
    }
    return True;
}

static Bool
_XkbReadKeyBehaviors(dpy,buf,xkb,rep)
    Display *		dpy;
    XkbReadBufferPtr	buf;
    XkbDescPtr 		xkb;
    xkbGetMapReply *	rep;
{
register int i;
xkbBehaviorWireDesc	*wire;

    if ( rep->totalKeyBehaviors>0 ) {
	if ( xkb->server->behaviors == NULL ) {
	    int size = xkb->max_key_code+1;
	    xkb->server->behaviors = 
			(XkbBehavior *)Xcalloc(size,sizeof(XkbBehavior));
	    if (xkb->server->behaviors==NULL)
		return False;
	}
	for (i=0;i<rep->totalKeyBehaviors;i++) {
	    wire= (xkbBehaviorWireDesc *)_XkbGetReadBufferPtr(buf,
						SIZEOF(xkbBehaviorWireDesc));
	    if (wire==NULL)
		return False;
	    xkb->server->behaviors[wire->key].type= wire->type;
	    xkb->server->behaviors[wire->key].data= wire->data;
	}
    }
    return True;
}

static Bool
_XkbReadVirtualMods(dpy,buf,xkb,rep)
    Display *		dpy;
    XkbReadBufferPtr	buf;
    XkbDescPtr		xkb;
    xkbGetMapReply *	rep;
{
    if ( rep->virtualMods ) {
	register int i,bit,nVMods;
	register char *data;

	for (i=nVMods=0,bit=1;i<XkbNumVirtualMods;i++,bit<<=1) {
	    if (rep->virtualMods&bit)
		nVMods++;
	}
	data= _XkbGetReadBufferPtr(buf,XkbPaddedSize(nVMods));
	if (data==NULL)
	    return False;
	for (i=0,bit=1;(i<XkbNumVirtualMods)&&(nVMods>0);i++,bit<<=1) {
	    if (rep->virtualMods&bit) {
		xkb->server->vmods[i]= *data++;
		nVMods--;
	    }
	}
	return True;
    }
    return True;
}

static Bool
_XkbReadExplicitComponents(dpy,buf,xkb,rep)
    Display *		dpy;
    XkbReadBufferPtr	buf;
    XkbDescPtr		xkb;
    xkbGetMapReply *	rep;
{
register int i;
unsigned char *wire,*start;

    if ( rep->totalKeyExplicit>0 ) {
	if ( xkb->server->explicit == NULL ) {
	    int size = xkb->max_key_code+1;
	    xkb->server->explicit = (unsigned char *)Xcalloc(size,1);
	    if (xkb->server->explicit==NULL)
		return False;
	}
	i= XkbPaddedSize(2*rep->totalKeyExplicit);
	wire=(unsigned char *)_XkbGetReadBufferPtr(buf,i);
	for (i=0;i<rep->totalKeyExplicit;i++,wire+=2) {
	    xkb->server->explicit[wire[0]]= wire[1];
	}
    }
    return True;
}

static xkbGetMapReq *
_XkbGetGetMapReq(dpy,xkb)
    Display *dpy;
    XkbDescRec *xkb;
{
xkbGetMapReq *req;

    GetReq(kbGetMap, req);
    req->reqType = dpy->xkb_info->codes->major_opcode;
    req->xkbReqType = X_kbGetMap;
    req->deviceSpec = xkb->device_spec;
    req->full = req->partial = 0;
    req->firstType = req->nTypes = 0;
    req->firstKeySym = req->nKeySyms = 0;
    req->firstKeyAction = req->nKeyActions = 0;
    req->firstKeyBehavior = req->nKeyBehaviors = 0;
    req->virtualMods = 0;
    req->firstKeyExplicit = req->nKeyExplicit = 0;
    return req;
}

static Bool
_XkbHandleGetMapReply(dpy,xkb)
    Display *dpy;
    XkbDescRec *xkb;
{
xkbGetMapReply rep;

    if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
	return False;
    }
    if ( xkb->device_spec == XkbUseCoreKbd )
	xkb->device_spec= rep.deviceID;
    xkb->min_key_code = rep.minKeyCode;
    xkb->max_key_code = rep.maxKeyCode;

    if ((rep.present&XkbFullServerInfoMask)&&(!xkb->server))
	_XkbInitServerMap(xkb);
    if (rep.length) {
	XkbReadBufferRec	buf;
	int			left;
	if (_XkbInitReadBuffer(dpy,&buf,rep.length*4)) {
	    Status ok= True;
	    ok= _XkbReadKeyTypes(dpy,&buf,xkb,&rep,1)&&ok;
	    ok= _XkbReadKeySyms(dpy,&buf,xkb,&rep,1)&&ok;
	    ok= _XkbReadKeyActions(dpy,&buf,xkb,&rep,1)&&ok;
	    ok= _XkbReadKeyBehaviors(dpy,&buf,xkb,&rep)&&ok;
	    ok= _XkbReadVirtualMods(dpy,&buf,xkb,&rep)&&ok;
	    ok= _XkbReadExplicitComponents(dpy,&buf,xkb,&rep)&&ok;
	    left= _XkbFreeReadBuffer(&buf);
	    if (ok && left) 
		fprintf(stderr,
			"HandleGetMapReply! Bad length (%d extra bytes)\n",
			left);
	    if ((!ok) || left || buf.error) {
		return False;
	    }
	}
    }
    return True;
}

Status
XkbGetUpdatedMap(dpy,which,xkb)
    Display *dpy;
    unsigned which;
    XkbDescRec *xkb;
{
    if ((dpy->flags & XlibDisplayNoXkb) ||
	(!dpy->xkb_info && !XkbUseExtension(dpy)))
	return False;
    if (which) {
	register xkbGetMapReq *req;
	Status ok;

	LockDisplay(dpy);

	req = _XkbGetGetMapReq(dpy, xkb);
	req->full = which;
	ok= _XkbHandleGetMapReply(dpy, xkb);

	UnlockDisplay(dpy);
	SyncHandle();
	return ok;
    }
    return True;
}

XkbDescRec	*
XkbGetMap(dpy,which,deviceSpec)
    Display *dpy;
    unsigned which;
    unsigned deviceSpec;
{
XkbDescRec *xkb;

    xkb = (XkbDescRec *)Xmalloc(sizeof(XkbDescRec)+sizeof(XkbClientMapRec));
    if (xkb) {
	bzero(xkb,sizeof(XkbDescRec));
	xkb->device_spec = deviceSpec;
	xkb->map = (XkbClientMapRec *)&xkb[1];
	bzero(xkb->map,sizeof(XkbClientMapRec));
	if ((which)&&(!XkbGetUpdatedMap(dpy,which,xkb))) {
	    Xfree(xkb);
	    xkb= NULL;
	}
    }
    return xkb;
}

Status
XkbGetKeyTypes(dpy,first,num,xkb)
    Display *dpy;
    unsigned first;
    unsigned num;
    XkbDescRec *xkb;
{
    register xkbGetMapReq *req;
    Status ok;

    if ((dpy->flags & XlibDisplayNoXkb) ||
	(!dpy->xkb_info && !XkbUseExtension(dpy)))
	return False;

    LockDisplay(dpy);

    req = _XkbGetGetMapReq(dpy, xkb);
    req->firstType = first;
    req->nTypes = num;
    ok= _XkbHandleGetMapReply(dpy, xkb);

    UnlockDisplay(dpy);
    SyncHandle();
    return ok;
}

Status
XkbGetKeyActions(dpy,first,num,xkb)
    Display *dpy;
    unsigned first;
    unsigned num;
    XkbDescRec *xkb;
{
    register xkbGetMapReq *req;
    Status ok;

    if ((dpy->flags & XlibDisplayNoXkb) ||
	(!dpy->xkb_info && !XkbUseExtension(dpy)))
	return False;

    LockDisplay(dpy);

    req = _XkbGetGetMapReq(dpy, xkb);
    req->firstKeyAction = first;
    req->nKeyActions = num;
    ok= _XkbHandleGetMapReply(dpy, xkb);

    UnlockDisplay(dpy);
    SyncHandle();
    return ok;
}

Status
XkbGetKeySyms(dpy,first,num,xkb)
    Display *dpy;
    unsigned first;
    unsigned num;
    XkbDescRec *xkb;
{
    register xkbGetMapReq *req;
    Status ok;

    if ((dpy->flags & XlibDisplayNoXkb) ||
	(!dpy->xkb_info && !XkbUseExtension(dpy)))
	return False;

    LockDisplay(dpy);

    req = _XkbGetGetMapReq(dpy, xkb);
    req->firstKeySym = first;
    req->nKeySyms = num;
    ok= _XkbHandleGetMapReply(dpy, xkb);

    UnlockDisplay(dpy);
    SyncHandle();

    return ok;
}

Status
XkbGetKeyBehaviors(dpy,first,num,xkb)
    Display *dpy;
    unsigned first;
    unsigned num;
    XkbDescRec *xkb;
{
    register xkbGetMapReq *req;
    Status ok;

    if ((dpy->flags & XlibDisplayNoXkb) ||
	(!dpy->xkb_info && !XkbUseExtension(dpy)))
	return False;

    LockDisplay(dpy);

    req = _XkbGetGetMapReq(dpy, xkb);
    req->firstKeyBehavior = first;
    req->nKeyBehaviors = num;
    ok= _XkbHandleGetMapReply(dpy, xkb);

    UnlockDisplay(dpy);
    SyncHandle();
    return ok;
}

Status
XkbGetVirtualMods(dpy,which,xkb)
    Display *dpy;
    unsigned which;
    XkbDescRec *xkb;
{
    register xkbGetMapReq *req;
    Status ok;

    if ((dpy->flags & XlibDisplayNoXkb) ||
	(!dpy->xkb_info && !XkbUseExtension(dpy)))
	return False;

    LockDisplay(dpy);

    req = _XkbGetGetMapReq(dpy, xkb);
    req->virtualMods = which;
    ok= _XkbHandleGetMapReply(dpy, xkb);

    UnlockDisplay(dpy);
    SyncHandle();
    return ok;
}

Status
XkbGetKeyExplicitComponents(dpy,first,num,xkb)
    Display *dpy;
    unsigned first;
    unsigned num;
    XkbDescRec *xkb;
{
    register xkbGetMapReq *req;
    Status ok;

    if ((dpy->flags & XlibDisplayNoXkb) ||
	(!dpy->xkb_info && !XkbUseExtension(dpy)))
	return False;

    LockDisplay(dpy);

    req = _XkbGetGetMapReq(dpy, xkb);
    req->firstKeyExplicit = first;
    req->nKeyExplicit = num;
    ok= _XkbHandleGetMapReply(dpy, xkb);

    UnlockDisplay(dpy);
    SyncHandle();
    return ok;
}

Status
XkbRefreshMap(dpy,xkb,changes)
    Display *dpy;
    XkbDescRec *xkb;
    XkbMapChangesRec *changes;
{
    xkbGetMapReq *req;
    xkbGetMapReply rep;
    int nRead;
    XkbInfoPtr xkbi;

    if ((dpy->flags & XlibDisplayNoXkb) ||
	(!dpy->xkb_info && !XkbUseExtension(dpy)))
	return False;
    LockDisplay(dpy);
    xkbi = dpy->xkb_info;
    if (changes->changed) {
	Status ok= True;
	req = _XkbGetGetMapReq(dpy, xkb);
	req->full = 0;
	req->partial = changes->changed;
	req->firstType = changes->first_type;
	req->nTypes = changes->num_types;
	req->firstKeySym = changes->first_key_sym;
	req->nKeySyms = changes->num_key_syms;
	req->firstKeyAction = changes->first_key_act;
	req->nKeyActions = changes->num_key_acts;
	req->firstKeyBehavior = changes->first_key_behavior;
	req->nKeyBehaviors = changes->num_key_behaviors;
	req->virtualMods = changes->vmods;
	req->firstKeyExplicit = changes->first_key_explicit;
	req->nKeyExplicit = changes->num_key_explicit;
	ok= _XkbHandleGetMapReply(dpy, xkb);
	SyncHandle();
	UnlockDisplay(dpy);
	return ok;
    }
    return True;
}