File: SYNC.pm

package info (click to toggle)
libx11-protocol-other-perl 28-1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 1,692 kB
  • ctags: 556
  • sloc: perl: 17,055; ansic: 624; sh: 238; lisp: 143; makefile: 39
file content (1121 lines) | stat: -rw-r--r-- 32,728 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
# Copyright 2011, 2012, 2013 Kevin Ryde

# This file is part of X11-Protocol-Other.
#
# X11-Protocol-Other is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 3, or (at your option) any
# later version.
#
# X11-Protocol-Other is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with X11-Protocol-Other.  If not, see <http://www.gnu.org/licenses/>.

BEGIN { require 5 }
package X11::Protocol::Ext::SYNC;
use strict;
use Carp;
use X11::Protocol;

use vars '$VERSION', '@CARP_NOT';
$VERSION = 28;
@CARP_NOT = ('X11::Protocol');

# uncomment this to run the ### lines
# use Smart::Comments;

# /usr/share/doc/x11proto-core-dev/x11protocol.txt.gz
#
# SYNC 3.1
#    /usr/share/doc/x11proto-xext-dev/sync.txt.gz
#
#    /usr/include/X11/extensions/syncproto.h
#    /usr/include/X11/extensions/syncconst.h
#    /usr/include/X11/extensions/syncstr.h
#
#    /usr/include/X11/extensions/sync.h
#    /usr/share/X11/doc/hardcopy/Xext/synclib.PS.gz
#    /so/xorg/libXext-1.2.0/src/XSync.c
#    /so/xorg/libXext-1.2.0/specs/synclib.xml
#       Xlib
#    /usr/share/xcb/sync.xml
#       xcb
#
#    /so/xorg/xorg-server-1.10.0/Xext/sync.c
#       server source
#
# X11R7.6 SYNC 3.0, no Fence
#    /so/xorg/sync-3.0/sync.txt
#    /so/x11r6.4/xc/include/extensions/sync.h
#    /so/x11r6.4/xc/include/extensions/syncstr.h
#    /so/x11r6.4/xc/programs/Xserver/Xext/sync.c
#
#    /so/xfree/xfree86-3.3.2.3a/include/extensions/sync.h
#    /so/xfree/xfree86-3.3.2.3a/include/extensions/syncstr.h
#    /so/xfree/xfree86-3.3.2.3a/programs/Xserver/Xext/sync.c
#
#    /so/x11r2/X.V11R2/lib/X/XSync.c
#       Xlib

# these not documented yet ...
use constant CLIENT_MAJOR_VERSION => 3;
use constant CLIENT_MINOR_VERSION => 1;

#------------------------------------------------------------------------------
# symbolic constants

use constant constants_list =>
  (
   SyncValueType  => ['Absolute', 'Relative' ],
   SyncTestType   => [ 'PositiveTransition','NegativeTransition',
                       'PositiveComparison','NegativeComparison' ],
   SyncAlarmState => ['Active', 'Inactive', 'Destroyed' ],
  );

sub _ext_constants_install {
  my ($X, $constants_arrayref) = @_;
  foreach (my $i = 0; $i <= $#$constants_arrayref; $i+=2) {
    my $name = $constants_arrayref->[$i];
    my $aref = $constants_arrayref->[$i+1];
    $X->{'ext_const'}->{$name} = $aref;
    $X->{'ext_const_num'}->{$name} = { X11::Protocol::make_num_hash($aref) };
  }
}

#------------------------------------------------------------------------------
# events

use constant events_list =>
  (SyncCounterNotify =>
   [ sub {
       my $X = shift;
       my $data = shift;

       my ($counter,
           $wait_value_hi, $wait_value_lo,
           $counter_value_hi,$counter_value_lo,
           $time,
           $count,
           $destroyed)
         = unpack 'xxxxLLLLLLSCx', $data;
       return
         (@_,
          counter       => $counter,
          wait_value    => _hilo_to_int64($wait_value_hi,$wait_value_lo),
          counter_value => _hilo_to_int64($counter_value_hi,$counter_value_lo),
          time          => _interp_time($time),
          count         => $count,
          destroyed     => $destroyed);
     },
     sub {
       my ($X, %h) = @_;
       return (pack('xxxxLLLLLLSCx',
                    $h{'counter'},
                    _int64_to_hilo($h{'wait_value'}),
                    _int64_to_hilo($h{'counter_value'}),
                    _num_time($h{'time'}),
                    $h{'count'},
                    $h{'destroyed'}),
               1); # "do_seq" put in sequence number
     } ],

   SyncAlarmNotify =>
   [ sub {
       my $X = shift;
       my $data = shift;

       my ($alarm,
           $counter_value_hi,$counter_value_lo,
           $alarm_value_hi, $alarm_value_lo,
           $time,
           $state)
         = unpack 'xxxxLLLLLLCx3', $data;
       return
         (@_,
          alarm         => $alarm,
          counter_value => _hilo_to_int64($counter_value_hi,$counter_value_lo),
          alarm_value   => _hilo_to_int64($alarm_value_hi,$alarm_value_lo),
          time          => _interp_time($time),
          state         => $X->interp('SyncAlarmState',$state));
     },
     sub {
       my ($X, %h) = @_;
       return (pack('xxxxLLLLLLCx3',
                    $h{'alarm'},
                    _int64_to_hilo($h{'counter_value'}),
                    _int64_to_hilo($h{'alarm_value'}),
                    _num_time($h{'time'}),
                    $X->num('SyncAlarmState',$h{'state'})),
               1); # "do_seq" put in sequence number
     } ],
  );

sub _ext_events_install {
  my ($X, $event_num, $events_arrayref) = @_;
  foreach (my $i = 0; $i <= $#$events_arrayref; $i += 2) {
    my $name = $events_arrayref->[$i];
    if (defined (my $already = $X->{'ext_const'}->{'Events'}->[$event_num])) {
      carp "Event $event_num $already overwritten with $name";
    }
    $X->{'ext_const'}->{'Events'}->[$event_num] = $name;
    $X->{'ext_events'}->[$event_num] = $events_arrayref->[$i+1]; # pack/unpack
    $event_num++;
  }
}

#------------------------------------------------------------------------------

my $reqs =
  [
   # Version 3.0

   ['SyncInitialize',  # 0
    sub {
      my ($X, $major, $minor) = @_;
      return pack 'CCxx', $major, $minor;
    },
    sub {
      my ($X, $data) = @_;
      return unpack 'x8CC', $data;
    } ],

   ['SyncListSystemCounters',  # 1
    \&_request_empty,
    sub {
      my ($X, $data) = @_;
      ### SyncListSystemCounters reply(): length($data), $data
      my ($ncounters) = unpack 'x8L', $data;
      ### $ncounters
      my @ret;
      my $pos = 32;
      foreach (1 .. $ncounters) {
        ### at: $pos, substr($data,$pos)

        my ($counter, $resolution_hi, $resolution_lo, $name_len)
          = unpack 'LlLS', substr($data,$pos,14); # 4+8+2=14
        ### elem: [ $counter, $resolution_hi, $resolution_lo, $name_len ]
        $pos += 14;

        my $name = substr ($data, $pos, $name_len);
        $pos += $name_len;
        $pos += X11::Protocol::padding($pos);

        push @ret, [ $counter,
                     _hilo_to_int64($resolution_hi,$resolution_lo),
                     $name ];
      }
      return @ret;
    }],

   ['SyncCreateCounter',  # 2
    sub {
      my ($X, $counter, $initial) = @_;
      return pack 'L3', $counter, _int64_to_hilo($initial);
    },
   ],

   ['SyncSetCounter',  # 3
    sub {
      my ($X, $counter, $value) = @_;
      ### SyncSetCounter() ...
      return pack 'L3', $counter, _int64_to_hilo($value);
    },
   ],

   ['SyncChangeCounter',  # 4
    sub {
      my ($X, $counter, $value) = @_;
      return pack 'L3', $counter, _int64_to_hilo($value);
    },
   ],

   ['SyncQueryCounter',  # 5
    \&_request_card32s, # ($X, $counter)
    sub {
      my ($X, $data) = @_;
      return _hilo_to_int64 (unpack 'x8LL', $data);
    },
   ],

   ['SyncDestroyCounter',  # 6
    \&_request_card32s, # ($X, $counter)
   ],

   ['SyncAwait',  # 7
    \&_request_empty,
   ],

   ['SyncCreateAlarm',  # 8
    \&_request_alarm_parameters,
   ],

   ['SyncChangeAlarm',  # 9
    \&_request_alarm_parameters,
   ],

   ['SyncQueryAlarm',  # 10
    \&_request_card32s, # ($X, $alarm)
    sub {
      my ($X, $data) = @_;
      ### SyncQueryAlarm() reply ...

      # use Data::HexDump::XXD;
      # print scalar(Data::HexDump::XXD::xxd($data));
      # print "\n";

      my ($counter, $value_type, $value_hi,$value_lo,
          $test_type, $delta_hi,$delta_lo,
          $events, $state)
        = unpack 'x8L7CC', $data;

      return (counter    => $counter,
              value      => _hilo_to_int64($value_hi,$value_lo),
              test_type  => $X->interp('SyncTestType',$test_type),
              value_type => $X->interp('SyncValueType',$value_type),
              delta      => _hilo_to_int64($delta_hi,$delta_lo),
              events     => $events,
              state      => $X->interp('SyncAlarmState',$state));
    } ],

   ['SyncDestroyAlarm',  # 11
    \&_request_card32s, # ($X, $alarm)
   ],

   ['SyncSetPriority',  # 12
    sub {
      my ($X, $xid, $priority) = @_;
      return pack 'Ll', _num_none($xid), $priority;
    }],
   ['SyncGetPriority',  # 13
    \&_request_xids, # ($X, $xid)
    sub {
      my ($X, $data) = @_;
      return unpack 'x8l', $data;
    }],

   #------------------------
   # version 3.1

   ['SyncCreateFence',  # 14
    sub {
      my ($X, $fence, $drawable, $initially_triggered) = @_;
      return pack 'LLCxxx', $drawable, $fence, $initially_triggered;
    }],

   ['SyncTriggerFence',  # 15
    \&_request_card32s, # ($X, $fence)
   ],

   ['SyncResetFence',  # 16
    \&_request_card32s, # ($X, $fence)
   ],

   ['SyncDestroyFence',  # 17
    \&_request_card32s, # ($X, $fence)
   ],

   ['SyncQueryFence',  # 18
    \&_request_card32s, # ($X, $fence)
    sub {
      my ($X, $data) = @_;
      return unpack 'x8C', $data;
    } ],

   ['SyncAwaitFence',  # 19
    \&_request_card32s, # ($X, $fence,...)
   ],
  ];

{
  my @keys = ('counter',
              'value_type',
              'value',
              'test_type',
              'delta',
              'events');
  my %key_to_conversion = (value => \&_int64_to_hilo,
                           delta => \&_int64_to_hilo);
  my %key_to_interp = (value_type => 'SyncValueType',
                       test_type  => 'SyncTestType');

  sub _request_alarm_parameters {
    my ($X, $alarm, %h) = @_;
    my $mask = 0;
    my @args;
    my $i;
    foreach $i (0 .. $#keys) {
      my $key = $keys[$i];
      next unless exists $h{$key};

      my $arg = delete $h{$key};
      $mask |= (1 << $i);

      if (my $conversion = $key_to_conversion{$key}) {
        push @args, &$conversion($arg);
      } else {
        if (my $interp = $key_to_interp{$key}) {
          $arg = $X->num($interp,$arg);
        }
        push @args, $arg;
      }
    }
    if (%h) {
      croak "Unrecognised alarm parameter(s): ",join(',',keys %h);
    }
    ### $mask
    ### @args
    return pack 'L*', $alarm, $mask, @args;
  }
}


#------------------------------------------------------------------------------

sub new {
  my ($class, $X, $request_num, $event_num, $error_num) = @_;
  ### Sync new()

  my $self = bless { }, $class;
  _ext_requests_install ($X, $request_num, $reqs);
  _ext_constants_install ($X, [ $self->constants_list ]);
  _ext_events_install ($X, $event_num, [ $self->events_list ]);

  # SYNC spec says must initialize or behaviour undefined (though for
  # example the X.org server doesn't enforce this).  Also we want to know
  # whether 3.0 or 3.1 so that the Fence error type can be setup or not.
  #
  my ($major, $minor) = $X->req('SyncInitialize',
                                CLIENT_MAJOR_VERSION,
                                CLIENT_MINOR_VERSION);
  $self->{'major'} = $major;
  $self->{'minor'} = $minor;

  # Errors
  _ext_const_error_install ($X, $error_num,
                            'Counter',  # 0
                            'Alarm',    # 1

                            # Fence new in 3.1
                            (($major <=> 3 || $minor <=> 1) >= 0
                             ? ('Fence') : ()));   # 2
  return $self;
}


#------------------------------------------------------------------------------
# generic

sub _num_time {
  my ($time) = @_;
  if (defined $time && $time eq 'CurrentTime') {
    return 0;
  } else {
    return $time;
  }
}
sub _interp_time {
  my ($time) = @_;
  if ($time == 0) {
    return 'CurrentTime';
  } else {
    return $time;
  }
}

sub _num_none {
  my ($xid) = @_;
  if (defined $xid && $xid eq "None") {
    return 0;
  } else {
    return $xid;
  }
}


sub _ext_requests_install {
  my ($X, $request_num, $reqs) = @_;

  $X->{'ext_request'}->{$request_num} = $reqs;
  my $href = $X->{'ext_request_num'};
  my $i;
  foreach $i (0 .. $#$reqs) {
    $href->{$reqs->[$i]->[0]} = [$request_num, $i];
  }
}
sub _ext_const_error_install {
  my $X = shift;  # ($X, $errname1,$errname2,...)
  ### _ext_const_error_install: @_
  my $error_num = shift;
  my $aref = $X->{'ext_const'}{'Error'}  # copy
    = [ @{$X->{'ext_const'}{'Error'} || []} ];
  my $href = $X->{'ext_const_num'}{'Error'}  # copy
    = { %{$X->{'ext_const_num'}{'Error'} || {}} };
  my $i;
  foreach $i (0 .. $#_) {
    $aref->[$error_num + $i] = $_[$i];
    $href->{$_[$i]} = $error_num + $i;
  }
}

sub _request_empty {
  # ($X)
  if (@_ > 1) {
    croak "No parameters in this request";
  }
  return '';
}
sub _request_card32s {
  shift;
  ### _request_card32s(): @_
  return pack 'L*', @_;
}
sub _request_xids {
  my $X = shift;
  ### _request_xids(): @_
  return _request_card32s ($X, map {_num_none($_)} @_);
}

#------------------------------------------------------------------------------
# 64-bits

# -2^64 + $hi*2^32 + $lo
# = 2^32 * (-2^32 + $hi) + $lo
#
# -2^64 + $hi*2^32 + $lo
# = -2^64 + ($hi-2^31+2^31)*2^32 + $lo
# = -2^64 + 2^63 + ($hi-2^31)*2^32 + $lo
# = -2^63 + ($hi-2^31)*2^32 + $lo
#
# Crib: "<<" shift operator turns a negative into a positive, so must shift
# $hi as positive then adjust.

use constant _INT_BITS => do {
  # In Perl 5.14.2 with -T taint mode, $lo += -(1<<63) becomes an NV not a
  # UV making the int64 return lose precision.  $lo is tainted by being a
  # value read from the server.  Not sure what can be relied on for integer
  # arithmetic in taint mode, but for now treat it as 32-bit Perl.

  # $n = tainted zero.  It's assumed there will be something in %ENV which
  # is tainted.  Normally everything in %ENV is tainted, but it's not
  # uncommon to wash $PATH.  Could use Taint::Util::taint(), but don't
  # really want to demand that module.
  my $n = '0' . join ('', map {defined $_ && substr($_,0,0)} values %ENV);

  my $bits = 0;
  for (;;) {
    $bits++;
    $n *= 2;
    my $n2 = $n;
    $n += 1;
    if ($n <= $n2 || $n >= $n2 + 2) {
      # floating point round-off, stop
      last;
    }
    if ($bits >= 64) {
      # enough good bits for our purposes, stop
      last;
    }
  }
  ### $bits
  # Devel::Peek::Dump ($n);
  $bits;
};

if (_INT_BITS >= 64) {
  ### 64-bit UV ...

  eval "\n#line ".(__LINE__+1)." \"".__FILE__."\"\n" . <<'HERE' or die;
sub _hilo_to_int64 {
  my ($hi,$lo) = @_;
  ### _hilo_to_int64() ...
  ### $hi
  ### $lo
  if ($hi & 0x8000_0000) {
    $hi -= 0x8000_0000;
    $lo += -(1<<63);
    ### twos-complement negative to ...
    ### $hi
    ### $lo
    ### lo hex: sprintf '%X', $lo
  }
  ### hi shift: $hi<<1
  ### result: ($hi << 32) + $lo
  return ($hi << 32) + $lo;
}
1;
HERE
} else {
  ### 32-bit UV (or anything less than 64) ...

  eval "\n#line ".(__LINE__+1)." \"".__FILE__."\"\n" . <<'HERE' or die;
use Math::BigInt;
sub _hilo_to_int64 {
  my ($hi,$lo) = @_;
  # print "_hilo_to_int64() hi=$hi lo=$lo\n";

  my $ret = Math::BigInt->new("$hi") * (Math::BigInt->new(2) ** 32) + $lo;
  if ($hi & 0x8000_0000) {
    $ret -= Math::BigInt->new(2) ** 64;
  }
  ### $ret
  return $ret;
}
1;
HERE
}

# NV floats are converted to UV for bitwise ">>" or "&", which would lose
# some of the mantissa if UV==32bits, so use "%".
#
# Divisor 2^32 would be an NV float if UV==32bits, so do two divisions by
# 65536.
#
# Math::BigInt in perl 5.6.0 has a bug in the division where it mis-handles
# the sign of the dividend, giving for example -16 div 8 = -1 rem +7, where
# it should be -2 rem +1 (or perhaps -1 rem -7 if a negative remainder).
# Avoid that by making $sv positive and bit-invert the resulting $hi,$lo.
#
# For reference, Math::BigInt in perl 5.6.0 had int() returning a plain
# string, not a BigInt object, so avoid that.
#
sub _int64_to_hilo {
  my ($sv) = @_;
  ### _int64_to_hilo(): $sv
  # print "_int64_to_hilo() sv=$sv ",(ref $sv || '[plain scalar]'),"\n";

  my $xor = 0;
  if ($sv < 0) {
    $sv = -1 - $sv;
    $xor = 0xFFFF;
  }

  ($sv, my $lo) = _divrem($sv,65536);
  $lo ^= $xor;
  ($sv, my $lo2) = _divrem($sv,65536);
  $lo2 ^= $xor;
  $lo += 65536*$lo2;

  ($sv, my $hi) = _divrem($sv,65536);
  $hi ^= $xor;
  ($sv, my $hi2) = _divrem($sv,65536);
  $hi2 ^= $xor;
  $hi += 65536*$hi2;

  ### $hi
  ### $lo
  return ($hi, $lo)
}

sub _divrem {
  my ($n, $d) = @_;
  my $rem = $n % $d;
  return (($n-$rem)/$d, $rem);
}

1;
__END__

=for stopwords SYNC XID Ryde Pre-defined SERVERTIME timestamp IDLETIME BigInts arrayref ie unsatisfy untriggered XIDs builtin ENUM SyncValueType SyncTestType SyncAlarmState

=head1 NAME

X11::Protocol::Ext::SYNC - inter-client synchronization

=head1 SYNOPSIS

 use X11::Protocol;
 my $X = X11::Protocol->new;
 $X->init_extension('SYNC')
   or print "SYNC extension not available";

=head1 DESCRIPTION

The SYNC extension adds

=over

=item *

Counter objects, counting in 64-bits client controlled or server builtin.

=item *

Alarm objects to receive events for counter values.

=item *

Priority level for clients.

=item *

Fence objects triggered by completion of screen rendering.  New in SYNC
version 3.1.

=back

Counters and alarms allow multiple clients to synchronize their actions.
One client can create a counter and increment it.  Other clients can block
or receive events for desired target values.

Counter values are INT64 signed 64-bit integers, so -2^63 to +2^63-1
inclusive.  On a 64-bit Perl these values are returned as plain integers.
On a 32-bit Perl they're returned as C<Math::BigInt> objects.  For requests
values can be given as either integers, floating point, or BigInts.

=head2 Client Counters

Client counters are changed by C<SyncChangeCounter()> or C<SyncSetCounter()>
requests.  The meaning of a counter value and when and by how much it
changes is entirely up to client programs.

Client counters do not wrap-around.  If an increment overflows the -2^63 to
+2^63-1 range then a C<BadValue> results, or alarms becomes Inactive when
adding their C<delta> overflows the INT64 range.

=head2 System Counters

Pre-defined system counters are controlled by the server.  As of SYNC
specification 3.1 the only system counter always available is "SERVERTIME".
A particular server might have more.

=over

=item "SERVERTIME"

The server timestamp in milliseconds.  This is per the C<time> field of
server events etc.

=back

Recent versions of the X.org server (1.10 or thereabouts) have

=over

=item "IDLETIME"

The idle time in milliseconds, being the time since any device input.  This
is the same as the idle time in C<MitScreenSaverQueryInfo()> (see
L<X11::Protocol::Ext::MIT_SCREEN_SAVER>).

=back

=head1 REQUESTS

The following requests are made available with an C<init_extension()>, as
per L<X11::Protocol/EXTENSIONS>.

    my $is_available = $X->init_extension('SYNC');

=over

=item C<($server_major, $server_minor) = $X-E<gt>SyncInitialize ($client_major, $client_minor)>

Negotiate a protocol version with the server.  C<$client_major> and
C<$client_minor> is what the client would like.  The returned
C<$server_major> and C<$server_minor> is what the server will do.

This negotiation request is made automatically in C<init_extension()> and
the SYNC spec says it should not be done again later.  The current module
code supports up to SYNC version 3.1 and that version is requested.  The
version the server returns is stored in the extension object,

    my $extobj = $X->{'ext'}->{'SYNC'}->[3];
    $major = $extobj->{'major'};
    $minor = $extobj->{'minor'};

=item C<@infos = $X-E<gt>SyncListSystemCounters ($client_major, $client_minor)>

Return a list of the server-defined counters.  Each return value is an
arrayref

    [ $counter, $resolution, $name ]

C<$counter> is the XID (an integer) of the counter.

C<$resolution> is an estimate of the granularity of the counter.  For
example if resolution is 10 then it might increment by 10 or thereabouts
each time.

C<$name> is a string name of the counter.

See F<examples/sync-info.pl> in the X11-Protocol-Other sources for a
complete program listing the system counters.

=item C<$X-E<gt>SyncCreateCounter ($counter, $value)>

Create C<$counter> (a new XID) as a counter with initial value C<$value> (an
INT64).

=item C<$X-E<gt>SyncSetCounter ($counter, $value)>

Set C<$counter> (an XID) to C<$value> (64-bit integer).  The server system
counters cannot be changed by clients.

=item C<$X-E<gt>SyncChangeCounter ($counter, $add)>

Change C<$counter> (an XID) by adding C<$add> (64-bit integer) to it.  The
server system counters cannot be changed by clients.

If C<$add> would make the resulting counter overflow a 64-bit integer then a
C<BadValue> error results.

=item C<$value = $X-E<gt>SyncQueryCounter ($counter)>

Return the current value of C<$counter> (an XID).

=item C<$X-E<gt>SyncDestroyCounter ($counter)>

Destroy C<$counter> (an XID).

Any clients currently waiting on C<$counter> are sent a C<SyncCounterNotify>
event with the C<destroyed> field true.  Any alarms on C<$counter> become
state "Inactive".  A client's counters are destroyed automatically on
connection close.  System counters cannot be destroyed.

=back

=head2 Alarms and Waiting

=over

=item C<$X-E<gt>SyncAwait ([$key=E<gt>$value,...],...)>

Block processing of further requests from the current client until one of
the given counter conditions is satisfied.  The call C<$X-E<gt>SyncAwait()>
returns immediately, but any further requests sent on C<$X> will not be read
by the server until the wait is satisfied.

If one of the wait conditions is already satisfied then the block is for no
time.  The C<event_threshold> events described below are still generated in
this case.

Each condition is an arrayref of key/value pairs

    counter           the target counter (integer XID)
    value_type        "Absolute" or "Relative"
    value             target value (INT64 signed integer)
    test_type         "PositiveTransition", "NegativeTransition",
                      "PositiveComparison" or "NegativeComparison"
    event_threshold   possible difference (INT64 signed integer)

For example to wait on two counters

    $X->SyncAwait ([ counter    => $c1,
                     value_type => "Absolute",
                     value      => 1000,
                     test_type  => "PositiveComparison",
                     event_threshold => 100 ],
                   [ counter    => $c2,
                     value_type => "Absolute",
                     value      => 500,
                     test_type  => "NegativeTransition",
                     event_threshold => 100 ]);

C<test_type> is how the condition will be satisfied,

    "PositiveComparison"   whenever counter >= value
    "NegativeComparison"   whenever counter <= value
    "PositiveTransition"   change from counter<value to counter>=value
    "NegativeTransition"   change from counter>value to counter<=value

C<value_type> is how C<value> is interpreted

    "Absolute"     target value as given
    "Relative"     target value is counter current value + given value

For "Absolute" the C<counter> can be "None" and that's considered satisfied
immediately.  For "Relative" each C<counter> must be a valid counter.  If
adding the relative amount overflows an INT64 then a C<BadValue> error
results.

If any of the counters is destroyed during C<SyncAwait()> then the wait
finishes and a C<CounterNotify> event with the C<destroyed> flag is
generated.

When C<SyncAwait()> finishes, the C<event_threshold> can generate
C<CounterNotify> events for the client.  The difference

    diff = counter - target value

is compared to the given C<event_threshold>

    if diff >= event_threshold for "Positive"
    or diff <= event_threshold for "Negative"
    then send CounterNotify

This is designed to alert the client that a counter has run on by more than
a threshold amount.  This could be due to lag, or perhaps a jump in the
value.

=item C<$X-E<gt>SyncCreateAlarm ($alarm, $key=E<gt>$value, ...)>

=item C<$X-E<gt>SyncChangeAlarm ($alarm, $key=E<gt>$value, ...)>

Create C<$alarm> (a new XID) as an alarm, or change the parameters of an
existing C<$alarm>.  The key/value parameters are similar to C<SyncAwait()>
above,

    counter       the target counter (integer XID)
    value_type    "Absolute" or "Relative"
    value         target value (64-bit signed integer)
    test_type     "PositiveTransition", "NegativeTransition",
                  "PositiveComparison" or "NegativeComparison"
    delta         step target value (64-bit signed, default 1)
    events        boolean (default true)

All the parameters have defaults, so an alarm can be created with no counter
etc at all just by

    my $alarm = $X->new_rsrc;
    $X->SyncCreateAlarm ($alarm);

C<counter> "None" (0) or omitted makes the alarm "Inactive".

C<delta> is added to C<value> when the alarm is satisfied.  C<delta> is
added repeatedly if necessary to make it unsatisfied, ie. add smallest
necessary multiple of C<delta> to become unsatisfied.  The default C<delta>
of 1 means C<value> has 1 added until unsatisfied again, so reset the alarm
target to counter value+1.

If adding C<delta> this way would overflow an INT64, or if C<delta> is 0 in
a "Comparison" test (and thus no amount of adding will unsatisfy), then the
alarm value is unchanged and the alarm set "Inactive" instead.  Setting
C<delta> to 0 therefore makes a "once-only" alarm.

C<delta> must be positive or negative in the same direction as the
C<test_type> or a C<Match> error results.

    "Positive"     must have delta >= 0
    "Negative"     must have delta <= 0

If C<events> is true then when the alarm is satisfied an C<AlarmNotify>
event is generated.  If the C<delta> caused the alarm to become "Inactive"
then the C<state> field in the event will show it Inactive.

The C<events> flag is a per-client setting.  Each client can individually
select or deselect events from any alarm using C<SyncChangeAlarm()>,

    $X->SyncChangeAlarm ($alarm, events => $bool);

If an error results (bad type, bad counter, etc) then the SYNC specification
allows some of the request changes to have been applied but others not.

=item C<@list = $X-E<gt>SyncQueryAlarm ($alarm)>

Return the current parameters of C<$alarm> (integer XID) in the form of a
key/value list like C<SyncCreateAlarm()> above.

For reference, in the X.org server circa its version 1.10 if C<value_type>
is set to "Relative" then it reads back as "Absolute" with a C<value> which
is the target counter+relative_value.  Not sure what the SYNC spec says
about this.

=item C<$X-E<gt>SyncDestroyAlarm ($alarm)>

Destroy C<$alarm> (an XID).

=back

=head1 Client Priority

=over

=item C<$X-E<gt>SyncSetPriority ($xid, $priority)>

=item C<$priority = $X-E<gt>SyncGetPriority ($xid)>

Get or set a client's scheduling priority level in the server.  C<$xid> is
any XID belonging to the desired client, or "None" (0) for the current
client.  C<$priority> is an INT32 integer.  Higher numbers are higher
priority.  The default priority is 0.

    $X->SyncSetPriority ("None", 100);   # higher priority

    $X->SyncSetPriority ("None", -123);  # lower priority

Setting a client to high priority may help it do smooth animations etc.
A high priority client might have to be careful that it doesn't flood the
server with requests which starve other clients.  The server may or may not
actually do anything with the priority level.

=back

=head2 Fence

Fences are new in SYNC version 3.1.  A fence represents completion of all
queued drawing in the server.  It can be in "triggered" or "untriggered"
state.  Clients can ask for trigger when drawing has completed, and then
either query or block waiting for that to occur.

=over

=item C<$X-E<gt>SyncCreateFence ($fence, $drawable, $initially_triggered)>

Create C<$fence> (a new XID) as a fence on the screen of C<$drawable>.

=item C<$X-E<gt>SyncTriggerFence ($fence)>

Ask the server to set C<$fence> (XID) to triggered state when all drawing
requests currently in progress on the screen of C<$fence> have completed.
This is all drawing from both the current client and other clients.  If
C<$fence> is already triggered then do nothing.

If a simple server does all drawing direct to video memory with no queuing
then C<$fence> will be triggered immediately.  If the server or graphics
card has some sort or rendering pipeline or queue then C<$fence> is
triggered only when the drawing requests issued so far have reached the
actual screen.

=item C<$X-E<gt>SyncResetFence ($fence)>

Reset C<$fence> (an XID) from triggered to untriggered state.  A C<Match>
error results if C<$fence> is not currently in triggered state.

=item C<$X-E<gt>SyncDestroyFence ($fence)>

Destroy C<$fence> (an XID).

=item C<$triggered = $X-E<gt>SyncQueryFence ($fence)>

Get the current triggered state of C<$fence> (an XID).  The return is 0 if
untriggered or 1 if triggered.

=item C<$X-E<gt>SyncAwaitFence ($fence1, $fence2, ...)>

Block the processing of further requests from the current client until one
or more of the given C<$fence> XIDs is in triggered state.  If one of the
fences is already currently triggered then there's no block and request
processing continues immediately.

=back

=head1 EVENTS

Each event has the usual fields

    name             "SyncCounterNotify" etc
    synthetic        true if from a SendEvent
    code             integer opcode
    sequence_number  integer

plus event-specific fields described below.

=over

=item C<SyncCounterNotify>

A C<SyncCounterNotify> event is generated when a C<SyncAwait()> request is
unblocked by one or more of its requested conditions being satisfied.

The event-specific fields are

    time           integer, server timestamp
    counter        integer XID
    wait_value     INT64
    counter_value  INT64
    destroyed      bool, 0 or 1
    count          integer, how many more SyncCounterNotify

If multiple conditions in the C<SyncAwait()> have been satisfied then each
one results in a C<SyncCounterNotify> event.  The C<count> field is how many
more such C<SyncCounterNotify> are following the present one (0 if no more).

C<destroyed> is 1 if the C<counter> was destroyed during the C<SyncAwait()>.

=item C<SyncAlarmNotify>

A C<SyncAlarmNotify> is generated when an alarm object is triggered and its
C<events> flag is true for this client.

The event-specific fields are

    time           integer, server timestamp
    alarm          integer XID
    alarm_value    INT64
    counter_value  INT64
    state          enum "Active", "Inactive", or "Destroyed"

=back

=head1 ENUM TYPES

The following types are available for C<$X-E<gt>interp()> and
C<$X-E<gt>num()>, after C<init_extension()>.

=over

=item SyncValueType

    "Absolute"     0
    "Relative"     1

=item SyncTestType

    "PositiveTransition"     0
    "NegativeTransition"     1
    "PositiveComparison"     2
    "NegativeComparison"     3

=item SyncAlarmState

    "Active"        0
    "Inactive"      1
    "Destroyed"     2

=back

For example,

    my $num = $X->num("SyncTestType", "PositiveComparison");
    # sets $num to 2

=head1 ERRORS

The extension error types are

    "Counter"
    "Alarm"
    "Fence"         # if server has SYNC 3.1

which are respectively a bad C<$counter>, C<$alarm> or C<$fence> resource
XID in a request.

=head1 SEE ALSO

L<X11::Protocol>

F</usr/share/doc/x11proto-xext-dev/sync.txt.gz>,
F</usr/share/X11/doc/hardcopy/Xext/sync.PS.gz>

=head1 HOME PAGE

L<http://user42.tuxfamily.org/x11-protocol-other/index.html>

=head1 LICENSE

Copyright 2011, 2012, 2013 Kevin Ryde

X11-Protocol-Other is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any later
version.

X11-Protocol-Other is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
more details.

You should have received a copy of the GNU General Public License along with
X11-Protocol-Other.  If not, see <http://www.gnu.org/licenses/>.

=cut

# The C<Math::BigInt> which is included in Perl 5.6.0 and thereabouts has a
# couple of dubious bits.  Believe it suffices for adding and subtracting
# but it might be necessary to demand a newer version for more involved
# calculations.  Working with an overloaded type for the 64-bits is much
# more convenient than two 32-bit parts.