~bennabiy/+junk/python-xlib

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
# -*- coding: latin-1 -*-
#
# Xlib.protocol.display -- core display communication
#
#    Copyright (C) 2000-2002 Peter Liljenberg <petli@ctrl-c.liu.se>
#
#    This program 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 2 of the License, or
#    (at your option) any later version.
#
#    This program 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 this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# Standard modules
import sys
import select
import struct
import errno
import socket

# Xlib modules
from Xlib import error

from Xlib.support import lock, connect

# Xlib.protocol modules
import rq
import event

class Display:
    resource_classes = {}
    extension_major_opcodes = {}
    error_classes = error.xerror_class.copy()
    event_classes = event.event_class.copy()

    def __init__(self, display = None):
        name, host, displayno, screenno = connect.get_display(display)

        self.display_name = name
        self.default_screen = screenno

        self.socket = connect.get_socket(name, host, displayno)

        auth_name, auth_data = connect.get_auth(self.socket,
                                                name, host, displayno)

        # Internal structures for communication, grouped
        # by their function and locks

        # Socket error indicator, set when the socket is closed
        # in one way or another
        self.socket_error_lock = lock.allocate_lock()
        self.socket_error = None

        # Event queue
        self.event_queue_read_lock = lock.allocate_lock()
        self.event_queue_write_lock = lock.allocate_lock()
        self.event_queue = []

        # Unsent request queue and sequence number counter
        self.request_queue_lock = lock.allocate_lock()
        self.request_serial = 1
        self.request_queue = []

        # Send-and-recieve loop, see function send_and_recive
        # for a detailed explanation
        self.send_recv_lock = lock.allocate_lock()
        self.send_active = 0
        self.recv_active = 0

        self.event_waiting = 0
        self.event_wait_lock = lock.allocate_lock()
        self.request_waiting = 0
        self.request_wait_lock = lock.allocate_lock()

        # Data used by the send-and-recieve loop
        self.sent_requests = []
        self.request_length = 0
        self.data_send = ''
        self.data_recv = ''
        self.data_sent_bytes = 0

        # Resource ID structures
        self.resource_id_lock = lock.allocate_lock()
        self.resource_ids = {}
        self.last_resource_id = 0

        # Use an default error handler, one which just prints the error
        self.error_handler = None


        # Right, now we're all set up for the connection setup
        # request with the server.

        # Figure out which endianess the hardware uses
        self.big_endian = struct.unpack('BB', struct.pack('H', 0x0100))[0]

        if self.big_endian:
            order = 0x42
        else:
            order = 0x6c

        # Send connection setup
        r = ConnectionSetupRequest(self,
                                   byte_order = order,
                                   protocol_major = 11,
                                   protocol_minor = 0,
                                   auth_prot_name = auth_name,
                                   auth_prot_data = auth_data)

        # Did connection fail?
        if r.status != 1:
            raise error.DisplayConnectionError(self.display_name, r.reason)

        # Set up remaining info
        self.info = r
        self.default_screen = min(self.default_screen, len(self.info.roots) - 1)


    #
    # Public interface
    #

    def get_display_name(self):
        return self.display_name

    def get_default_screen(self):
        return self.default_screen

    def fileno(self):
        self.check_for_error()
        return self.socket.fileno()

    def next_event(self):
        self.check_for_error()

        # Main lock, so that only one thread at a time performs the
        # event waiting code.  This at least guarantees that the first
        # thread calling next_event() will get the next event, although
        # no order is guaranteed among other threads calling next_event()
        # while the first is blocking.

        self.event_queue_read_lock.acquire()

        # Lock event queue, so we can check if it is empty
        self.event_queue_write_lock.acquire()

        # We have too loop until we get an event, as
        # we might be woken up when there is no event.

        while not self.event_queue:

            # Lock send_recv so no send_and_recieve
            # can start or stop while we're checking
            # whether there are one active.
            self.send_recv_lock.acquire()

            # Release event queue to allow an send_and_recv to
            # insert any now.
            self.event_queue_write_lock.release()

            # Call send_and_recv, which will return when
            # something has occured
            self.send_and_recv(event = 1)

            # Before looping around, lock the event queue against
            # modifications.
            self.event_queue_write_lock.acquire()

        # Whiew, we have an event!  Remove it from
        # the event queue and relaese its write lock.

        event = self.event_queue[0]
        del self.event_queue[0]
        self.event_queue_write_lock.release()

        # Finally, allow any other threads which have called next_event()
        # while we were waiting to proceed.

        self.event_queue_read_lock.release()

        # And return the event!
        return event

    def pending_events(self):
        self.check_for_error()

        # Make a send_and_recv pass, receiving any events
        self.send_recv_lock.acquire()
        self.send_and_recv(recv = 1)

        # Lock the queue, get the event count, and unlock again.
        self.event_queue_write_lock.acquire()
        count = len(self.event_queue)
        self.event_queue_write_lock.release()

        return count

    def flush(self):
        self.check_for_error()
        self.send_recv_lock.acquire()
        self.send_and_recv(flush = 1)

    def close(self):
        self.flush()
        self.close_internal('client')

    def set_error_handler(self, handler):
        self.error_handler = handler


    def allocate_resource_id(self):
        """id = d.allocate_resource_id()

        Allocate a new X resource id number ID.

        Raises ResourceIDError if there are no free resource ids.
        """

        self.resource_id_lock.acquire()
        try:
            i = self.last_resource_id
            while self.resource_ids.has_key(i):
                i = i + 1
                if i > self.info.resource_id_mask:
                    i = 0
                if i == self.last_resource_id:
                    raise error.ResourceIDError('out of resource ids')

            self.resource_ids[i] = None
            self.last_resource_id = i
            return self.info.resource_id_base | i
        finally:
            self.resource_id_lock.release()

    def free_resource_id(self, rid):
        """d.free_resource_id(rid)

        Free resource id RID.  Attempts to free a resource id which
        isn't allocated by us are ignored.
        """

        self.resource_id_lock.acquire()
        try:
            i = rid & self.info.resource_id_mask

            # Attempting to free a resource id outside our range
            if rid - i != self.info.resource_id_base:
                return None

            try:
                del self.resource_ids[i]
            except KeyError:
                pass
        finally:
            self.resource_id_lock.release()



    def get_resource_class(self, class_name, default = None):
        """class = d.get_resource_class(class_name, default = None)

        Return the class to be used for X resource objects of type
        CLASS_NAME, or DEFAULT if no such class is set.
        """

        return self.resource_classes.get(class_name, default)

    def set_extension_major(self, extname, major):
        self.extension_major_opcodes[extname] = major

    def get_extension_major(self, extname):
        return self.extension_major_opcodes[extname]

    def add_extension_event(self, code, evt):
        self.event_classes[code] = evt

    def add_extension_error(self, code, err):
        self.error_classes[code] = err


    #
    # Private functions
    #

    def check_for_error(self):
        self.socket_error_lock.acquire()
        err = self.socket_error
        self.socket_error_lock.release()

        if err:
            raise err

    def send_request(self, request, wait_for_response):
        if self.socket_error:
            raise self.socket_error

        self.request_queue_lock.acquire()

        request._serial = self.request_serial
        self.request_serial = (self.request_serial + 1) % 65536

        self.request_queue.append((request, wait_for_response))
        qlen = len(self.request_queue)

        self.request_queue_lock.release()

#       if qlen > 10:
#           self.flush()

    def close_internal(self, whom):
        # Clear out data structures
        self.request_queue = None
        self.sent_requests = None
        self.event_queue = None
        self.data_send = None
        self.data_recv = None

        # Close the connection
        self.socket.close()

        # Set a connection closed indicator
        self.socket_error_lock.acquire()
        self.socket_error = error.ConnectionClosedError(whom)
        self.socket_error_lock.release()


    def send_and_recv(self, flush = None, event = None, request = None, recv = None):
        """send_and_recv(flush = None, event = None, request = None, recv = None)

        Perform I/O, or wait for some other thread to do it for us.

        send_recv_lock MUST be LOCKED when send_and_recv is called.
        It will be UNLOCKED at return.

        Exactly or one of the parameters flush, event, request and recv must
        be set to control the return condition.

        To attempt to send all requests in the queue, flush should
        be true.  Will return immediately if another thread is
        already doing send_and_recv.

        To wait for an event to be recieved, event should be true.

        To wait for a response to a certain request (either an error
        or a response), request should be set the that request's
        serial number.

        To just read any pending data from the server, recv should be true.

        It is not guaranteed that the return condition has been
        fulfilled when the function returns, so the caller has to loop
        until it is finished.
        """

        # We go to sleep if there is already a thread doing what we
        # want to do:

        #  If flushing, we want to send
        #  If waiting for a response to a request, we want to send
        #    (to ensure that the request was sent - we alway recv
        #     when we get to the main loop, but sending is the important
        #     thing here)
        #  If waiting for an event, we want to recv
        #  If just trying to receive anything we can, we want to recv

        if (((flush or request is not None) and self.send_active)
            or ((event or recv) and self.recv_active)):

            # Signal that we are waiting for something.  These locks
            # together with the *_waiting variables are used as
            # semaphores.  When an event or a request response arrives,
            # it will zero the *_waiting and unlock the lock.  The
            # locks will also be unlocked when an active send_and_recv
            # finishes to signal the other waiting threads that one of
            # them has to take over the send_and_recv function.

            # All this makes these locks and variables a part of the
            # send_and_recv control logic, and hence must be modified
            # only when we have the send_recv_lock locked.
            if event:
                wait_lock = self.event_wait_lock
                if not self.event_waiting:
                    self.event_waiting = 1
                    wait_lock.acquire()

            elif request is not None:
                wait_lock = self.request_wait_lock
                if not self.request_waiting:
                    self.request_waiting = 1
                    wait_lock.acquire()

            # Release send_recv, allowing a send_and_recive
            # to terminate or other threads to queue up
            self.send_recv_lock.release()

            # Return immediately if flushing, even if that
            # might mean that not necessarily all requests
            # have been sent.
            if flush or recv:
                return

            # Wait for something to happen, as the wait locks are
            # unlocked either when what we wait for has arrived (not
            # necessarily the exact object we're waiting for, though),
            # or when an active send_and_recv exits.

            # Release it immediately afterwards as we're only using
            # the lock for synchonization.  Since we're not modifying
            # event_waiting or request_waiting here we don't have
            # to lock send_and_recv_lock.  In fact, we can't do that
            # or we trigger a dead-lock.

            wait_lock.acquire()
            wait_lock.release()

            # Return to caller to let it check whether it has
            # got the data it was waiting for
            return


        # There's no thread doing what we need to do.  Find out exactly
        # what to do

        # There must always be some thread recieving data, but it must not
        # necessarily be us

        if not self.recv_active:
            recieving = 1
            self.recv_active = 1
        else:
            recieving = 0

        flush_bytes = None
        sending = 0

        # Loop, recieving and sending data.
        while 1:

            # We might want to start sending data
            if sending or not self.send_active:

                # Turn all requests on request queue into binary form
                # and append them to self.data_send

                self.request_queue_lock.acquire()
                for req, wait in self.request_queue:
                    self.data_send = self.data_send + req._binary
                    if wait:
                        self.sent_requests.append(req)

                del self.request_queue[:]
                self.request_queue_lock.release()

                # If there now is data to send, mark us as senders

                if self.data_send:
                    self.send_active = 1
                    sending = 1
                else:
                    self.send_active = 0
                    sending = 0

            # We've done all setup, so release the lock and start waiting
            # for the network to fire up
            self.send_recv_lock.release()

            # If we're flushing, figure out how many bytes we
            # have to send so that we're not caught in an interminable
            # loop if other threads continuously append requests.
            if flush and flush_bytes is None:
                flush_bytes = self.data_sent_bytes + len(self.data_send)


            try:
                # We're only checking for the socket to be writable
                # if we're the sending thread.  We always check for it
                # to become readable: either we are the recieving thread
                # and should take care of the data, or the recieving thread
                # might finish recieving after having read the data

                if sending:
                    writeset = [self.socket]
                else:
                    writeset = []

                # Timeout immediately if we're only checking for
                # something to read or if we're flushing, otherwise block

                if recv or flush:
                    timeout = 0
                else:
                    timeout = None

                rs, ws, es = select.select([self.socket], writeset, [], timeout)

            # Ignore errors caused by a signal recieved while blocking.
            # All other errors are re-raised.
            except select.error, err:
                if err[0] != errno.EINTR:
                    raise err

                # We must lock send_and_recv before we can loop to
                # the start of the loop

                self.send_recv_lock.acquire()
                continue


            # Socket is ready for sending data, send as much as possible.
            if ws:
                try:
                    i = self.socket.send(self.data_send)
                except socket.error, err:
                    self.close_internal('server: %s' % err[1])
                    raise self.socket_error

                self.data_send = self.data_send[i:]
                self.data_sent_bytes = self.data_sent_bytes + i


            # There is data to read
            gotreq = 0
            if rs:

                # We're the recieving thread, parse the data
                if recieving:
                    try:
                        bytes_recv = self.socket.recv(2048)
                    except socket.error, err:
                        self.close_internal('server: %s' % err[1])
                        raise self.socket_error

                    if not bytes_recv:
                        # Clear up, set a connection closed indicator and raise it
                        self.close_internal('server')
                        raise self.socket_error

                    self.data_recv = self.data_recv + bytes_recv
                    gotreq = self.parse_response(request)

                # Otherwise return, allowing the calling thread to figure
                # out if it has got the data it needs
                else:
                    # We must be a sending thread if we're here, so reset
                    # that indicator.
                    self.send_recv_lock.acquire()
                    self.send_active = 0
                    self.send_recv_lock.release()

                    # And return to the caller
                    return


            # There are three different end of send-recv-loop conditions.
            # However, we don't leave the loop immediately, instead we
            # try to send and recieve any data that might be left.  We
            # do this by giving a timeout of 0 to select to poll
            # the socket.

            # When flushing: all requests have been sent
            if flush and flush_bytes >= self.data_sent_bytes:
                break

            # When waiting for an event: an event has been read
            if event and self.event_queue:
                break

            # When processing a certain request: got its reply
            if request is not None and gotreq:
                break

            # Always break if we just want to recieve as much as possible
            if recv:
                break

            # Else there's may still data which must be sent, or
            # we haven't got the data we waited for.  Lock and loop

            self.send_recv_lock.acquire()


        # We have accomplished the callers request.
        # Record that there are now no active send_and_recv,
        # and wake up all waiting thread

        self.send_recv_lock.acquire()

        if sending:
            self.send_active = 0
        if recieving:
            self.recv_active = 0

        if self.event_waiting:
            self.event_waiting = 0
            self.event_wait_lock.release()

        if self.request_waiting:
            self.request_waiting = 0
            self.request_wait_lock.release()

        self.send_recv_lock.release()


    def parse_response(self, request):
        """Internal method.

        Parse data recieved from server.  If REQUEST is not None
        true is returned if the request with that serial number
        was recieved, otherwise false is returned.

        If REQUEST is -1, we're parsing the server connection setup
        response.
        """

        if request == -1:
            return self.parse_connection_setup()

        # Parse ordinary server response
        gotreq = 0
        while 1:
            # Are we're waiting for additional data for a request response?
            if self.request_length:
                if len(self.data_recv) < self.request_length:
                    return gotreq
                else:
                    gotreq = self.parse_request_response(request) or gotreq


            # Every response is at least 32 bytes long, so don't bother
            # until we have recieved that much
            if len(self.data_recv) < 32:
                return gotreq

            # Check the first byte to find out what kind of response it is
            rtype = ord(self.data_recv[0])

            # Error resposne
            if rtype == 0:
                gotreq = self.parse_error_response(request) or gotreq

            # Request response
            elif rtype == 1:
                # Set reply length, and loop around to see if
                # we have got the full response
                rlen = int(struct.unpack('=L', self.data_recv[4:8])[0])
                self.request_length = 32 + rlen * 4

            # Else event response
            else:
                self.parse_event_response(rtype)


    def parse_error_response(self, request):
        # Code is second byte
        code = ord(self.data_recv[1])

        # Fetch error class
        estruct = self.error_classes.get(code, error.XError)

        e = estruct(self, self.data_recv[:32])
        self.data_recv = buffer(self.data_recv, 32)

        # print 'recv Error:', e

        req = self.get_waiting_request(e.sequence_number)

        # Error for a request whose response we are waiting for,
        # or which have an error handler.  However, if the error
        # handler indicates that it hasn't taken care of the
        # error, pass it on to the default error handler

        if req and req._set_error(e):

            # If this was a ReplyRequest, unlock any threads waiting
            # for a request to finish

            if isinstance(req, rq.ReplyRequest):
                self.send_recv_lock.acquire()

                if self.request_waiting:
                    self.request_waiting = 0
                    self.request_wait_lock.release()

                self.send_recv_lock.release()

            return request == e.sequence_number

        # Else call the error handler
        else:
            if self.error_handler:
                rq.call_error_handler(self.error_handler, e, None)
            else:
                self.default_error_handler(e)

            return 0


    def default_error_handler(self, err):
        sys.stderr.write('X protocol error:\n%s\n' % err)


    def parse_request_response(self, request):
        req = self.get_waiting_replyrequest()

        # Sequence number is always data[2:4]
        # Do sanity check before trying to parse the data
        sno = struct.unpack('=H', self.data_recv[2:4])[0]
        if sno != req._serial:
            raise RuntimeError("Expected reply for request %s, but got %s.  Can't happen!"
                               % (req._serial, sno))

        req._parse_response(self.data_recv[:self.request_length])
        # print 'recv Request:', req

        self.data_recv = buffer(self.data_recv, self.request_length)
        self.request_length = 0


        # Unlock any response waiting threads

        self.send_recv_lock.acquire()

        if self.request_waiting:
            self.request_waiting = 0
            self.request_wait_lock.release()

        self.send_recv_lock.release()


        return req.sequence_number == request


    def parse_event_response(self, etype):
        # Skip bit 8 at lookup, that is set if this event came from an
        # SendEvent
        estruct = self.event_classes.get(etype & 0x7f, event.AnyEvent)

        e = estruct(display = self, binarydata = self.data_recv[:32])

        self.data_recv = buffer(self.data_recv, 32)

        # Drop all requests having an error handler,
        # but which obviously succeded.

        # Decrement it by one, so that we don't remove the request
        # that generated these events, if there is such a one.
        # Bug reported by Ilpo Nyyssönen
        self.get_waiting_request((e.sequence_number - 1) % 65536)

        # print 'recv Event:', e

        # Insert the event into the queue
        self.event_queue_write_lock.acquire()
        self.event_queue.append(e)
        self.event_queue_write_lock.release()

        # Unlock any event waiting threads
        self.send_recv_lock.acquire()

        if self.event_waiting:
            self.event_waiting = 0
            self.event_wait_lock.release()

        self.send_recv_lock.release()


    def get_waiting_request(self, sno):
        if not self.sent_requests:
            return None

        # Normalize sequence numbers, even if they have wrapped.
        # This ensures that
        #   sno <= last_serial
        # and
        #   self.sent_requests[0]._serial <= last_serial

        if self.sent_requests[0]._serial > self.request_serial:
            last_serial = self.request_serial + 65536
            if sno < self.request_serial:
                sno = sno + 65536

        else:
            last_serial = self.request_serial
            if sno > self.request_serial:
                sno = sno - 65536

        # No matching events at all
        if sno < self.sent_requests[0]._serial:
            return None

        # Find last req <= sno
        req = None
        reqpos = len(self.sent_requests)
        adj = 0
        last = 0

        for i in range(0, len(self.sent_requests)):
            rno = self.sent_requests[i]._serial + adj

            # Did serial numbers just wrap around?
            if rno < last:
                adj = 65536
                rno = rno + adj

            last = rno

            if sno == rno:
                req = self.sent_requests[i]
                reqpos = i + 1
                break
            elif sno < rno:
                req = None
                reqpos = i
                break

        # Delete all request such as req <= sno
        del self.sent_requests[:reqpos]

        return req

    def get_waiting_replyrequest(self):
        for i in range(0, len(self.sent_requests)):
            if hasattr(self.sent_requests[i], '_reply'):
                req = self.sent_requests[i]
                del self.sent_requests[:i + 1]
                return req

        # Reply for an unknown request?  No, that can't happen.
        else:
            raise RuntimeError("Request reply to unknown request.  Can't happen!")

    def parse_connection_setup(self):
        """Internal function used to parse connection setup response.
        """

        # Only the ConnectionSetupRequest has been sent so far
        r = self.sent_requests[0]

        while 1:
            # print 'data_send:', repr(self.data_send)
            # print 'data_recv:', repr(self.data_recv)

            if r._data:
                alen = r._data['additional_length'] * 4

                # The full response haven't arrived yet
                if len(self.data_recv) < alen:
                    return 0

                # Connection failed or further authentication is needed.
                # Set reason to the reason string
                if r._data['status'] != 1:
                    r._data['reason'] = self.data_recv[:r._data['reason_length']]

                # Else connection succeeded, parse the reply
                else:
                    x, d = r._success_reply.parse_binary(self.data_recv[:alen],
                                                         self, rawdict = 1)
                    r._data.update(x)

                del self.sent_requests[0]

                self.data_recv = self.data_recv[alen:]

                return 1

            else:
                # The base reply is 8 bytes long
                if len(self.data_recv) < 8:
                    return 0

                r._data, d = r._reply.parse_binary(self.data_recv[:8],
                                                   self, rawdict = 1)
                self.data_recv = self.data_recv[8:]

                # Loop around to see if we have got the additional data
                # already


PixmapFormat = rq.Struct( rq.Card8('depth'),
                          rq.Card8('bits_per_pixel'),
                          rq.Card8('scanline_pad'),
                          rq.Pad(5)
                          )

VisualType = rq.Struct ( rq.Card32('visual_id'),
                         rq.Card8('visual_class'),
                         rq.Card8('bits_per_rgb_value'),
                         rq.Card16('colormap_entries'),
                         rq.Card32('red_mask'),
                         rq.Card32('green_mask'),
                         rq.Card32('blue_mask'),
                         rq.Pad(4)
                         )

Depth = rq.Struct( rq.Card8('depth'),
                   rq.Pad(1),
                   rq.LengthOf('visuals', 2),
                   rq.Pad(4),
                   rq.List('visuals', VisualType)
                   )

Screen = rq.Struct( rq.Window('root'),
                    rq.Colormap('default_colormap'),
                    rq.Card32('white_pixel'),
                    rq.Card32('black_pixel'),
                    rq.Card32('current_input_mask'),
                    rq.Card16('width_in_pixels'),
                    rq.Card16('height_in_pixels'),
                    rq.Card16('width_in_mms'),
                    rq.Card16('height_in_mms'),
                    rq.Card16('min_installed_maps'),
                    rq.Card16('max_installed_maps'),
                    rq.Card32('root_visual'),
                    rq.Card8('backing_store'),
                    rq.Card8('save_unders'),
                    rq.Card8('root_depth'),
                    rq.LengthOf('allowed_depths', 1),
                    rq.List('allowed_depths', Depth)
                    )


class ConnectionSetupRequest(rq.GetAttrData):
    _request = rq.Struct( rq.Set('byte_order', 1, (0x42, 0x6c)),
                          rq.Pad(1),
                          rq.Card16('protocol_major'),
                          rq.Card16('protocol_minor'),
                          rq.LengthOf('auth_prot_name', 2),
                          rq.LengthOf('auth_prot_data', 2),
                          rq.Pad(2),
                          rq.String8('auth_prot_name'),
                          rq.String8('auth_prot_data') )

    _reply = rq.Struct ( rq.Card8('status'),
                         rq.Card8('reason_length'),
                         rq.Card16('protocol_major'),
                         rq.Card16('protocol_minor'),
                         rq.Card16('additional_length') )

    _success_reply = rq.Struct( rq.Card32('release_number'),
                                rq.Card32('resource_id_base'),
                                rq.Card32('resource_id_mask'),
                                rq.Card32('motion_buffer_size'),
                                rq.LengthOf('vendor', 2),
                                rq.Card16('max_request_length'),
                                rq.LengthOf('roots', 1),
                                rq.LengthOf('pixmap_formats', 1),
                                rq.Card8('image_byte_order'),
                                rq.Card8('bitmap_format_bit_order'),
                                rq.Card8('bitmap_format_scanline_unit'),
                                rq.Card8('bitmap_format_scanline_pad'),
                                rq.Card8('min_keycode'),
                                rq.Card8('max_keycode'),
                                rq.Pad(4),
                                rq.String8('vendor'),
                                rq.List('pixmap_formats', PixmapFormat),
                                rq.List('roots', Screen),
                                )


    def __init__(self, display, *args, **keys):
        self._binary = apply(self._request.to_binary, args, keys)
        self._data = None

        # Don't bother about locking, since no other threads have
        # access to the display yet

        display.request_queue.append((self, 1))

        # However, we must lock send_and_recv, but we don't have
        # to loop.

        display.send_recv_lock.acquire()
        display.send_and_recv(request = -1)