| 1 | |||
| 2 | # The X Sychronization Extension Protocol | ||
| 3 | package X11::Protocol::Ext::SYNC; | ||
| 4 | |||
| 5 | use X11::Protocol qw(make_num_hash); | ||
| 6 | use Carp; | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use vars '$VERSION'; | ||
| 10 | $VERSION = 0.01; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | X11::Protocol::Ext::SYNC -- Perl extention module for X Synchronization Extension Protocol | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | use X11::Protocol; | ||
| 19 | $x = X11::Protocol->new($ENV{DISPLAY}); | ||
| 20 | $x->init_extension('SYNC') or die; | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | This module is used by the L<X11::Protocol(3pm)> module to participate | ||
| 25 | in the synchronization extension to the X protocol, allowing | ||
| 26 | time synchronization between clients and to absolute real-time sources | ||
| 27 | per the L<X Synchronization Extension Protocol>, a copy of which can be | ||
| 28 | obtained from L<http://www.x.org/releases/X11R7.7/doc/xextproto/sync.pdf>. | ||
| 29 | |||
| 30 | This manual page does not attempt to document the protocol itself, see | ||
| 31 | the specification for that. It documents the L</CONSTANTS>, | ||
| 32 | L</REQUESTS>, L</EVENTS> and L</ERRORS> that are added to the | ||
| 33 | L<X11::Protocol(3pm)> module. | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | sub new | ||
| 38 | { | ||
| 39 | my $self = bless {}, shift; | ||
| 40 | my ($x, $request_base, $event_base, $error_base) = @_; | ||
| 41 | |||
| 42 | =head1 CONSTANTS | ||
| 43 | |||
| 44 | B<X11::Protocol::Ext::SYNC> provides the following symbolic constants: | ||
| 45 | |||
| 46 | ValueType => [ 'Absolute', 'Relative' ] | ||
| 47 | TestType => [ 'PositiveTransition', 'NegativeTransition', | ||
| 48 | 'PositiveCommparison', 'NegativeComparison' ] | ||
| 49 | AlarmState => [ 'Active', 'Inactive', 'Destroyed' ] | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | $x->{ext_const}{ValueType} = [ qw(Absolute Relative) ]; | ||
| 54 | $x->{ext_const}{TestType} = [ qw(PositiveTransition | ||
| 55 | NegativeTransition | ||
| 56 | PositiveComparison | ||
| 57 | NegativeComparison) ]; | ||
| 58 | $x->{ext_const}{AlarmState} = [ qw(Active Inactive Destroyed) ]; | ||
| 59 | |||
| 60 | $x->{ext_const_num}{$_} = {make_num_hash($x->{ext_const}{$_})} | ||
| 61 | foreach (qw(ValueType TestType AlarmState)); | ||
| 62 | |||
| 63 | =head1 ERRORS | ||
| 64 | |||
| 65 | B<X11::Protocol::Ext::SYNC> provides the folowing bad resource errors: | ||
| 66 | C<Counter>, C<Alarm>, C<Fence>. | ||
| 67 | |||
| 68 | =cut | ||
| 69 | { | ||
| 70 | my $i = $error_base; | ||
| 71 | foreach (qw(Counter Alarm Fence)) { | ||
| 72 | $x->{ext_const}{Error}[$i] = $_; | ||
| 73 | $x->{ext_const_num}{Error}{$_} = $i; | ||
| 74 | $x->{ext_error_type}[$i] = 1; # bad resource | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | =head1 REQUESTS | ||
| 79 | |||
| 80 | B<X11::Protocol::Ext::SYNC> provides the following requests: | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | =pod | ||
| 85 | |||
| 86 | $X->XsyncInitialize($major,$minor) | ||
| 87 | => | ||
| 88 | ($major,$minor) | ||
| 89 | =cut | ||
| 90 | $x->{ext_request}{$request_base}[0] = | ||
| 91 | [XsyncInitialize => sub{ # 0 | ||
| 92 | return pack('CCxx',@_[1..2]); | ||
| 93 | }, sub { | ||
| 94 | return unpack('x[8]CCx[22]',$_[1]); | ||
| 95 | }]; | ||
| 96 | =pod | ||
| 97 | |||
| 98 | $X->ListSystemCounters() | ||
| 99 | => | ||
| 100 | ( [ $counter,$resolution_hi,$resolution_lo ], ... ) | ||
| 101 | =cut | ||
| 102 | $x->{ext_request}{$request_base}[1] = | ||
| 103 | [ListSystemCounters => sub{ # 1 | ||
| 104 | return ''; | ||
| 105 | }, sub { | ||
| 106 | my ($x,$data) = @_; | ||
| 107 | my ($llen,@counters) = | ||
| 108 | unpack('xxxxxxxxlxxxxxxxxxxxxxxxxxxxx',$data); | ||
| 109 | my $counters = substr($data,32); | ||
| 110 | while ($llen>0 and length($counters)>=16) { | ||
| 111 | my ($counter,$res_hi,$res_lo,$len,$name) = | ||
| 112 | unpack('LLLS',$counters); | ||
| 113 | $counters = substr($counters,14); | ||
| 114 | $name = substr($counters,0,$len); | ||
| 115 | $counters = substr($counters,$len+(-$len&3)); | ||
| 116 | push @counters, [ $counter, $res_hi, $res_lo, $name, ]; | ||
| 117 | } | ||
| 118 | return (@counters); | ||
| 119 | }]; | ||
| 120 | =pod | ||
| 121 | |||
| 122 | $X->CreateCounter($id,($initial_value_hi,$initial_value_lo)) | ||
| 123 | =cut | ||
| 124 | $x->{ext_request}{$request_base}[2] = | ||
| 125 | [CreateCounter => sub{ # 2 | ||
| 126 | return pack('LLL',$_[1..3]); | ||
| 127 | }]; | ||
| 128 | =pod | ||
| 129 | |||
| 130 | $X->SetCounter($counter,($value_hi,$value_lo)) | ||
| 131 | =cut | ||
| 132 | $x->{ext_request}{$request_base}[3] = | ||
| 133 | [SetCounter => sub{ # 3 | ||
| 134 | return pack('LLL',$_[1..3]); | ||
| 135 | }]; | ||
| 136 | =pod | ||
| 137 | |||
| 138 | $X->ChangeCounter($counter,($amount_hi,$amount_lo)) | ||
| 139 | =cut | ||
| 140 | $x->{ext_request}{$request_base}[4] = | ||
| 141 | [ChangeCounter => sub{ # 4 | ||
| 142 | return pack('LLL',$_[1..3]); | ||
| 143 | }]; | ||
| 144 | =pod | ||
| 145 | |||
| 146 | $X->DestroyCounter($counter) | ||
| 147 | => | ||
| 148 | ($counter_hi,$counter_lo) | ||
| 149 | =cut | ||
| 150 | $x->{ext_request}{$request_base}[6] = | ||
| 151 | [DestroyCounter => sub{ # 6 | ||
| 152 | return pack('L',$_[1]); | ||
| 153 | }, sub { | ||
| 154 | return unpack('xxxxxxxxLLxxxxxxxxxxxxxxxx',$_[1]); | ||
| 155 | }]; | ||
| 156 | =pod | ||
| 157 | |||
| 158 | $X->Await([$trigger,($thresh_hi,$thresh_lo)], ...) | ||
| 159 | =cut | ||
| 160 | $x->{ext_request}{$request_base}[7] = | ||
| 161 | [Await => sub{ # 7 | ||
| 162 | shift; return join('',map{pack('a[20]LL',@$_)}@_); | ||
| 163 | }]; | ||
| 164 | =pod | ||
| 165 | |||
| 166 | $X->CreateAlarm($id, | ||
| 167 | counter=>$counter, | ||
| 168 | value_type=>$value_type, | ||
| 169 | value=>[$value_hi,$value_lo], | ||
| 170 | test_type=>$test_type, | ||
| 171 | delta=>[$delta_hi,$delta_lo], | ||
| 172 | events=>$events) | ||
| 173 | =cut | ||
| 174 | $x->{ext_request}{$request_base}[8] = | ||
| 175 | [CreateAlarm => sub{ # 8 | ||
| 176 | my ($x,$id,%values) = @_; | ||
| 177 | my ($i,$mask,$pack,$n) = (0,0,'',0); | ||
| 178 | foreach (qw(counter value_type value test_type delta events)) { | ||
| 179 | if ($values{$_}) { | ||
| 180 | $mask |= (1<<$i); | ||
| 181 | $values{$_} = $x->num('ValueType',$values{$_}) | ||
| 182 | if $_ eq 'value_type'; | ||
| 183 | $values{$_} = $x->num('TestType',$values{$_}) | ||
| 184 | if $_ eq 'test_type'; | ||
| 185 | $values{$_} = $x->num('Bool',$values{$_}) | ||
| 186 | if $_ eq 'events'; | ||
| 187 | if ($_ eq 'delta' or $_ eq 'value') { | ||
| 188 | $pack .= pack('LL',@{$values{$_}}); | ||
| 189 | } else { | ||
| 190 | $pack .= pack('L',$values{$_}); | ||
| 191 | } | ||
| 192 | $n++; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | return pack('L',$id) . $pack; | ||
| 196 | }]; | ||
| 197 | =pod | ||
| 198 | |||
| 199 | $X->ChangeAlarm($id, | ||
| 200 | counter=>$counter, | ||
| 201 | value_type=>$value_type, | ||
| 202 | value=>[$value_hi,$value_lo], | ||
| 203 | test_type=>$test_type, | ||
| 204 | delta=>[$delta_hi,$delta_lo], | ||
| 205 | events=>$events) | ||
| 206 | =cut | ||
| 207 | $x->{ext_request}{$request_base}[9] = | ||
| 208 | [ChangeAlarm => sub{ # 9 | ||
| 209 | my ($x,$id,%values) = @_; | ||
| 210 | my ($i,$mask,$pack,$n) = (0,0,'',0); | ||
| 211 | foreach (qw(counter value_type value test_type delta events)) { | ||
| 212 | if ($values{$_}) { | ||
| 213 | $mask |= (1<<$i); | ||
| 214 | $values{$_} = $x->num('ValueType',$values{$_}) | ||
| 215 | if $_ eq 'value_type'; | ||
| 216 | $values{$_} = $x->num('TestType',$values{$_}) | ||
| 217 | if $_ eq 'test_type'; | ||
| 218 | $values{$_} = $x->num('Bool',$values{$_}) | ||
| 219 | if $_ eq 'events'; | ||
| 220 | if ($_ eq 'delta' or $_ eq 'value') { | ||
| 221 | $pack .= pack('LL',@{$values{$_}}); | ||
| 222 | } else { | ||
| 223 | $pack .= pack('L',$values{$_}); | ||
| 224 | } | ||
| 225 | $n++; | ||
| 226 | } | ||
| 227 | } | ||
| 228 | return pack('L',$id) . $pack; | ||
| 229 | }]; | ||
| 230 | =pod | ||
| 231 | |||
| 232 | $X->QueryAlarm($alarm) | ||
| 233 | => | ||
| 234 | ($trigger,($delta_hi,$delta_lo),$events,$state) | ||
| 235 | =cut | ||
| 236 | $x->{ext_request}{$request_base}[10] = | ||
| 237 | [QueryAlarm => sub{ # 10 | ||
| 238 | return pack('L',$_[1]); | ||
| 239 | }, sub { | ||
| 240 | my $x = shift; | ||
| 241 | my @vals = unpack('xxxxxxxxa[20]LLCCxx',shift); | ||
| 242 | $vals[-2] = $x->interp('Bool',$vals[-2]); | ||
| 243 | $vals[-1] = $x->interp('AlarmState',$vals[-1]); | ||
| 244 | return @vals; | ||
| 245 | }]; | ||
| 246 | =pod | ||
| 247 | |||
| 248 | $X->DestroyAlarm($alarm) | ||
| 249 | =cut | ||
| 250 | $x->{ext_request}{$request_base}[11] = | ||
| 251 | [DestroyAlarm => sub{ # 11 | ||
| 252 | return pack('L',$_[1]); | ||
| 253 | }]; | ||
| 254 | =pod | ||
| 255 | |||
| 256 | $X->SetPriority($id,$priority) | ||
| 257 | =cut | ||
| 258 | $x->{ext_request}{$request_base}[12] = | ||
| 259 | [SetPriority => sub{ # 12 | ||
| 260 | return pack('Ll',$_[1..2]); | ||
| 261 | }]; | ||
| 262 | =pod | ||
| 263 | |||
| 264 | $X->GetPriority($id) | ||
| 265 | => | ||
| 266 | ($priority) | ||
| 267 | =cut | ||
| 268 | $x->{ext_request}{$request_base}[13] = | ||
| 269 | [GetPriority => sub{ # 13 | ||
| 270 | return pack('L',$_[1]); | ||
| 271 | }, sub { | ||
| 272 | return unpack('xxxxxxxxlxxxxxxxxxxxxxxxxxxxx',$_[1]); | ||
| 273 | }]; | ||
| 274 | =pod | ||
| 275 | |||
| 276 | $X->CreateFence($drawable,$id,$initially_triggered) | ||
| 277 | =cut | ||
| 278 | $x->{ext_request}{$request_base}[14] = | ||
| 279 | [CreateFence => sub{ # 14 | ||
| 280 | my ($x,@vals) = @_; | ||
| 281 | $vals[2] = $x->num('Bool',$vals[2]); | ||
| 282 | return pack('LLCxxx',@vals[0..2]); | ||
| 283 | }]; | ||
| 284 | =pod | ||
| 285 | |||
| 286 | $X->TriggerFence($id) | ||
| 287 | =cut | ||
| 288 | $x->{ext_request}{$request_base}[15] = | ||
| 289 | [TriggerFence => sub{ # 15 | ||
| 290 | return pack('L',$_[1]); | ||
| 291 | }]; | ||
| 292 | =pod | ||
| 293 | |||
| 294 | $X->ResetFence($id) | ||
| 295 | =cut | ||
| 296 | $x->{ext_request}{$request_base}[16] = | ||
| 297 | [ResetFence => sub{ # 16 | ||
| 298 | return pack('L',$_[1]); | ||
| 299 | }]; | ||
| 300 | =pod | ||
| 301 | |||
| 302 | $X->DestroyFence($id) | ||
| 303 | =cut | ||
| 304 | $x->{ext_request}{$request_base}[17] = | ||
| 305 | [DestroyFence => sub{ # 17 | ||
| 306 | return pack('L',$_[1]); | ||
| 307 | }]; | ||
| 308 | =pod | ||
| 309 | |||
| 310 | $X->QueryFence($id) | ||
| 311 | => | ||
| 312 | ($triggered) | ||
| 313 | =cut | ||
| 314 | $x->{ext_request}{$request_base}[18] = | ||
| 315 | [QueryFence => sub{ # 18 | ||
| 316 | return pack('L',$_[1]); | ||
| 317 | }, sub { | ||
| 318 | return $_[0]->interp('Bool',unpack('xxxxxxxxCxxxxxxxxxxxxxxxxxxxxxxx',$_[1])); | ||
| 319 | }]; | ||
| 320 | =pod | ||
| 321 | |||
| 322 | $X->AwaitFence(@ids) | ||
| 323 | =cut | ||
| 324 | $x->{ext_request}{$request_base}[19] = | ||
| 325 | [AwaitFence => sub{ # 19 | ||
| 326 | shift; return pack('L*',@_); | ||
| 327 | }]; | ||
| 328 | |||
| 329 | for (my $i=0;$i<scalar(@{$x->{ext_request}{$request_base}});$i++) { | ||
| 330 | my $req = $x->{ext_request}{$request_base}[$i]; | ||
| 331 | next unless $req; | ||
| 332 | $x->{ext_request_num}{$req->[0]} = [$request_base, $i]; | ||
| 333 | } | ||
| 334 | |||
| 335 | =head1 EVENTS | ||
| 336 | |||
| 337 | B<X11::Protocol::Ext::SYNC> provides the following events: | ||
| 338 | |||
| 339 | CounterNotify => { | ||
| 340 | counter=>$counter, | ||
| 341 | wait_value=>[$wait_value_hi,$wait_value_lo], | ||
| 342 | counter_value=>[$counter_value_hi,$counter_value_lo], | ||
| 343 | time=>$time, | ||
| 344 | count=>$count, | ||
| 345 | destroyed=>'True'|'False' } | ||
| 346 | =cut | ||
| 347 | $x->{ext_const}{Events}[$event_base+0] = 'CounterNotify'; | ||
| 348 | $x->{ext_events}[$event_base+0] = [sub{ | ||
| 349 | my ($x,$data,%h) = @_; | ||
| 350 | @_ = unpack('xxxxLLLLLLSCx',$data); | ||
| 351 | $h{counter} = $_[0]; | ||
| 352 | $h{wait_value} = [$_[1..2]]; | ||
| 353 | $h{counter_value} = [$_[3..4]]; | ||
| 354 | $h{time} = $_[5]; | ||
| 355 | $h{count} = $_[6]; | ||
| 356 | $h{destroyed} = $x->interp('Bool',$_[7]); | ||
| 357 | return %h; | ||
| 358 | }, sub{ | ||
| 359 | my ($x,%h) = @_; | ||
| 360 | my $data = pack('xxxxLLLLLLSCx', $h{counter}, @{$h{wait_value}}, | ||
| 361 | @{$h{counter_value}}, $h{time}, $h{count}, | ||
| 362 | $x->interp('Bool',$h{destroyed})); | ||
| 363 | return ($data,1); | ||
| 364 | }]; | ||
| 365 | =pod | ||
| 366 | |||
| 367 | AlarmNotify => { | ||
| 368 | alarm=>$alarm, | ||
| 369 | counter_value=>[$counter_value_hi,$counter_value_lo], | ||
| 370 | alarm_value=>[$alarm_value_hi,$alarm_value_lo], | ||
| 371 | time=>$time, | ||
| 372 | state=>$state} # 'AlarmState' | ||
| 373 | =cut | ||
| 374 | $x->{ext_const}{Events}[$event_base+1] = 'AlarmNotify'; | ||
| 375 | $x->{ext_events}[$event_base+1] = [sub{ | ||
| 376 | my ($x,$data,%h) = @_; | ||
| 377 | @_ = unpack('xxxxLLLLLLCxxx',$data); | ||
| 378 | $h{alarm} = $_[0]; | ||
| 379 | $h{counter_value} = [$_[1..2]]; | ||
| 380 | $h{alarm_value} = [$_[3..4]]; | ||
| 381 | $h{time} = $_[5]; | ||
| 382 | $h{state} = $x->interp('AlarmState', $_[5]); | ||
| 383 | return %h; | ||
| 384 | }, sub{ | ||
| 385 | my ($x,%h) = @_; | ||
| 386 | my $data = pack('xxxxLLLLLLCxxx', $h{alarm}, | ||
| 387 | @{$h{counter_value}}, @{$h{alarm_value}}, | ||
| 388 | $h{time}, $x->interp('AlarmState', $h{state})); | ||
| 389 | return ($data,1); | ||
| 390 | }]; | ||
| 391 | |||
| 392 | ($self->{major},$self->{minor}) = $x->req('XsyncInitialize',3,1); | ||
| 393 | if ($self->{major} != 3) { | ||
| 394 | carp "Wrong SYNC version ($self->{major} != 3)"; | ||
| 395 | return 0; | ||
| 396 | } | ||
| 397 | return $self; | ||
| 398 | } | ||
| 399 | |||
| 400 | 1; | ||
| 401 | __END__ |