1. xemacs
  2. xlib

Source

xlib / lisp / xlib-xr.el

   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
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
;;; xlib-xr.el --- X receive part.

;; Copyright (C) 2003 by Free Software Foundation, Inc.

;; Author: Eric M. Ludlam <zappo@gnu.ai.mit.edu>
;;	   Zajcev Evgeny <zevlg@yandex.ru>
;; Keywords: xlib, xwem
;; X-CVS: $Id$

;; This file is part of XWEM.

;; XWEM 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, or (at your option)
;; any later version.

;; XWEM 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 XEmacs; see the file COPYING.  If not, write to the Free
;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
;; 02111-1307, USA.

;;; Synched up with: Not in FSF

;;; Commentary:

;; 

;;; Code:


;; GNU Emacs compatibility
(unless (fboundp 'define-error)
  (defun define-error (err-sym doc-string &optional inherits-from)
    "Define a new error, denoted by ERR-SYM."
    (put err-sym 'error-message doc-string)
    (unless inherits-from
      (setq inherits-from 'error))
    (let ((conds (get inherits-from 'error-conditions)))
      (or conds (signal 'error (list "Not an error symbol" err-sym)))
      (put err-sym 'error-conditions (cons err-sym conds)))))

(define-error 'X-Error
  "X Server error.")

(define-error 'X-Events-stop
  "Error used to stop X events processing.")

;;; X Events section.
(defun XVectorizeList (lst)
  "Take list LST and turn it into a vector.
This makes random access of its fields much faster."
  (let ((nv (make-vector (length lst) nil))
	(cnt 0))
    (while lst
      (aset nv cnt (if (and (car lst) (listp (car lst)))
		       (XVectorizeList (car lst))
		     (car lst)))
      (setq cnt (1+ cnt))
      (setq lst (cdr lst)))
    nv))

(defvar X-Event-LASTEvent 128
  "Any event must be less then this one.
NOTE: * Core event are less than 35, but extensions may generate greater.
      * Eight bit is syntetic bit.")

(defvar X-EventsList (make-vector X-Event-LASTEvent nil)
  "List of event descriptions.")

;;;###autoload
(defstruct (X-Event (:predicate X-Event-isevent-p))
  dpy					; display
  type					; type of event
  send-event				; non-nil if event came from SendEvent request
  evdata				; binary event represetation
  evinfo				; parsed variant of evdata

  list					;for use in X-Generate-message
  properties				; User defined plist
  )

;;;###autoload
(defsubst X-Event-put-property (xev prop val)
  "Put property PROP with value VAL in XEV's properties list."
  (setf (X-Event-properties xev)
	(plist-put (X-Event-properties xev) prop val)))

;;;###autoload
(defsubst X-Event-get-property (xev prop)
  "Get property PROP from XEV's properties list."
  (plist-get (X-Event-properties xev) prop))

;;;###autoload
(defsubst X-Event-rem-property (xev prop)
  "Remove property PROP from XEV's properties list."
  (setf (X-Event-properties xev) (plist-remprop (X-Event-properties xev) prop)))

;;;###autoload
(defun X-Event-p (ev &optional sig)
  "Return non-nil if EV is X-Event."
  (let ((isev (X-Event-isevent-p ev)))
    (if (and (not isev) sig)
	(signal 'wrong-type-argument (list sig 'X-Event-p ev))
      isev)))

;;;###autoload
(defsubst X-Event-detail (xev)
  "Return detail info stored in XEV."
  (nth 0 (X-Event-evinfo xev)))

;;;###autoload
(defsubst X-Event-seq (xev)
  "Return sequence number of XEvent XEV."
  (nth 1 (X-Event-evinfo xev)))

;;;###autoload
(defun X-Event-win (xev)
  "Return window for which EV generated."
  (let* ((evt (X-Event-type xev))
	 (evd (aref X-EventsList evt))
	 (win (nth (aref evd 2) (X-Event-evinfo xev))))
    win))

;;;###autoload
(defun X-Event-name (xev)
  "Return symbolic XEV name."
  (aref (aref X-EventsList (X-Event-type xev)) 0))

(defun X-Event-make (&rest args)
  "Like `make-X-Event', but also fills list field automatically."
  (let* ((xev (apply 'make-X-Event args))
	 (evspec (aref (X-Event-type xev) X-EventsList)))
    ;; TODO: write me ..
    ))


(defmacro X-Event-declare (type descr)
  "Only declare event of TYPE with DESCR in `X-EventsList'."
  `(aset X-EventsList ,type ,descr))

(defmacro X-Event-define (type name dnames descr)
  "Define new event of TYPE, NAME and description of event DESCR."
  (let ((offs 0)
	fsym
	forms)

    (cl-push `(aset X-EventsList ,type ,descr) forms)
    (while dnames
      (when (car dnames)
	(setq fsym (intern (concat "X-Event-" name "-" (symbol-name (car dnames)))))
	(cl-push `(defsubst* ,fsym (ev)
		       (nth ,offs (X-Event-evinfo ev)))
		 forms))
      (setq offs (1+ offs))
      (setq dnames (cdr dnames)))
    
    `(progn ,@forms)))

(defun X-Event->symbolkey (xev)
  "Convert XEV type to symbolic name, return keyword."
  (let ((evt (X-Event-type xev)))
    (cond ((= evt X-KeyPress) :X-KeyPress)
	  ((= evt X-KeyRelease) :X-KeyRelease)
	  ((= evt X-ButtonPress) :X-ButtonPress)
	  ((= evt X-ButtonRelease) :X-ButtonRelease)
	  ((= evt X-MotionNotify) :X-MotionNotify)
	  ((= evt X-EnterNotify) :X-EnterNotify)
	  ((= evt X-LeaveNotify) :X-LeaveNotify)
	  ((= evt X-FocusIn) :X-FocusIn)
	  ((= evt X-FocusOut) :X-FocusOut)
	  ((= evt X-KeymapNotify) :X-KeymapNotify)
	  ((= evt X-Expose) :X-Expose)
	  ((= evt X-GraphicsExpose) :X-GraphicsExpose)
	  ((= evt X-NoExpose) :X-NoExpose)
	  ((= evt X-VisibilityNotify) :X-VisibilityNotify)
	  ((= evt X-CreateNotify) :X-CreateNotify)
	  ((= evt X-DestroyNotify) :X-DestroyNotify)
	  ((= evt X-UnmapNotify) :X-UnmapNotify)
	  ((= evt X-MapNotify) :X-MapNotify)
	  ((= evt X-MapRequest) :X-MapRequest)
	  ((= evt X-ReparentNotify) :X-ReparentNotify)
	  ((= evt X-ConfigureRequest) :X-ConfigureRequest)
	  ((= evt X-ConfigureNotify) :X-ConfigureNotify)
	  ((= evt X-GravityNotify) :X-GravityNotify)
	  ((= evt X-ResizeRequest) :X-ResizeRequest)
	  ((= evt X-CirculateNotify) :X-CirculateNotify)
	  ((= evt X-CirculateRequest) :X-CirculateRequest)
	  ((= evt X-PropertyNotify) :X-PropertyNotify)
	  ((= evt X-SelectionClear) :X-SelectionClear)
	  ((= evt X-SelectionRequest) :X-SelectionRequest)
	  ((= evt X-SelectionNotify) :X-SelectionNotify)
	  ((= evt X-ColormapNotify) :X-ColormapNotify)
	  ((= evt X-ClientMessage) :X-ClientMessage)
	  ((= evt X-MappingNotify) :X-MappingNotify)

	  (t :X-Unknown))))

;;;###autoload
(defmacro X-Event-CASE (xev &rest body)
  "Run event case. BODY in form (EVTYPE FORMS) (EVTYPE FORMS) ..
EVTYPE is one of :X-KeyPress, :X-KeyRelease etc."
  `(case (X-Event->symbolkey ,xev)
     ,@body))

(put 'X-Event-CASE 'lisp-indent-function 1)

(defstruct X-EventHandler
  priority
  evtypes-list				; list of event types
  handler				; function to call
  (active t)				; Non-nil mean event handler activated

  plist)				; user defined plist

;;;###autoload
(defun X-EventHandler-add (evhlist handler &optional priority evtypes-list)
  "To event handlers list EVHLIST add event HANDLER.

HANDLER is function which should accept three arguments - xdpy(X-Dpy),
xwin(X-Win) and xev(X-Event).  Only events with type that in
EVTYPES-LIST are passed to HANDLER. By default all events passed.
PRIORITY is place in events handler list, i.e. when HANDLER will be
called. Higher priorities runs first.

Return new list, use it like `(setq lst (X-EventHandler-add lst 'handler))'."
  (unless priority
    (setq priority 0))

  (let ((xeh (make-X-EventHandler :priority priority
				  :evtypes-list evtypes-list
				  :handler handler)))

    ;; Insert new event handler and sort event handlers by priority.
    (sort (cons xeh evhlist)
	  (lambda (xeh1 xeh2)
	    (> (X-EventHandler-priority xeh1)
	       (X-EventHandler-priority xeh2))))))

;;;###autoload
(defun X-EventHandler-isset (evhlist handler &optional prioritiy evtypes-list)
  "Examine EVHLIST and return X-EventHandler with HANDLER, PRIORITY and EVTYPES-LIST.
If you does not specify PRIORITY and EVTYPES-LIST, only matching with HANDLER occurs.
If event handler not found - nil will be returned."
  (let ((evhs evhlist))
    ;; Find appopriate handler
    (while (and evhs
		(not (and (eq (X-EventHandler-handler (car evhs)) handler)
			  (if prioritiy (equal prioritiy (X-EventHandler-priority (car evhs))) t)
			  (if evtypes-list (equal evtypes-list (X-EventHandler-evtypes-list (car evhs))) t))))
      (setq evhs (cdr evhs)))

    (car evhs)))

;;;###autoload
(defun X-EventHandler-rem (evhlist handler &optional prioritiy evtypes-list)
  "From EVHLIST remove event HANDLER with PRIORITY and EVTYPES-LIST.
If you does not specify PRIORITY and EVTYPES-LIST, only matching with HANDLER occurs.
Return new list, use it like `(setq lst (X-EventHandler-rem lst 'handler))'."
  (let ((xeh (X-EventHandler-isset evhlist handler prioritiy evtypes-list)))
    (when xeh
      (setq evhlist (delete xeh evhlist)))
    evhlist))

;;;###autoload
(defun X-EventHandler-enable (evhlist handler &optional prioritiy evtypes-list)
  "In event handlers list EVHLIST mark HANDLER with PRIORITY and EVTYPES-LIST as active."
  (let ((xeh (X-EventHandler-isset evhlist handler prioritiy evtypes-list)))
    (when xeh
      (setf (X-EventHandler-active xeh) t))))

;;;###autoload
(defun X-EventHandler-disable (evhlist handler &optional prioritiy evtypes-list)
  "In event handlers list EVHLIST mark HANDLER with PRIORITY and EVTYPES-LIST as inactive."
  (let ((xeh (X-EventHandler-isset evhlist handler prioritiy evtypes-list)))
    (when xeh
      (setf (X-EventHandler-active xeh) nil))))

;;;###autoload
(defun X-EventHandler-runall (evhlist xev)
  "Run all event handlers in EVHLIST on XEV.
Signal `X-Events-stop' to stop events processing."
  (let ((evhs evhlist))			; EVHS should be already
					; sorted by priority
    (condition-case nil
	(while evhs
	  ;; Check is there appopriate event handler to handle XEV event.
	  (when (and (X-EventHandler-active (car evhs))
		     (or (null (X-EventHandler-evtypes-list (car evhs)))
			 (member (X-Event-type xev) (X-EventHandler-evtypes-list (car evhs)))))
	
	    (funcall (X-EventHandler-handler (car evhs)) (X-Event-dpy xev) (X-Event-win xev) xev))
	  (setq evhs (cdr evhs)))
      (X-Events-stop nil))))

;;; X Events description.

;;;###autoload(autoload 'X-Event-xkey-keycode "xlib-xr")
;;;###autoload(autoload 'X-Event-xkey-time "xlib-xr")
;;;###autoload(autoload 'X-Event-xkey-root "xlib-xr")
;;;###autoload(autoload 'X-Event-xkey-event "xlib-xr")
;;;###autoload(autoload 'X-Event-xkey-child "xlib-xr")
;;;###autoload(autoload 'X-Event-xkey-root-x "xlib-xr")
;;;###autoload(autoload 'X-Event-xkey-root-y "xlib-xr")
;;;###autoload(autoload 'X-Event-xkey-event-x "xlib-xr")
;;;###autoload(autoload 'X-Event-xkey-event-y "xlib-xr")
;;;###autoload(autoload 'X-Event-xkey-state "xlib-xr")
;;;###autoload(autoload 'X-Event-xkey-same-screen "xlib-xr")

;;;###autoload(autoload 'X-Event-xbutton-button "xlib-xr")
;;;###autoload(autoload 'X-Event-xbutton-time "xlib-xr")
;;;###autoload(autoload 'X-Event-xbutton-root "xlib-xr")
;;;###autoload(autoload 'X-Event-xbutton-event "xlib-xr")
;;;###autoload(autoload 'X-Event-xbutton-child "xlib-xr")
;;;###autoload(autoload 'X-Event-xbutton-root-x "xlib-xr")
;;;###autoload(autoload 'X-Event-xbutton-root-y "xlib-xr")
;;;###autoload(autoload 'X-Event-xbutton-event-x "xlib-xr")
;;;###autoload(autoload 'X-Event-xbutton-event-y "xlib-xr")
;;;###autoload(autoload 'X-Event-xbutton-state "xlib-xr")
;;;###autoload(autoload 'X-Event-xbutton-same-screen "xlib-xr")

;;;###autoload(autoload 'X-Event-xmotion-time "xlib-xr")
;;;###autoload(autoload 'X-Event-xmotion-root "xlib-xr")
;;;###autoload(autoload 'X-Event-xmotion-event "xlib-xr")
;;;###autoload(autoload 'X-Event-xmotion-child "xlib-xr")
;;;###autoload(autoload 'X-Event-xmotion-root-x "xlib-xr")
;;;###autoload(autoload 'X-Event-xmotion-root-y "xlib-xr")
;;;###autoload(autoload 'X-Event-xmotion-event-x "xlib-xr")
;;;###autoload(autoload 'X-Event-xmotion-event-y "xlib-xr")
;;;###autoload(autoload 'X-Event-xmotion-state "xlib-xr")
;;;###autoload(autoload 'X-Event-xmotion-same-screen "xlib-xr")

;;;###autoload(autoload 'X-Event-xcrossing-time "xlib-xr")
;;;###autoload(autoload 'X-Event-xcrossing-root "xlib-xr")
;;;###autoload(autoload 'X-Event-xcrossing-event "xlib-xr")
;;;###autoload(autoload 'X-Event-xcrossing-child "xlib-xr")
;;;###autoload(autoload 'X-Event-xcrossing-root-x "xlib-xr")
;;;###autoload(autoload 'X-Event-xcrossing-root-y "xlib-xr")
;;;###autoload(autoload 'X-Event-xcrossing-event-x "xlib-xr")
;;;###autoload(autoload 'X-Event-xcrossing-event-y "xlib-xr")
;;;###autoload(autoload 'X-Event-xcrossing-state "xlib-xr")
;;;###autoload(autoload 'X-Event-xcrossing-mode "xlib-xr")
;;;###autoload(autoload 'X-Event-xcrossing-same-screen-focus "xlib-xr")

;;;###autoload(autoload 'X-Event-xfocus-event "xlib-xr")
;;;###autoload(autoload 'X-Event-xfocus-mode "xlib-xr")

;;;###autoload(autoload 'X-Event-xexpose-window "xlib-xr")
;;;###autoload(autoload 'X-Event-xexpose-x "xlib-xr")
;;;###autoload(autoload 'X-Event-xexpose-y "xlib-xr")
;;;###autoload(autoload 'X-Event-xexpose-width "xlib-xr")
;;;###autoload(autoload 'X-Event-xexpose-height "xlib-xr")
;;;###autoload(autoload 'X-Event-xexpose-count "xlib-xr")

;;;###autoload(autoload 'X-Event-xgraphicsexpose-drawable "xlib-xr")
;;;###autoload(autoload 'X-Event-xgraphicsexpose-x "xlib-xr")
;;;###autoload(autoload 'X-Event-xgraphicsexpose-y "xlib-xr")
;;;###autoload(autoload 'X-Event-xgraphicsexpose-width "xlib-xr")
;;;###autoload(autoload 'X-Event-xgraphicsexpose-height "xlib-xr")
;;;###autoload(autoload 'X-Event-xgraphicsexpose-minor-event "xlib-xr")
;;;###autoload(autoload 'X-Event-xgraphicsexpose-count "xlib-xr")
;;;###autoload(autoload 'X-Event-xgraphicsexpose-major-event "xlib-xr")

;;;###autoload(autoload 'X-Event-xnoexpose-drawable "xlib-xr")
;;;###autoload(autoload 'X-Event-xnoexpose-minor-event "xlib-xr")
;;;###autoload(autoload 'X-Event-xnoexpose-major-event "xlib-xr")

;;;###autoload(autoload 'X-Event-xvisibility-window "xlib-xr")
;;;###autoload(autoload 'X-Event-xvisibility-state "xlib-xr")

;;;###autoload(autoload 'X-Event-xcreatewindow-parent "xlib-xr")
;;;###autoload(autoload 'X-Event-xcreatewindow-window "xlib-xr")
;;;###autoload(autoload 'X-Event-xcreatewindow-x "xlib-xr")
;;;###autoload(autoload 'X-Event-xcreatewindow-y "xlib-xr")
;;;###autoload(autoload 'X-Event-xcreatewindow-width "xlib-xr")
;;;###autoload(autoload 'X-Event-xcreatewindow-height "xlib-xr")
;;;###autoload(autoload 'X-Event-xcreatewindow-border-width "xlib-xr")
;;;###autoload(autoload 'X-Event-xcreatewindow-override "xlib-xr")

;;;###autoload(autoload 'X-Event-xdestroywindow-event "xlib-xr")
;;;###autoload(autoload 'X-Event-xdestroywindow-window "xlib-xr")

;;;###autoload(autoload 'X-Event-xunmap-window "xlib-xr")
;;;###autoload(autoload 'X-Event-xunmap-fromconfigure "xlib-xr")

;;;###autoload(autoload 'X-Event-xmap-event "xlib-xr")
;;;###autoload(autoload 'X-Event-xmap-window "xlib-xr")
;;;###autoload(autoload 'X-Event-xmap-override "xlib-xr")

;;;###autoload(autoload 'X-Event-xmaprequest-parent "xlib-xr")
;;;###autoload(autoload 'X-Event-xmaprequest-window "xlib-xr")

;;;###autoload(autoload 'X-Event-xreparent-event "xlib-xr")
;;;###autoload(autoload 'X-Event-xreparent-window "xlib-xr")
;;;###autoload(autoload 'X-Event-xreparent-parent "xlib-xr")
;;;###autoload(autoload 'X-Event-xreparent-x "xlib-xr")
;;;###autoload(autoload 'X-Event-xreparent-y "xlib-xr")
;;;###autoload(autoload 'X-Event-xreparent-override "xlib-xr")

;;;###autoload(autoload 'X-Event-xconfigure-event "xlib-xr")
;;;###autoload(autoload 'X-Event-xconfigure-window "xlib-xr")
;;;###autoload(autoload 'X-Event-xconfigure-above-sibling "xlib-xr")
;;;###autoload(autoload 'X-Event-xconfigure-x "xlib-xr")
;;;###autoload(autoload 'X-Event-xconfigure-y "xlib-xr")
;;;###autoload(autoload 'X-Event-xconfigure-width "xlib-xr")
;;;###autoload(autoload 'X-Event-xconfigure-height "xlib-xr")
;;;###autoload(autoload 'X-Event-xconfigure-border-width "xlib-xr")
;;;###autoload(autoload 'X-Event-xconfigure-override-redirect "xlib-xr")

;;;###autoload(autoload 'X-Event-xconfigurerequest-parent "xlib-xr")
;;;###autoload(autoload 'X-Event-xconfigurerequest-window "xlib-xr")
;;;###autoload(autoload 'X-Event-xconfigurerequest-sibling "xlib-xr")
;;;###autoload(autoload 'X-Event-xconfigurerequest-x "xlib-xr")
;;;###autoload(autoload 'X-Event-xconfigurerequest-y "xlib-xr")
;;;###autoload(autoload 'X-Event-xconfigurerequest-width "xlib-xr")
;;;###autoload(autoload 'X-Event-xconfigurerequest-height "xlib-xr")
;;;###autoload(autoload 'X-Event-xconfigurerequest-border-width "xlib-xr")
;;;###autoload(autoload 'X-Event-xconfigurerequest-value-mask "xlib-xr")

;;;###autoload(autoload 'X-Event-xgravity-event "xlib-xr")
;;;###autoload(autoload 'X-Event-xgravity-window "xlib-xr")
;;;###autoload(autoload 'X-Event-xgravity-x "xlib-xr")
;;;###autoload(autoload 'X-Event-xgravity-y "xlib-xr")

;;;###autoload(autoload 'X-Event-xresizerequest-window "xlib-xr")
;;;###autoload(autoload 'X-Event-xresizerequest-width "xlib-xr")
;;;###autoload(autoload 'X-Event-xresizerequest-height "xlib-xr")

;;;###autoload(autoload 'X-Event-xcirculate-event "xlib-xr")
;;;###autoload(autoload 'X-Event-xcirculate-window "xlib-xr")
;;;###autoload(autoload 'X-Event-xcirculate-parent "xlib-xr")
;;;###autoload(autoload 'X-Event-xcirculate-place "xlib-xr")

;;;###autoload(autoload 'X-Event-xproperty-window "xlib-xr")
;;;###autoload(autoload 'X-Event-xproperty-atom "xlib-xr")
;;;###autoload(autoload 'X-Event-xproperty-time "xlib-xr")
;;;###autoload(autoload 'X-Event-xproperty-state "xlib-xr")

;;;###autoload(autoload 'X-Event-xselectionclear-time "xlib-xr")
;;;###autoload(autoload 'X-Event-xselectionclear-window "xlib-xr")
;;;###autoload(autoload 'X-Event-xselectionclear-atom "xlib-xr")

;;;###autoload(autoload 'X-Event-xselectionrequest-time "xlib-xr")
;;;###autoload(autoload 'X-Event-xselectionrequest-owner "xlib-xr")
;;;###autoload(autoload 'X-Event-xselectionrequest-requestor "xlib-xr")
;;;###autoload(autoload 'X-Event-xselectionrequest-selection "xlib-xr")
;;;###autoload(autoload 'X-Event-xselectionrequest-target "xlib-xr")
;;;###autoload(autoload 'X-Event-xselectionrequest-property "xlib-xr")

;;;###autoload(autoload 'X-Event-xselection-time "xlib-xr")
;;;###autoload(autoload 'X-Event-xselection-requestor "xlib-xr")
;;;###autoload(autoload 'X-Event-xselection-selection "xlib-xr")
;;;###autoload(autoload 'X-Event-xselection-target "xlib-xr")
;;;###autoload(autoload 'X-Event-xselection-property "xlib-xr")

;;;###autoload(autoload 'X-Event-xcolormap-window "xlib-xr")
;;;###autoload(autoload 'X-Event-xcolormap-colormap "xlib-xr")
;;;###autoload(autoload 'X-Event-xcolormap-new "xlib-xr")
;;;###autoload(autoload 'X-Event-xcolormap-state "xlib-xr")

;;;###autoload(autoload 'X-Event-xclient-window "xlib-xr")
;;;###autoload(autoload 'X-Event-xclient-atom "xlib-xr")
;;;###autoload(autoload 'X-Event-xclient-msg "xlib-xr")

;;;###autoload(autoload 'X-Event-xmapping-request "xlib-xr")
;;;###autoload(autoload 'X-Event-xmapping-first-keycode "xlib-xr")
;;;###autoload(autoload 'X-Event-xmapping-count "xlib-xr")

;;;###autoload(autoload 'X-Event-xerror-code "xlib-xr")
;;;###autoload(autoload 'X-Event-xerror-resourceid "xlib-xr")
;;;###autoload(autoload 'X-Event-xerror-min-op "xlib-xr")
;;;###autoload(autoload 'X-Event-xerror-maj-op "xlib-xr")

;; TODO:
;;   - Should be X-Dpy depended to support extensions derived events
(X-Event-define X-KeyPress "xkey" (keycode nil time root event child root-x root-y event-x event-y state same-screen)
		[ "KeyPress"
		  ( [1 integerp]	; keycode
		    [2 integerp]	; sequence
		    [4 integerp]	; time
		    [4 :X-Win]		; root
		    [4 :X-Win]		; event
		    [4 :X-Win]		; child
		    [2 integerp]	; root_x
		    [2 integerp]	; root_y
		    [2 integerp]	; event_x
		    [2 integerp]	; event_y
		    [2 integerp]	; state
		    [1 booleanp]	; same_screen
		    [1 nil] )
		  4 ])
(X-Event-declare X-KeyRelease
		 [ "KeyRelease"
		   ( [1 integerp]	; keycode
		     [2 integerp]	; sequence
		     [4 integerp]	; time
		     [4 :X-Win]		; root
		     [4 :X-Win]		; event
		     [4 :X-Win]		; child
		     [2 integerp]	; root_x
		     [2 integerp]	; root_y
		     [2 integerp]	; event_x
		     [2 integerp]	; event_y
		     [2 integerp]	; state
		     [1 booleanp]	; same_screen
		     [1 nil] )
		   4 ])
(X-Event-define X-ButtonPress "xbutton" (button nil time root event child root-x root-y event-x event-y state same-screen)
		[ "ButtonPress"
		  ( [1 integerp]	; button
		    [2 integerp]	; sequence
		    [4 integerp]	; time
		    [4 :X-Win]		; root
		    [4 :X-Win]		; event
		    [4 :X-Win]		; child
		    [2 integerp]	; root_x
		    [2 integerp]	; root_y
		    [2 integerp]	; event_x
		    [2 integerp]	; event_y
		    [2 integerp]	; state
		    [1 booleanp]	; same_screen
		    [1 nil] )
		  4 ])
(X-Event-declare X-ButtonRelease
		 [ "ButtonRelease"
		   ( [1 integerp]	; button
		     [2 integerp]	; sequence
		     [4 integerp]	; time
		     [4 :X-Win]		; root
		     [4 :X-Win]		; event
		     [4 :X-Win]		; child
		     [2 integerp]	; root_x
		     [2 integerp]	; root_y
		     [2 integerp]	; event_x
		     [2 integerp]	; event_y
		     [2 integerp]	; state
		     [1 booleanp]	; same_screen
		     [1 nil] )
		   4 ])
(X-Event-define X-MotionNotify "xmotion" (nil nil time root event child root-x root-y event-x event-y state same-screen)
		[ "MotionNotify"
		  ( [1 integerp]	; detail
		    [2 integerp]	; sequence
		    [4 integerp]	; time
		    [4 :X-Win]          ; root
		    [4 :X-Win]		; event
		    [4 :X-Win]          ; child
		    [2 integerp]	; root_x
		    [2 integerp]	; root_y
		    [2 integerp]	; event_x
		    [2 integerp]	; event_y
		    [2 integerp]	; state
		    [1 booleanp]	; same_screen
		    [1 nil] )
		  4 ])
(X-Event-define X-EnterNotify "xcrossing" (nil nil time root event child root-x root-y event-x event-y state mode same-screen-focus)
		[ "EnterNotify"
		  ( [1 integerp]	; detail
		    [2 integerp]	; sequence
		    [4 integerp]	; time
		    [4 :X-Win]          ; root
		    [4 :X-Win]		; event
		    [4 :X-Win]          ; child
		    [2 integerp]	; root_x
		    [2 integerp]	; root_y
		    [2 integerp]	; event_x
		    [2 integerp]	; event_y
		    [2 integerp]	; state
		    [1 integerp]	; mode
		    [1 integerp])	; same-screen, focus
		  4 ])
(X-Event-declare X-LeaveNotify
		 [ "LeaveNotify"
		   ( [1 integerp]	; detail
		     [2 integerp]	; sequence
		     [4 integerp]	; time
		     [4 :X-Win]         ; root
		     [4 :X-Win]		; event
		     [4 :X-Win]         ; child
		     [2 integerp]	; root_x
		     [2 integerp]	; root_y
		     [2 integerp]	; event_x
		     [2 integerp]	; event_y
		     [2 integerp]	; state
		     [1 integerp]	; mode
		     [1 integerp] )	; same-screen, focus
		   4 ])
(X-Event-define X-FocusIn "xfocus" (nil nil event mode)
		[ "FocusIn"
		  ( [1 integerp]	; detail
		    [2 integerp]	; sequence
		    [4 :X-Win]		; event
		    [1 integerp]	; mode
		    [23 nil] )
		  2 ])
(X-Event-declare X-FocusOut
		 [ "FocusOut"
		   ( [1 integerp]	; detail
		     [2 integerp]	; sequence
		     [4 :X-Win]		; event
		     [1 integerp]	; mode
		     [23 nil] )
		   2 ])

;; TODO: X-KeymapNotify

(X-Event-define X-Expose "xexpose" (nil nil window x y width height count)
		[ "Expose"
		  ( [1 integerp]	; detail
		    [2 integerp]	; sequence
		    [4 :X-Win]		; window
		    [2 integerp]	; x
		    [2 integerp]	; y
		    [2 integerp]	; width
		    [2 integerp]	; height
		    [2 integerp]	; count
		    [14 nil] )
		  2 ])
(X-Event-define X-GraphicsExpose "xgraphicsexpose" (nil nil drawable x y width height minor-event count major-event)
		[ "GraphicsExpose"
		  ([1 integerp]
		   [2 integerp]
		   [4 integerp]		; drawable
		   [2 integerp]		; x
		   [2 integerp]		; y
		   [2 integerp]		; width
		   [2 integerp]		; height
		   [2 integerp]		; minorEvent
		   [2 integerp]		; count
		   [1 integerp]		; majorEvent
		   [11 nil])
		  2 ])
(X-Event-define X-NoExpose "xnoexpose" (nil nil drawable minor-event major-event)
		[ "NoExpose"
		  ([1 integerp]
		   [2 integerp]
		   [4 integerp]		; drawable
		   [2 integerp]		; minorEvent
		   [1 integerp]		; majorEvent
		   [21 nil])
		  2 ])
(X-Event-define X-VisibilityNotify "xvisibility" (nil nil window state)
		[ "VisibilityNotify"
		  ([1 integerp]
		   [2 integerp]
		   [4 :X-Win]		; window
		   [1 integerp]		; state
		   [23 nil])
		  2 ])
(X-Event-define X-CreateNotify "xcreatewindow" (nil nil parent window x y width height border-width override)
		[ "CreateNotify"     
		  ([1 integerp]		; detail
		   [2 integerp]		; sequence
		   [4 :X-Win]		; parent window
		   [4 :X-Win]		; window
		   [2 integerp]		; x
		   [2 integerp]		; y
		   [2 integerp]		; width
		   [2 integerp]		; height
		   [2 integerp]		; border width
		   [1 booleanp]		; override-redirect
		   [9 nil])
		  2 ])
(X-Event-define X-DestroyNotify "xdestroywindow" (nil nil event window)
		[ "DestroyNotify"    
		  ( [1 integerp]	; detail
		    [2 integerp]	; sequence
		    [4 :X-Win]		; event window
		    [4 :X-Win]		; window
		    [20 nil])
		  2 ])
(X-Event-define X-UnmapNotify "xunmap" (nil nil window fromconfigure)
		[ "UnmapNotify"      
		  ( [1 integerp]	; detail
		    [2 integerp]	; sequence
		    [4 :X-Win]		; event
		    [4 :X-Win]		; window
		    [1 booleanp]	; fromconfigure
		    [19 nil])
		  2 ])
(X-Event-define X-MapNotify "xmap" (nil nil event window override)
		[ "MapNotify"
		  ( [1 integerp]	; detail
		    [2 integerp]	; sequence
		    [4 :X-Win]		; event window
		    [4 :X-Win]		; window
		    [1 booleanp]	; override-redirect
		    [19 nil])
		  2 ])
(X-Event-define X-MapRequest "xmaprequest" (nil nil parent window)
		[ "MapRequest"
		  ( [1 integerp]	; detail
		    [2 integerp]	; sequence
		    [4 :X-Win]		; parent window
		    [4 :X-Win]		; window
		    [20 nil])
		  2 ])
(X-Event-define X-ReparentNotify "xreparent" (nil nil event window parent x y override)
		[ "ReparentNotify"
		  ( [1 integerp]	; detail
		    [2 integerp]	; sequence
		    [4 :X-Win]		; event
		    [4 :X-Win]		; window
		    [4 :X-Win]		; parent
		    [2 integerp]	; x
		    [2 integerp]	; y
		    [1 integerp]	; override
		    [11 nil])
		  2 ])
(X-Event-define X-ConfigureNotify "xconfigure" (nil nil event window above-sibling x y width height border-width override-redirect)
		[ "ConfigureNotify"
		  ( [1 integerp]	; detail
		    [2 integerp]	; sequence
		    [4 :X-Win]		; event
		    [4 :X-Win]		; window
		    [4 :X-Win]		; above-sibling
		    [2 integerp]	; x
		    [2 integerp]	; y
		    [2 integerp]	; width
		    [2 integerp]	; height
		    [2 integerp]	; border-width
		    [1 booleanp]	; override-redirect
		    [5 nil] )
		  2 ])
(X-Event-define X-ConfigureRequest "xconfigurerequest" (nil nil parent window sibling x y width height border-width value-mask)
		[ "ConfigureRequest"
		  ( [1 integerp]	; detail
		    [2 integerp]	; sequence
		    [4 :X-Win]		; parent window
		    [4 :X-Win]		; window
		    [4 :X-Win]		; sibling
		    [2 integerp]	; x
		    [2 integerp]	; y
		    [2 integerp]	; width
		    [2 integerp]	; height
		    [2 integerp]	; border width
		    [2 integerp]	; value mask
		    [4 nil])
		  2 ])
(X-Event-define X-GravityNotify "xgravity" (nil nil event window x y)
		[ "GravityNotify" 
		  ([1 integerp]
		   [2 integerp]
		   [4 :X-Win]		; event window
		   [4 :X-Win]		; window
		   [2 integerp]		; x
		   [2 integerp]		; y
		   [16 nil])
		  2 ])
(X-Event-define X-ResizeRequest "xresizerequest" (nil nil window width height)
		[ "ResizeRequest"
		  ( [1 integerp]	; detail
		    [2 integerp]	; sequence
		    [4 :X-Win]		; window
		    [2 integerp]	; width
		    [2 integerp]	; height
		    [20 nil] )
		  2 ])
(X-Event-define X-CirculateNotify "xcirculate" (nil nil event window parent place)
		[ "CirculateNotify" 
		  ([1 integerp]
		   [2 integerp]
		   [4 :X-Win]		; event window
		   [4 :X-Win]		; window
		   [4 :X-Win]		; parent
		   [1 integerp]		; place
		   [15 nil])
		  2 ])
;; The event field in the xcirculate record is really the parent when this
;; is used as a CirculateRequest instead of a CircluateNotify
(X-Event-declare X-CirculateRequest
		 [ "CirculateRequest"
		  ([1 integerp]
		   [2 integerp]
		   [4 :X-Win]		; event window
		   [4 :X-Win]		; window
		   [4 :X-Win]		; parent
		   [1 integerp]		; place
		   [15 nil])
		  2 ])

(X-Event-define X-PropertyNotify "xproperty" (nil nil window atom time state)
		[ "PropertyNotify"
		  ( [1 integerp]
		    [2 integerp]
		    [4 :X-Win]		; window
		    [4 :X-Atom]		; atom
		    [4 integerp]	; time
		    [1 integerp]	; state
		    [15 nil]
		    ) 2 ])
(X-Event-define X-SelectionClear "xselectionclear" (nil nil time window atom)
		[ "SelectionClear"
		  ([1 integerp]
		   [2 integerp]
		   [4 integerp]		; time
		   [4 :X-Win]		; window
		   [4 :X-Atom]		; atom
		   [16 nil])
		  3 ])
(X-Event-define X-SelectionRequest "xselectionrequest" (nil nil time owner requestor selection target property)
		[ "SelectionRequest"
		  ([1 integerp]
		   [2 integerp]
		   [4 integerp]		; time
		   [4 :X-Win]		; owner
		   [4 :X-Win]		; requestor
		   [4 :X-Atom]		; selection atom
		   [4 :X-Atom]		; target atom
		   [4 :X-Atom]		; property atom
		   [4 nil])
		  4 ])
(X-Event-define X-SelectionNotify "xselection" (nil nil time requestor selection target property)
		[ "SelectionNotify"
		  ([1 integerp]
		   [2 integerp]
		   [4 integerp]		; time
		   [4 :X-Win]		; requestor
		   [4 :X-Atom]		; selection atom
		   [4 :X-Atom]		; target atom
		   [4 :X-Atom]		; property atom
		   [8 nil])
		  3 ])
(X-Event-define X-ColormapNotify "xcolormap" (nil nil window colormap new state)
		[ "ColormapNotify"
		  ([1 integerp]		; detail
		   [2 integerp]		; sequence
		   [4 :X-Win]		; window
		   [4 integerp]		; colormap
		   [1 booleanp]		; new
		   [1 booleanp]		; state
		   [18 nil])
		  2 ])
(X-Event-define X-ClientMessage "xclient" (nil window atom msg)
		[ "ClientMessage"
		  ([1 length-1]		; format
		   [2 integerp]		; sequence number
		   [4 :X-Win]		; window
		   [4 :X-Atom]		; atom
		   ;; This reads in the correct number of integers of a type
		   ;; specified by the format which is 8, 16, or 32.
		   [(/ 20 (/ length-1 8)) ( [ (/ length-1 8) integerp ] ) ] )
		  1 ])
(X-Event-define X-MappingNotify "xmapping" (nil nil request first-keycode count)
		[ "MappingNotify"
		  ([1 integerp]
		   [2 integerp]
		   [1 integerp]		; request
		   [1 integerp]		; firstKeyCode
		   [1 integerp]		; count
		   [25 nil])
		  nil ])

;; error event
(X-Event-define 0 "xerror" (code nil resourceid min-op maj-op)
		["XError"
		 ([1 integerp]		; err code
		  [2 integerp]		; sequence
		  [4 integerp]		; id
		  [2 integerp]		; minor opcode
		  [1 integerp]		; major opcode
		  [21 nil])])
		 
;;; All receive message types will exclude the first byte which IDs it.
;;
;; a symbol gets 'set, functions such as integerp mean turn it into that,
;; and put it into the return list. 'arg means use next arg as this value.
(defun X-mod-4 (len)
  "Return a the number LEN moded to 4."
  (if (= (% len 4) 0) 0 (- 4 (% len 4))))

;;;###autoload
(defconst X-connect-response
  (list [1 success]
	(list [1 length-1]		; fail message len
	      [2 integerp]		; major version
	      [2 integerp]		; minor version
	      [2 length-2]		; pad length
	      [length-1 stringp]	; error conditions
	      [(X-mod-4 length-1) nil]	; padding
	      )
	(list [1 nil]			; successful list (this is unused)
	      [2 integerp]		; major version
	      [2 integerp]		; minor version
	      [2 length-1]		; len additional data (pad)
	      [4 integerp]		; release number
	      [4 integerp]		; resource id base
	      [4 integerp]		; resource id mask
	      [4 integerp]		; motion buffer size
	      [2 length-2]		; vendor length
	      [2 integerp]		; max request len
	      [1 length-4]		; number of screens
	      [1 length-3]		; number of formats in pix list
	      [1 integerp]		; image byte order
	      [1 integerp]		; bitmap byte order
	      [1 integerp]		; bitmap format scanline thingy
	      [1 integerp]		; bitmap format scanline pad
	      [1 integerp]		; min keycode
	      [1 integerp]		; max keycode
	      [4 nil]			; unused
	      [length-2 stringp]	; the vendor
	      [(X-mod-4 length-2) nil]	; padding
	      [length-3 		; sublist of formats
	       ( [1 integerp]		; depth
		 [1 integerp]		; bits/pixel
		 [1 integerp]		; scanline-pad
		 [5 nil] ) ]		; padding
	      [length-4
	       ( [4 integerp]		; root window
		 [4 integerp]		; colormap
		 [4 integerp]		; white-pixel
		 [4 integerp]		; black-pixel
		 [4 integerp]		; event-flags
		 [2 integerp]		; screen-width
		 [2 integerp]		; screen-height
		 [2 integerp]		; milimeters width
		 [2 integerp]		; milimeters height
		 [2 integerp]		; min-installed-maps
		 [2 integerp]		; max installed maps
		 [4 integerp]		; visualid
		 [1 integerp]		; backingstores
		 [1 booleanp]		; save-unders
		 [1 integerp]		; root depth
		 [1 length-1]		; # depths in depth
		 [length-1		; list of depths
		  ( [1 integerp]	; depth
		    [1 nil]
		    [2 length-1]	; # visual types
		    [4 nil]
		    [length-1		; the visuals
		     ( [4 integerp]	; visual id
		       [1 integerp]	; class
		       [1 integerp]	; bits/rgb value
		       [2 integerp]	; colormap entities
		       [4 integerp]	; red mask
		       [4 integerp]	; green mask
		       [4 integerp]	; blue mask
		       [4 nil])
		     ] )
		  ] )
	       ] )
	)
  "Connection response structure.")

;;; Protecting macros
;;;###autoload
(defmacro X-Dpy-read-excursion (xdpy &rest forms)
  "Execute FORMS in reading mode."
  `(let ((gc-cons-threshold most-positive-fixnum))	; inhibit GC'ing
     (progn
       (incf (X-Dpy-readings ,xdpy))
       (unwind-protect
	   (progn ,@forms)
	 (decf (X-Dpy-readings ,xdpy))))))

;;;###autoload
(defmacro X-Dpy-events-excursion (xdpy &rest forms)
  "Execute FORMS in safe manner."
  `(let ((gc-cons-threshold most-positive-fixnum))	; inhibit GC'ing
     (progn
       (incf (X-Dpy-evq-protects ,xdpy))
       (unwind-protect
	   (progn ,@forms)

	 (unwind-protect
	     (when (zerop (1- (X-Dpy-evq-protects ,xdpy)))
               (XSyncEvents ,xdpy))
	   (decf (X-Dpy-evq-protects ,xdpy)))))))

;;;###autoload
(defmacro X-Dpy-send-excursion (xdpy &rest forms)
  "Send all requests that appears in FORMS at once to X server.
Returns what FORMS returns.
Useful for drawing purposes."
  `(let ((gc-cons-threshold most-positive-fixnum))	; inhibit GC'ing
     (incf (X-Dpy-snd-protects ,xdpy))
     (unwind-protect
	 (progn ,@forms)

       (when (zerop (1- (X-Dpy-snd-protects ,xdpy)))
	 (when (> (length (X-Dpy-snd-buf ,xdpy)) 0)
	   (X-Dpy-send-flush ,xdpy (X-Dpy-snd-buf ,xdpy))
	   (setf (X-Dpy-snd-buf ,xdpy) "")))
       (decf (X-Dpy-snd-protects ,xdpy)))))

(put 'X-Dpy-read-excursion 'lisp-indent-function 1)
(put 'X-Dpy-events-excursion 'lisp-indent-function 1)
(put 'X-Dpy-send-excursion 'lisp-indent-function 1)

(defvar X-default-timeout 60
  "This should be big enought, larger than any XEmacs blocking.")

;;; Reading and parsing
(defun X-Dpy-grab-bytes (xdpy num &optional to-secs to-msecs)
  "On display XDPY, wait for at least NUM bytes and return string."
  (X-Dpy-p xdpy 'X-Dpy-grab-bytes)

  (let (rstr)
    (while (< (length (X-Dpy-message-buffer xdpy)) num)
      (when (null (accept-process-output (X-Dpy-proc xdpy)
                                         (or to-secs X-default-timeout) (or to-msecs 0)))
	;; Timeouted
	(error "X: Timeout while reading from server.")))

    (setq rstr (substring (X-Dpy-message-buffer xdpy) 0 num)) ; save bytes to string

    ;; Update message-buffer
    (setf (X-Dpy-message-buffer xdpy)
	  (substring (X-Dpy-message-buffer xdpy) num))
    rstr))

;; These are defined so we can use them recursivly below
(defvar length-1 nil)
(defvar length-2 nil)
(defvar length-3 nil)
(defvar length-4 nil)

;;;###autoload
(defun X-Dpy-parse-message (message-s may-guess xdpy &rest arglist)
  "Receive (via filter and waiting) a response from  the X server.
Parses MESSAGE-S structure.  When MAY-GUESS is t then if 1st el is not 1 or 0,
we must process as an event instead.  Then keep looping on guess until we get
a 0 or 1.  If not, then we are processing sub-lists.  Processing is done for
XDPY.  ARGLIST is some list of arguments.

When FROM-X-PARSE-MESSAGE is non-nil than we are called from `X-Dpy-parse-message'.

MESSAGE-S is made of size vectors `X-Dpy-create-message':

  [SIZE ENCODING]

  SIZE is how many bytes it occupies in the message.
  ENCODING is how to interpret it.

  If encoding is 'success, then the following vectors are two lists.
The first is the Failure case.  nil is a generic failure.
The second is the Success case.

  Encoding can also be one of the following:
  nil      -- Not used
  integerp -- Format integer
  stringp  -- Formatted string
  length-# -- Number stored in variable `length-#' where # is 0-4.

The length-# variables are used to read a length from one section
of a message, and use it as the size field of a later occuring field.
A variable-length string can occur like this:

  [2 length-0]       ; length of string, does not appear in the list
  [length-0 stringp] ; name"

  (X-Dpy-p xdpy 'X-Dpy-parse-message)

  (let ((rlist nil)
	(reverse-me t)
	(length-1 (if (boundp 'length-1) length-1 nil))
	(length-2 (if (boundp 'length-2) length-2 nil))
	(length-3 (if (boundp 'length-3) length-3 nil))
	(length-4 (if (boundp 'length-4) length-4 nil)) )
    (while (and message-s (listp message-s))
      (let* ((tvec (car message-s))
	     (tlen (aref tvec 0))
	     (tval1 (aref tvec 1))
	     (tval (if (and (listp tval1)
			    (member (car tval1) '(or if cond))) ;XXX
		       (eval tval1)
		     tval1))
	     (result (unless (and tval (listp tval))
		       ;; Do not grab bytes for sub-lists
		       (if (or (symbolp tlen) (listp tlen))
			   (X-Dpy-grab-bytes xdpy (eval tlen))
			 (X-Dpy-grab-bytes xdpy tlen)))))

	;; We need to put in code to represent sizes sometimes,
	;; this will get that size.
	(when (or (listp tlen) (symbolp tlen))
	  (setq tlen (eval tlen)))

	;; Check for use of an argument.
	(when (equal tval 'arg)
	  (setq tval (car arglist))
	  (setq arglist (cdr arglist)))

	;; If the val is a list, and it is an if statement, then
	;; we want to evaluate it to get the real tval type.
	(when (and (listp tval)
		   (member (car tval) '(if or make-list)))
	  (setq tval (eval tval)))

	(cond
	 ;; boolean success stories.
	 ((equal tval 'success)
	  (let ((sublst
		 (cond ((= (aref result 0) 1)
			;; success condition
			(setq result t)
			(X-Dpy-parse-message (car (cdr (cdr message-s))) nil xdpy arglist))

		       (t
			;; Here is event or error arrived, process
			;; errors in time or store event in events
			;; queue.
			(catch 'processed

			  (condition-case xerr
			      (X-Dpy-parse-event xdpy (Xforcenum (aref result 0)))
			    (X-Error
			     ;; Here is if error's sequence numbers matches
			     ;; with last request sequence, then end response
			     ;; evaluating.
			     (X-Dpy-log xdpy "Get ERROR seq: %d, rseq-id: %d\n" '(X-Event-seq (cdr xerr)) '(X-Dpy-rseq-id xdpy))
			     (when (= (X-Event-seq (cdr xerr)) (X-Dpy-reqseq xdpy))
			       (throw 'processed (setq result nil)))))

			  ;; Repeat processing XXX excluding t or nil
			  (let ((pmsg (X-Dpy-parse-message message-s nil xdpy arglist)))
			    (setq result (car pmsg))
			    (cdr pmsg))))
		       )))
	    (setq rlist (cons result sublst)))

	  (setq message-s nil)
	  (setq reverse-me nil))

	 ;; numberp means natural number, not safe!
	 ((eq tval 'numberp)
	  (setq rlist (cons (funcall (if (<= tlen 2)
					 'string2->number
				       'string4->number) result)
			    rlist)))

	 ;; integerp means tac onto end of list as an int
	 ((eq tval 'integerp)
	  (if (<= tlen 2)
	      (setq rlist (cons (string->int result) rlist))
	    (setq rlist (cons (string4->int result) rlist))))

	 ;; stringp means tac onto end of list as string (verbatim)
	 ((eq tval 'stringp)
	  (setq rlist (cons result rlist)))

	 ;; booleans don't really exist, but turn a 0 into nil, and 1 into t
	 ((eq tval 'booleanp)
	  (setq rlist (cons (if (= 0 (string->int result)) nil t) rlist)))

	 ;; TODO: maybe add card8, card16, card32, int8, int16, int32,
	 ;; string8, string16, etc?

	 ;; Special forms
	 ((eq tval :X-Rect)
	  (setq tlen (/ tlen 8))
	  (while (> tlen 0)
	    (setq rlist (cons (make-X-Rect :x (string->int (substring result 0 2))
					   :y (string->int (substring result 2 4))
					   :width (string->int (substring result 4 6))
					   :height (string->int (substring result 6 8)))
			      rlist))
	    (setq result (substring result 8))
	    (setq tlen (1- tlen))))

	 ((eq tval :X-Win)
	  (setq tlen (/ tlen 4))
	  (while (> tlen 0)
	      (setq rlist (cons (X-Win-find-or-make xdpy (string4->int result))
				rlist))
	      (setq result (substring result 4))
	      (setq tlen (1- tlen))))

	 ((eq tval :X-Atom)
	  (setq tlen (/ tlen 4))
	  (while (> tlen 0)
	      (setq rlist (cons (X-Atom-find-or-make xdpy (string4->int result))
				rlist))
	      (setq result (substring result 4))
	      (setq tlen (1- tlen))))

	 ;; if it is a list, then we need to recursivly call ourselvs X
	 ;; times on it.
	 ((and tval (listp tval))
	  ;; WARNING: subparts cannot use args. ;(
	  (let ((sublst nil))
	    (while (> tlen 0)
	      (setq sublst (cons (X-Dpy-parse-message tval nil xdpy arglist) sublst))
	      (setq tlen (1- tlen)))
	    ;; The sub-list of items is backwards: fix
	    (setq rlist (cons (nreverse sublst) rlist))))

	 ;; not a type, but some other symbol, then put it there!
	 ;; if it is one of the lengththings, intify it.
	 ((and tval (symbolp tval) (not (keywordp tval)))
	  (if (string-match "length" (symbol-name tval))
	      (set tval (string->int result))
	    (set tval result)))

	 ;; do nothing
	 ((equal tval nil))

	 ;; error case.
	 (t
	  (error "Error parsing X response!!!"))))
      (setq message-s (cdr message-s)))

    ;; Now that that is over, conditionally reverse the list.
    (if reverse-me
	(nreverse rlist)
      rlist)))

(defun X-Dpy-parse-event (xdpy evtype)
  "On XDPY construct and enqueue event of EVTYPE type."
  (X-Dpy-log xdpy "XLIB: Getting event ....\n")
  (X-Dpy-read-excursion xdpy
    (let* ((type evtype)
	   (synth (= (logand X-SyntheticMask type) X-SyntheticMask))
	   (type (if synth (- type X-SyntheticMask) type))
;	 (evdata (substring (X-Dpy-message-buffer xdpy) 0 31))
	   (xev (make-X-Event :dpy xdpy :type type :send-event synth)) ; :evdata (concat (char-to-string (XCharacter type)) evdata)))
	   (evspec (aref X-EventsList type))
	   (evin (X-Dpy-parse-message (or (and evspec (aref evspec 1)) (list [31 nil])) t xdpy)))

      (setf (X-Event-evinfo xev) evin)
      (if (= (X-Event-type xev) 0)
	  ;; Dispatch this error
	  (X-Dpy-error-dispatch xev)

	(X-Dpy-event-enqueue xdpy xev))
      xev)))

(defsubst X-Dpy-eval-error-or-event (xdpy)
  "There data on XDPY, it is error or event."
  (X-Dpy-read-excursion xdpy
    (let* ((result (X-Dpy-grab-bytes xdpy 1))
	   (evetype (Xforcenum (aref result 0))))

      (cond ((= evetype 1)		; reply, should not happen
	     (X-Dpy-log xdpy "Get unknonw reply\n")
	     nil)

	    (t (X-Dpy-parse-event xdpy evetype))) ; error or event
      )))

;;; Events queue support
;;;###autoload
(defmacro X-Generic-enqueue (obj queue)
  "Enqueue object QBJ into setf'able QUEUE."
  `(if (null ,queue)
       (setf ,queue (list ,obj))
     (setcdr (last ,queue) (list ,obj))))

(defmacro X-Generic-prequeue (obj queue)
  "Prepend object OBJ into setf'able QUEUE."
  `(setf ,queue (cons ,obj ,queue)))

(defmacro X-Generic-dequeue (queue)
  "Dequeue first object from setf'able QUEUE."
  `(let ((obj (car ,queue)))
     (setf ,queue (cdr ,queue))
     obj))

;;;###autoload
(defsubst X-Dpy-event-enqueue (xdpy event)
  "Enqueue EVENT in XDPY's events queue."
  (X-Generic-enqueue event (X-Dpy-evq xdpy)))

(defsubst X-Dpy-event-prequeue (xdpy event)
  "Prqueuue EVENT in XDPY's events queue."
  (X-Generic-prequeue event (X-Dpy-evq xdpy)))

(defsubst X-Dpy-event-dequeue (xdpy)
  "Dequeue event from XDPY's events queue and return."
  (X-Generic-dequeue (X-Dpy-evq xdpy)))

;; Events/Errors dispatchers
(defun X-Dpy-error-dispatch (xev)
  "Dispatch error event XEV."
  (let ((xdpy (X-Event-dpy xev)))
    (cond ((= (X-Event-xerror-code xev) 2)
	   (X-Dpy-log xdpy "Bad value %s sequence %d ops %d %d\n"
		      '(Xmask-string (X-Event-xerror-resourceid xev))
		      '(X-Event-seq xev)
		      '(X-Event-xerror-maj-op xev)
		      '(X-Event-xerror-min-op xev)))

	  ((= (X-Event-xerror-code xev) 3)
	   (X-Dpy-log xdpy "Bad window %.0f sequence %d ops %d %d\n"
		      '(X-Event-xerror-resourceid xev)
		      '(X-Event-seq xev)
		      '(X-Event-xerror-maj-op xev)
		      '(X-Event-xerror-min-op xev)))
	  ((= (X-Event-xerror-code xev) 9)
	   (X-Dpy-log xdpy "Bad Drawable %.0f sequence %d ops %d %d\n"
		      '(X-Event-xerror-resourceid xev)
		      '(X-Event-seq xev)
		      '(X-Event-xerror-maj-op xev)
		      '(X-Event-xerror-min-op xev)))

	  ((= (X-Event-xerror-code xev) 11)
	   (X-Dpy-log xdpy "Alloc failure id=%.0f\n" '(X-Event-xerror-resourceid xev)))

	  ((= (X-Event-xerror-code xev) 14)
	   (X-Dpy-log xdpy "Bad id %s sequence %d ops %d %d\n"
		      '(Xmask-string (X-Event-xerror-resourceid xev))
		      '(X-Event-seq xev)
		      '(X-Event-xerror-maj-op xev)
		      '(X-Event-xerror-min-op xev)))

	  ((= (X-Event-xerror-code xev) 16)
	   (X-Dpy-log xdpy "Length error! sequence %d ops %d %d\n"
		      '(X-Event-seq xev)
		      '(X-Event-xerror-maj-op xev)
		      '(X-Event-xerror-min-op xev)))

	   (t
	    (X-Dpy-log xdpy "Got error event %d!!!\n" '(X-Event-xerror-code xev))))

    ;; Now run hooks if any
    (when (X-Dpy-error-hooks xdpy)
      (mapcar (lambda (fun)
		(funcall fun xdpy xev))
	      (X-Dpy-error-hooks xdpy)))

    ;; Finnally signal an error.
    (signal-error 'X-Error xev)
    ))

(defun X-Dpy-event-dispatch (xev)
  "Dispatch event XEV."
  (let ((win (X-Event-win xev))
	(xdpy (X-Event-dpy xev)))

    (X-Dpy-log xdpy "Ready to dispatch event: %S for win %S\n"
	       '(X-Event-name xev) '(if (X-Win-p (X-Event-win xev))
					(X-Win-id (X-Event-win xev))
				      (X-Event-win xev)))

    (when (X-Dpy-events-dispatcher xdpy)
      (funcall (X-Dpy-events-dispatcher xdpy) xdpy win xev))))

(defun X-Dpy-process-events (xdpy)
  "Process all events in XDPY's events queue."
  (X-Dpy-p xdpy 'X-Dpy-process-events)

  (while (X-Dpy-evq xdpy)
    (X-Dpy-event-dispatch (X-Dpy-event-dequeue xdpy))))

;;; Function to call when there data in XDPY, but noone reading it.
(defun X-Dpy-parse-message-guess (xdpy)
  "There is data waiting on XDPY, but no-one is reading it.
Try to guess what it is."
  (X-Dpy-p xdpy 'X-Dpy-parse-message-guess)

  ;; If no-one reading now, mean than error or event arrived.
  (when (zerop (X-Dpy-readings xdpy))
    (while (> (length (X-Dpy-message-buffer xdpy)) 0)
      (X-Dpy-eval-error-or-event xdpy))

    ;; Execute events
    (when (zerop (X-Dpy-evq-protects xdpy))
      (incf (X-Dpy-evq-protects xdpy))
      (unwind-protect
          (X-Dpy-process-events xdpy)
        (decf (X-Dpy-evq-protects xdpy))))
    ))


(provide 'xlib-xr)

;;; xlib-xr.el ends here