summaryrefslogtreecommitdiff
path: root/src/XSync.c
blob: 3ca13082735a10d25797936a94a51e77ac5f48a7 (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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*

Copyright 1991, 1993, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from The Open Group.

*/

/***********************************************************
Copyright 1991,1993 by Digital Equipment Corporation, Maynard, Massachusetts,
and Olivetti Research Limited, Cambridge, England.

                        All Rights Reserved

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 names of Digital or Olivetti
not be used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

DIGITAL AND OLIVETTI DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL THEY 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.

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

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <X11/Xlibint.h>
#include <X11/extensions/Xext.h>
#include <X11/extensions/extutil.h>
#include <X11/extensions/sync.h>
#include <X11/extensions/syncproto.h>
#include "eat.h"

static XExtensionInfo _sync_info_data;
static XExtensionInfo *sync_info = &_sync_info_data;
static const char     *sync_extension_name = SYNC_NAME;

#define SyncCheckExtension(dpy,i,val) \
		XextCheckExtension(dpy, i, sync_extension_name, val)
#define SyncSimpleCheckExtension(dpy,i) \
		XextSimpleCheckExtension(dpy, i, sync_extension_name)

static int      close_display(Display *dpy, XExtCodes *codes);
static Bool wire_to_event(Display *dpy, XEvent *event, xEvent *wire);
static Status event_to_wire(Display *dpy, XEvent *event, xEvent *wire);
static char    *error_string(Display *dpy, int code, XExtCodes *codes,
			     char *buf, int n);

static XExtensionHooks sync_extension_hooks = {
    NULL,			/* create_gc */
    NULL,			/* copy_gc */
    NULL,			/* flush_gc */
    NULL,			/* free_gc */
    NULL,			/* create_font */
    NULL,			/* free_font */
    close_display,		/* close_display */
    wire_to_event,		/* wire_to_event */
    event_to_wire,		/* event_to_wire */
    NULL,			/* error */
    error_string,		/* error_string */
};

static const char *sync_error_list[] = {
    "BadCounter",
    "BadAlarm",
    "BadFence",
};

typedef struct _SyncVersionInfoRec {
    short major;
    short minor;
    int num_errors;
} SyncVersionInfo;

static /* const */ SyncVersionInfo supported_versions[] = {
    { 3 /* major */, 0 /* minor */, 2 /* num_errors */ },
    { 3 /* major */, 1 /* minor */, 3 /* num_errors */ },
};

#define NUM_VERSIONS (sizeof(supported_versions)/sizeof(supported_versions[0]))
#define GET_VERSION(info) ((const SyncVersionInfo*)(info)->data)
#define IS_VERSION_SUPPORTED(info) (!!(info))

static
const SyncVersionInfo* GetVersionInfo(Display *dpy)
{
    xSyncInitializeReply rep;
    xSyncInitializeReq *req;
    XExtCodes codes;
    int i;

    if (!XQueryExtension(dpy, sync_extension_name,
                         &codes.major_opcode,
                         &codes.first_event,
                         &codes.first_error))
        return NULL;

    LockDisplay(dpy);
    GetReq(SyncInitialize, req);
    req->reqType = codes.major_opcode;
    req->syncReqType = X_SyncInitialize;
    req->majorVersion = SYNC_MAJOR_VERSION;
    req->minorVersion = SYNC_MINOR_VERSION;
    if (!_XReply(dpy, (xReply *) & rep, 0, xTrue))
    {
	UnlockDisplay(dpy);
	SyncHandle();
	return NULL;
    }
    UnlockDisplay(dpy);
    SyncHandle();

    for (i = 0; i < NUM_VERSIONS; i++) {
	if (supported_versions[i].major == rep.majorVersion &&
	    supported_versions[i].minor == rep.minorVersion) {
	    return &supported_versions[i];
	}
    }

    return NULL;
}

static
XExtDisplayInfo *find_display_create_optional(Display *dpy, Bool create)
{
    XExtDisplayInfo *dpyinfo;

    if (!sync_info) {
        if (!(sync_info = XextCreateExtension())) return NULL;
    }

    if (!(dpyinfo = XextFindDisplay (sync_info, dpy)) && create) {
        dpyinfo = XextAddDisplay(sync_info, dpy,
                                 sync_extension_name,
                                 &sync_extension_hooks,
                                 XSyncNumberEvents,
                                 (XPointer)GetVersionInfo(dpy));
    }

    return dpyinfo;
}

static
XExtDisplayInfo *find_display (Display *dpy)
{
    return find_display_create_optional(dpy, True);
}

static
XEXT_GENERATE_CLOSE_DISPLAY(close_display, sync_info)

static
char *error_string(Display *dpy, int code, XExtCodes *codes, char *buf, int n)
{
    XExtDisplayInfo *info = find_display_create_optional(dpy, False);
    int nerr = IS_VERSION_SUPPORTED(info) ? GET_VERSION(info)->num_errors : 0;

    code -= codes->first_error;
    if (code >= 0 && code < nerr) {
	char tmp[256];
	sprintf (tmp, "%s.%d", sync_extension_name, code);
	XGetErrorDatabaseText (dpy, "XProtoError", tmp, sync_error_list[code], buf, n);
	return buf;
    }
    return (char *)0;
}

static Bool
wire_to_event(Display *dpy, XEvent *event, xEvent *wire)
{
    XExtDisplayInfo *info = find_display(dpy);
    XSyncCounterNotifyEvent *aevent;
    xSyncCounterNotifyEvent *awire;
    XSyncAlarmNotifyEvent *anl;
    xSyncAlarmNotifyEvent *ane;

    SyncCheckExtension(dpy, info, False);

    switch ((wire->u.u.type & 0x7F) - info->codes->first_event)
    {
      case XSyncCounterNotify:
	awire = (xSyncCounterNotifyEvent *) wire;
	aevent = (XSyncCounterNotifyEvent *) event;
	aevent->type = awire->type & 0x7F;
	aevent->serial = _XSetLastRequestRead(dpy,
					      (xGenericReply *) wire);
	aevent->send_event = (awire->type & 0x80) != 0;
	aevent->display = dpy;
	aevent->counter = awire->counter;
	XSyncIntsToValue(&aevent->wait_value, awire->wait_value_lo,
				    awire->wait_value_hi);
	XSyncIntsToValue(&aevent->counter_value,
				    awire->counter_value_lo,
				    awire->counter_value_hi);
	aevent->time = awire->time;
	aevent->count = awire->count;
	aevent->destroyed = awire->destroyed;
	return True;

      case XSyncAlarmNotify:
	ane = (xSyncAlarmNotifyEvent *) wire;	/* ENCODING EVENT PTR */
	anl = (XSyncAlarmNotifyEvent *) event;	/* LIBRARY EVENT PTR */
	anl->type = ane->type & 0x7F;
	anl->serial = _XSetLastRequestRead(dpy,
					   (xGenericReply *) wire);
	anl->send_event = (ane->type & 0x80) != 0;
	anl->display = dpy;
	anl->alarm = ane->alarm;
	XSyncIntsToValue(&anl->counter_value,
				    ane->counter_value_lo,
				    ane->counter_value_hi);
	XSyncIntsToValue(&anl->alarm_value,
				    ane->alarm_value_lo,
				    ane->alarm_value_hi);
	anl->state = (XSyncAlarmState)ane->state;
	anl->time = ane->time;
	return True;
    }

    return False;
}

static Status
event_to_wire(Display *dpy, XEvent *event, xEvent *wire)
{
    XExtDisplayInfo *info = find_display(dpy);
    XSyncCounterNotifyEvent *aevent;
    xSyncCounterNotifyEvent *awire;
    XSyncAlarmNotifyEvent *anl;
    xSyncAlarmNotifyEvent *ane;

    SyncCheckExtension(dpy, info, False);

    switch ((event->type & 0x7F) - info->codes->first_event)
    {
      case XSyncCounterNotify:
	awire = (xSyncCounterNotifyEvent *) wire;
	aevent = (XSyncCounterNotifyEvent *) event;
	awire->type = aevent->type | (aevent->send_event ? 0x80 : 0);
	awire->sequenceNumber = aevent->serial & 0xFFFF;
	awire->counter = aevent->counter;
	awire->wait_value_lo = XSyncValueLow32(aevent->wait_value);
	awire->wait_value_hi = XSyncValueHigh32(aevent->wait_value);
	awire->counter_value_lo = XSyncValueLow32(aevent->counter_value);
	awire->counter_value_hi = XSyncValueHigh32(aevent->counter_value);
	awire->time = aevent->time;
	awire->count = aevent->count;
	awire->destroyed = aevent->destroyed;
	return True;

      case XSyncAlarmNotify:
	ane = (xSyncAlarmNotifyEvent *) wire;	/* ENCODING EVENT PTR */
	anl = (XSyncAlarmNotifyEvent *) event;	/* LIBRARY EVENT PTR */
	ane->type = anl->type | (anl->send_event ? 0x80 : 0);
	ane->sequenceNumber = anl->serial & 0xFFFF;
	ane->alarm = anl->alarm;
	ane->counter_value_lo = XSyncValueLow32(anl->counter_value);
	ane->counter_value_hi = XSyncValueHigh32(anl->counter_value);
	ane->alarm_value_lo = XSyncValueLow32(anl->alarm_value);
	ane->alarm_value_hi = XSyncValueHigh32(anl->alarm_value);
	ane->state = anl->state;
	ane->time = anl->time;
	return True;
    }
    return False;
}

Status
XSyncQueryExtension(
    Display *dpy,
    int *event_base_return, int *error_base_return)
{
    XExtDisplayInfo *info = find_display(dpy);

    if (XextHasExtension(info))
    {
	*event_base_return = info->codes->first_event;
	*error_base_return = info->codes->first_error;
	return True;
    }
    else
	return False;
}

Status
XSyncInitialize(
    Display *dpy,
    int *major_version_return, int *minor_version_return)
{
    XExtDisplayInfo *info = find_display(dpy);

    SyncCheckExtension(dpy, info, False);

    if (IS_VERSION_SUPPORTED(info)) {
	*major_version_return = GET_VERSION(info)->major;
	*minor_version_return = GET_VERSION(info)->minor;

	return True;
    } else {
	return False;
    }
}

XSyncSystemCounter *
XSyncListSystemCounters(Display *dpy, int *n_counters_return)
{
    XExtDisplayInfo *info = find_display(dpy);
    xSyncListSystemCountersReply rep;
    xSyncListSystemCountersReq *req;
    XSyncSystemCounter *list = NULL;

    SyncCheckExtension(dpy, info, NULL);

    LockDisplay(dpy);
    GetReq(SyncListSystemCounters, req);
    req->reqType = info->codes->major_opcode;
    req->syncReqType = X_SyncListSystemCounters;
    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse))
	goto bail;

    *n_counters_return = rep.nCounters;
    if (rep.nCounters > 0)
    {
	xSyncSystemCounter *pWireSysCounter, *pNextWireSysCounter;
	XSyncCounter counter;
	int replylen;
	int i;

	list = Xmalloc(rep.nCounters * sizeof(XSyncSystemCounter));
	replylen = rep.length << 2;
	pWireSysCounter = Xmalloc ((unsigned) replylen + sizeof(XSyncCounter));
        /* +1 to leave room for last counter read-ahead */

	if ((!list) || (!pWireSysCounter))
	{
	    if (list) Xfree((char *) list);
	    if (pWireSysCounter)   Xfree((char *) pWireSysCounter);
	    _XEatDataWords(dpy, rep.length);
	    list = NULL;
	    goto bail;
	}

	_XReadPad(dpy, (char *)pWireSysCounter, replylen);

	counter = pWireSysCounter->counter;
	for (i = 0; i < rep.nCounters; i++)
	{
	    list[i].counter = counter;
	    XSyncIntsToValue(&list[i].resolution,
					pWireSysCounter->resolution_lo,
					pWireSysCounter->resolution_hi);

	    /* we may be about to clobber the counter field of the
	     * next syscounter because we have to add a null terminator
	     * to the counter name string.  So we save the next counter
	     * here.
	     */
	    pNextWireSysCounter = (xSyncSystemCounter *)
		(((char *)pWireSysCounter) + ((SIZEOF(xSyncSystemCounter) +
				     pWireSysCounter->name_length + 3) & ~3));
	    counter = pNextWireSysCounter->counter;

	    list[i].name = ((char *)pWireSysCounter) +
						SIZEOF(xSyncSystemCounter);
	    /* null-terminate the string */
	    *(list[i].name + pWireSysCounter->name_length) = '\0';
	    pWireSysCounter = pNextWireSysCounter;
	}
    }

bail:
    UnlockDisplay(dpy);
    SyncHandle();
    return list;
}

void
XSyncFreeSystemCounterList(XSyncSystemCounter *list)
{
    if (list)
    {
	Xfree( ((char *)list[0].name) - SIZEOF(xSyncSystemCounter));
	Xfree(list);
    }
}


XSyncCounter
XSyncCreateCounter(Display *dpy, XSyncValue initial_value)
{
    XExtDisplayInfo *info = find_display(dpy);
    xSyncCreateCounterReq *req;

    SyncCheckExtension(dpy, info, None);

    LockDisplay(dpy);
    GetReq(SyncCreateCounter, req);
    req->reqType = info->codes->major_opcode;
    req->syncReqType = X_SyncCreateCounter;

    req->cid = XAllocID(dpy);
    req->initial_value_lo = XSyncValueLow32(initial_value);
    req->initial_value_hi = XSyncValueHigh32(initial_value);

    UnlockDisplay(dpy);
    SyncHandle();
    return req->cid;
}

Status
XSyncSetCounter(Display *dpy, XSyncCounter counter, XSyncValue value)
{
    XExtDisplayInfo *info = find_display(dpy);
    xSyncSetCounterReq *req;

    SyncCheckExtension(dpy, info, False);

    LockDisplay(dpy);
    GetReq(SyncSetCounter, req);
    req->reqType = info->codes->major_opcode;
    req->syncReqType = X_SyncSetCounter;
    req->cid = counter;
    req->value_lo = XSyncValueLow32(value);
    req->value_hi = XSyncValueHigh32(value);
    UnlockDisplay(dpy);
    SyncHandle();
    return True;
}

Status
XSyncChangeCounter(Display *dpy, XSyncCounter counter, XSyncValue value)
{
    XExtDisplayInfo *info = find_display(dpy);
    xSyncChangeCounterReq *req;

    SyncCheckExtension(dpy, info, False);

    LockDisplay(dpy);
    GetReq(SyncChangeCounter, req);
    req->reqType = info->codes->major_opcode;
    req->syncReqType = X_SyncChangeCounter;
    req->cid = counter;
    req->value_lo = XSyncValueLow32(value);
    req->value_hi = XSyncValueHigh32(value);
    UnlockDisplay(dpy);
    SyncHandle();
    return True;
}

Status
XSyncDestroyCounter(Display *dpy, XSyncCounter counter)
{
    XExtDisplayInfo *info = find_display(dpy);
    xSyncDestroyCounterReq *req;

    SyncCheckExtension(dpy, info, False);

    LockDisplay(dpy);
    GetReq(SyncDestroyCounter, req);
    req->reqType = info->codes->major_opcode;
    req->syncReqType = X_SyncDestroyCounter;
    req->counter = counter;
    UnlockDisplay(dpy);
    SyncHandle();

    return True;
}

Status
XSyncQueryCounter(Display *dpy, XSyncCounter counter, XSyncValue *value_return)
{
    XExtDisplayInfo *info = find_display(dpy);
    xSyncQueryCounterReply rep;
    xSyncQueryCounterReq *req;

    SyncCheckExtension(dpy, info, False);

    LockDisplay(dpy);
    GetReq(SyncQueryCounter, req);
    req->reqType = info->codes->major_opcode;
    req->syncReqType = X_SyncQueryCounter;
    req->counter = counter;
    if (!_XReply(dpy, (xReply *) & rep, 0, xTrue))
    {
	UnlockDisplay(dpy);
	SyncHandle();
	return False;
    }
    XSyncIntsToValue(value_return, rep.value_lo, rep.value_hi);
    UnlockDisplay(dpy);
    SyncHandle();

    return True;
}


Status
XSyncAwait(Display *dpy, XSyncWaitCondition *wait_list, int n_conditions)
{
    XExtDisplayInfo *info = find_display(dpy);
    XSyncWaitCondition *wait_item = wait_list;
    xSyncAwaitReq  *req;
    unsigned int    len;

    SyncCheckExtension(dpy, info, False);

    LockDisplay(dpy);
    GetReq(SyncAwait, req);
    req->reqType = info->codes->major_opcode;
    req->syncReqType = X_SyncAwait;
    len = (n_conditions * SIZEOF(xSyncWaitCondition)) >> 2;
    SetReqLen(req, len, len /* XXX */ );

    while (n_conditions--)
    {
	xSyncWaitCondition  wc;
	wc.counter = wait_item->trigger.counter;
	wc.value_type = wait_item->trigger.value_type;
	wc.wait_value_lo = XSyncValueLow32(wait_item->trigger.wait_value);
	wc.wait_value_hi = XSyncValueHigh32(wait_item->trigger.wait_value);
	wc.test_type = wait_item->trigger.test_type;
	wc.event_threshold_lo = XSyncValueLow32(wait_item->event_threshold);
	wc.event_threshold_hi = XSyncValueHigh32(wait_item->event_threshold);
	Data(dpy, (char *)&wc, SIZEOF(xSyncWaitCondition));
	wait_item++;		/* get next trigger */
    }

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

static void
_XProcessAlarmAttributes(Display *dpy, xSyncChangeAlarmReq *req,
			 unsigned long valuemask,
			 XSyncAlarmAttributes *attributes)
{

    unsigned long  values[32];
    unsigned long *value = values;
    unsigned int    nvalues;

    if (valuemask & XSyncCACounter)
	*value++ = attributes->trigger.counter;

    if (valuemask & XSyncCAValueType)
	*value++ = attributes->trigger.value_type;

    if (valuemask & XSyncCAValue)
    {
	*value++ = XSyncValueHigh32(attributes->trigger.wait_value);
	*value++ = XSyncValueLow32(attributes->trigger.wait_value);
    }

    if (valuemask & XSyncCATestType)
	*value++ = attributes->trigger.test_type;

    if (valuemask & XSyncCADelta)
    {
	*value++ = XSyncValueHigh32(attributes->delta);
	*value++ = XSyncValueLow32(attributes->delta);
    }

    if (valuemask & XSyncCAEvents)
	*value++ = attributes->events;

    /* N.B. the 'state' field cannot be set or changed */
    req->length += (nvalues = value - values);
    nvalues <<= 2;		/* watch out for macros... */

    Data32(dpy, (long *) values, (long) nvalues);
}

XSyncAlarm
XSyncCreateAlarm(
    Display *dpy,
    unsigned long values_mask,
    XSyncAlarmAttributes *values)
{
    XExtDisplayInfo *info = find_display(dpy);
    xSyncCreateAlarmReq *req;
    XSyncAlarm      aid;

    SyncCheckExtension(dpy, info, False);

    LockDisplay(dpy);
    GetReq(SyncCreateAlarm, req);
    req->reqType = info->codes->major_opcode;
    req->syncReqType = X_SyncCreateAlarm;
    req->id = aid = XAllocID(dpy);
    values_mask &= XSyncCACounter | XSyncCAValueType | XSyncCAValue
			| XSyncCATestType | XSyncCADelta | XSyncCAEvents;
    if ((req->valueMask = values_mask))
	_XProcessAlarmAttributes(dpy, (xSyncChangeAlarmReq *) req,
				 values_mask, values);
    UnlockDisplay(dpy);
    SyncHandle();
    return aid;
}

Status
XSyncDestroyAlarm(Display *dpy, XSyncAlarm alarm)
{
    XExtDisplayInfo *info = find_display(dpy);
    xSyncDestroyAlarmReq *req;

    SyncCheckExtension(dpy, info, False);

    LockDisplay(dpy);
    GetReq(SyncDestroyAlarm, req);
    req->reqType = info->codes->major_opcode;
    req->syncReqType = X_SyncDestroyAlarm;
    req->alarm = alarm;
    UnlockDisplay(dpy);
    SyncHandle();
    return True;
}

Status
XSyncQueryAlarm(
    Display *dpy,
    XSyncAlarm alarm,
    XSyncAlarmAttributes *values_return)
{
    XExtDisplayInfo *info = find_display(dpy);
    xSyncQueryAlarmReq *req;
    xSyncQueryAlarmReply rep;

    SyncCheckExtension(dpy, info, False);

    LockDisplay(dpy);
    GetReq(SyncQueryAlarm, req);
    req->reqType = info->codes->major_opcode;
    req->syncReqType = X_SyncQueryAlarm;
    req->alarm = alarm;

    if (!(_XReply(dpy, (xReply *) & rep,
    ((SIZEOF(xSyncQueryAlarmReply) - SIZEOF(xGenericReply)) >> 2), xFalse)))
    {
	UnlockDisplay(dpy);
	SyncHandle();
	return False;
    }

    values_return->trigger.counter = rep.counter;
    values_return->trigger.value_type = (XSyncValueType)rep.value_type;
    XSyncIntsToValue(&values_return->trigger.wait_value,
				rep.wait_value_lo, rep.wait_value_hi);
    values_return->trigger.test_type = (XSyncTestType)rep.test_type;
    XSyncIntsToValue(&values_return->delta, rep.delta_lo,
				rep.delta_hi);
    values_return->events = rep.events;
    values_return->state = (XSyncAlarmState)rep.state;
    UnlockDisplay(dpy);
    SyncHandle();
    return True;
}

Status
XSyncChangeAlarm(
    Display *dpy,
    XSyncAlarm alarm,
    unsigned long values_mask,
    XSyncAlarmAttributes *values)
{
    XExtDisplayInfo *info = find_display(dpy);
    xSyncChangeAlarmReq *req;

    SyncCheckExtension(dpy, info, False);

    LockDisplay(dpy);
    GetReq(SyncChangeAlarm, req);
    req->reqType = info->codes->major_opcode;
    req->syncReqType = X_SyncChangeAlarm;
    req->alarm = alarm;
    values_mask &= XSyncCACounter | XSyncCAValueType | XSyncCAValue
		 | XSyncCATestType | XSyncCADelta | XSyncCAEvents;
    if ((req->valueMask = values_mask))
	_XProcessAlarmAttributes(dpy, req, values_mask, values);
    UnlockDisplay(dpy);
    SyncHandle();
    return True;
}

Status
XSyncSetPriority(
    Display *dpy,
    XID client_resource_id,
    int priority)
{
    XExtDisplayInfo *info = find_display(dpy);
    xSyncSetPriorityReq *req;

    SyncCheckExtension(dpy, info, False);

    LockDisplay(dpy);
    GetReq(SyncSetPriority, req);
    req->reqType = info->codes->major_opcode;
    req->syncReqType = X_SyncSetPriority;
    req->id = client_resource_id;
    req->priority = priority;
    UnlockDisplay(dpy);
    SyncHandle();
    return True;
}

Status
XSyncGetPriority(Display *dpy, XID client_resource_id, int *return_priority)
{
    XExtDisplayInfo *info = find_display(dpy);
    xSyncGetPriorityReply rep;
    xSyncGetPriorityReq *req;

    SyncCheckExtension(dpy, info, False);

    LockDisplay(dpy);
    GetReq(SyncGetPriority, req);
    req->reqType = info->codes->major_opcode;
    req->syncReqType = X_SyncGetPriority;
    req->id = client_resource_id;

    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse))
    {
	UnlockDisplay(dpy);
	SyncHandle();
	return False;
    }
    if (return_priority)
	*return_priority = rep.priority;

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

XSyncFence
XSyncCreateFence(Display *dpy, Drawable d, Bool initially_triggered)
{
    XExtDisplayInfo *info = find_display(dpy);
    xSyncCreateFenceReq *req;
    XSyncFence id;

    SyncCheckExtension(dpy, info, None);

    LockDisplay(dpy);
    GetReq(SyncCreateFence, req);
    req->reqType = info->codes->major_opcode;
    req->syncReqType = X_SyncCreateFence;

    req->d = d;
    id = req->fid = XAllocID(dpy);
    req->initially_triggered = initially_triggered;

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

Bool
XSyncTriggerFence(Display *dpy, XSyncFence fence)
{
    XExtDisplayInfo *info = find_display(dpy);
    xSyncTriggerFenceReq *req;

    SyncCheckExtension(dpy, info, None);

    LockDisplay(dpy);
    GetReq(SyncTriggerFence, req);
    req->reqType = info->codes->major_opcode;
    req->syncReqType = X_SyncTriggerFence;

    req->fid = fence;

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

Bool
XSyncResetFence(Display *dpy, XSyncFence fence)
{
    XExtDisplayInfo *info = find_display(dpy);
    xSyncResetFenceReq *req;

    SyncCheckExtension(dpy, info, None);

    LockDisplay(dpy);
    GetReq(SyncResetFence, req);
    req->reqType = info->codes->major_opcode;
    req->syncReqType = X_SyncResetFence;

    req->fid = fence;

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

Bool
XSyncDestroyFence(Display *dpy, XSyncFence fence)
{
    XExtDisplayInfo *info = find_display(dpy);
    xSyncDestroyFenceReq *req;

    SyncCheckExtension(dpy, info, None);

    LockDisplay(dpy);
    GetReq(SyncDestroyFence, req);
    req->reqType = info->codes->major_opcode;
    req->syncReqType = X_SyncDestroyFence;

    req->fid = fence;

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

Bool
XSyncQueryFence(Display *dpy, XSyncFence fence, Bool *triggered)
{
    XExtDisplayInfo *info = find_display(dpy);
    xSyncQueryFenceReply rep;
    xSyncQueryFenceReq *req;

    SyncCheckExtension(dpy, info, None);

    LockDisplay(dpy);
    GetReq(SyncQueryFence, req);
    req->reqType = info->codes->major_opcode;
    req->syncReqType = X_SyncQueryFence;
    req->fid = fence;

    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse))
    {
	UnlockDisplay(dpy);
	SyncHandle();
	return False;
    }
    if (triggered)
	*triggered = rep.triggered;

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

Bool
XSyncAwaitFence(Display *dpy, const XSyncFence *fence_list, int n_fences)
{
    XExtDisplayInfo *info = find_display(dpy);
    xSyncAwaitFenceReq  *req;

    SyncCheckExtension(dpy, info, False);

    LockDisplay(dpy);
    GetReq(SyncAwaitFence, req);
    req->reqType = info->codes->major_opcode;
    req->syncReqType = X_SyncAwaitFence;
    SetReqLen(req, n_fences, n_fences);

    Data32(dpy, (char *)fence_list, sizeof(CARD32) * n_fences);

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

/*
 *  Functions corresponding to the macros for manipulating 64-bit values
 */

void
XSyncIntToValue(XSyncValue *pv, int i)
{
    _XSyncIntToValue(pv,i);
}

void
XSyncIntsToValue(XSyncValue *pv, unsigned int l, int h)
{
    _XSyncIntsToValue(pv, l, h);
}

Bool
XSyncValueGreaterThan(XSyncValue a, XSyncValue b)
{
    return _XSyncValueGreaterThan(a, b);
}

Bool
XSyncValueLessThan(XSyncValue a, XSyncValue b)
{
    return _XSyncValueLessThan(a, b);
}

Bool
XSyncValueGreaterOrEqual(XSyncValue a, XSyncValue b)
{
    return _XSyncValueGreaterOrEqual(a, b);
}

Bool
XSyncValueLessOrEqual(XSyncValue a, XSyncValue b)
{
    return _XSyncValueLessOrEqual(a, b);
}

Bool
XSyncValueEqual(XSyncValue a, XSyncValue b)
{
    return _XSyncValueEqual(a, b);
}

Bool
XSyncValueIsNegative(XSyncValue v)
{
    return _XSyncValueIsNegative(v);
}

Bool
XSyncValueIsZero(XSyncValue a)
{
    return _XSyncValueIsZero(a);
}

Bool
XSyncValueIsPositive(XSyncValue v)
{
    return _XSyncValueIsPositive(v);
}

unsigned int
XSyncValueLow32(XSyncValue v)
{
    return _XSyncValueLow32(v);
}

int
XSyncValueHigh32(XSyncValue v)
{
    return _XSyncValueHigh32(v);
}

void
XSyncValueAdd(XSyncValue *presult, XSyncValue a, XSyncValue b, Bool *poverflow)
{
    _XSyncValueAdd(presult, a, b, poverflow);
}

void
XSyncValueSubtract(
    XSyncValue *presult,
    XSyncValue a, XSyncValue b,
    Bool *poverflow)
{
    _XSyncValueSubtract(presult, a, b, poverflow);
}

void
XSyncMaxValue(XSyncValue *pv)
{
    _XSyncMaxValue(pv);
}

void
XSyncMinValue(XSyncValue *pv)
{
    _XSyncMinValue(pv);
}