home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / CLX / dependent.l < prev    next >
Encoding:
Text File  |  1991-08-18  |  109.5 KB  |  3,330 lines

  1. ;;; -*- Mode: LISP; Syntax: Common-lisp; Package: XLIB; Base: 10; Lowercase: Yes -*-
  2.  
  3. ;; This file contains some of the system dependent code for CLX
  4.  
  5. ;;;
  6. ;;;             TEXAS INSTRUMENTS INCORPORATED
  7. ;;;                  P.O. BOX 2909
  8. ;;;                   AUSTIN, TEXAS 78769
  9. ;;;
  10. ;;; Copyright (C) 1987 Texas Instruments Incorporated.
  11. ;;;
  12. ;;; Permission is granted to any individual or institution to use, copy, modify,
  13. ;;; and distribute this software, provided that this complete copyright and
  14. ;;; permission notice is maintained, intact, in all copies and supporting
  15. ;;; documentation.
  16. ;;;
  17. ;;; Texas Instruments Incorporated provides this software "as is" without
  18. ;;; express or implied warranty.
  19. ;;;
  20.  
  21. (in-package :xlib)
  22.  
  23. ;;; The size of the output buffer.  Must be a multiple of 4.
  24. (defparameter *output-buffer-size* 8192)
  25.  
  26. #+explorer
  27. (zwei:define-indentation event-case (1 1))
  28.  
  29. ;;; Number of seconds to wait for a reply to a server request
  30. (defparameter *reply-timeout* nil) 
  31.  
  32. #-(or clx-overlapping-arrays (not clx-little-endian))
  33. (progn
  34.   (defconstant *word-0* 0)
  35.   (defconstant *word-1* 1)
  36.  
  37.   (defconstant *long-0* 0)
  38.   (defconstant *long-1* 1)
  39.   (defconstant *long-2* 2)
  40.   (defconstant *long-3* 3))
  41.  
  42. #-(or clx-overlapping-arrays clx-little-endian)
  43. (progn
  44.   (defconstant *word-0* 1)
  45.   (defconstant *word-1* 0)
  46.  
  47.   (defconstant *long-0* 3)
  48.   (defconstant *long-1* 2)
  49.   (defconstant *long-2* 1)
  50.   (defconstant *long-3* 0))
  51.  
  52. ;;; Set some compiler-options for often used code
  53.  
  54. (eval-when (eval compile load)
  55.  
  56. (defconstant *buffer-speed* 3
  57.   "Speed compiler option for buffer code.")
  58. (defconstant *buffer-safety* #+clx-debugging 3 #-clx-debugging 0
  59.   "Safety compiler option for buffer code.")
  60.  
  61. (defun declare-bufmac ()
  62.   `(declare (optimize (speed ,*buffer-speed*) (safety ,*buffer-safety*))))
  63.  
  64. ;;; It's my impression that in lucid there's some way to make a declaration
  65. ;;; called fast-entry or something that causes a function to not do some
  66. ;;; checking on args. Sadly, we have no lucid manuals here.  If such a
  67. ;;; declaration is available, it would be a good idea to make it here when
  68. ;;; *buffer-speed* is 3 and *buffer-safety* is 0.
  69. (defun declare-buffun ()
  70.   `(declare (optimize (speed ,*buffer-speed*) (safety ,*buffer-safety*))))
  71.  
  72. )
  73.  
  74. (declaim (inline card8->int8 int8->card8
  75.          card16->int16 int16->card16
  76.          card32->int32 int32->card32))
  77.  
  78. #-Genera
  79. (progn
  80.  
  81. (defun card8->int8 (x)
  82.   (declare (type card8 x))
  83.   (declare (values int8))
  84.   #.(declare-buffun)
  85.   (the int8 (if (logbitp 7 x)
  86.         (the int8 (- x #x100))
  87.           x)))
  88.  
  89. (defun int8->card8 (x)
  90.   (declare (type int8 x))
  91.   (declare (values card8))
  92.   #.(declare-buffun)
  93.   (the card8 (ldb (byte 8 0) x)))
  94.  
  95. (defun card16->int16 (x)
  96.   (declare (type card16 x))
  97.   (declare (values int16))
  98.   #.(declare-buffun)
  99.   (the int16 (if (logbitp 15 x)
  100.          (the int16 (- x #x10000))
  101.          x)))
  102.  
  103. (defun int16->card16 (x)
  104.   (declare (type int16 x))
  105.   (declare (values card16))
  106.   #.(declare-buffun)
  107.   (the card16 (ldb (byte 16 0) x)))
  108.  
  109. (defun card32->int32 (x)
  110.   (declare (type card32 x))
  111.   (declare (values int32))
  112.   #.(declare-buffun)
  113.   (the int32 (if (logbitp 31 x)
  114.          (the int32 (- x #x100000000))
  115.          x)))
  116.  
  117. (defun int32->card32 (x)
  118.   (declare (type int32 x))
  119.   (declare (values card32))
  120.   #.(declare-buffun)
  121.   (the card32 (ldb (byte 32 0) x)))
  122.  
  123. )
  124.  
  125. #+Genera
  126. (progn
  127.  
  128. (defun card8->int8 (x)
  129.   (declare lt:(side-effects simple reducible))
  130.   (if (logbitp 7 x) (- x #x100) x))
  131.  
  132. (defun int8->card8 (x)
  133.   (declare lt:(side-effects simple reducible))
  134.   (ldb (byte 8 0) x))
  135.  
  136. (defun card16->int16 (x)
  137.   (declare lt:(side-effects simple reducible))
  138.   (if (logbitp 15 x) (- x #x10000) x))
  139.  
  140. (defun int16->card16 (x)
  141.   (declare lt:(side-effects simple reducible))
  142.   (ldb (byte 16 0) x))
  143.  
  144. (defun card32->int32 (x)
  145.   (declare lt:(side-effects simple reducible))
  146.   (sys:%logldb (byte 32 0) x))
  147.  
  148. (defun int32->card32 (x)
  149.   (declare lt:(side-effects simple reducible))
  150.   (ldb (byte 32 0) x))
  151.  
  152. )
  153.  
  154. (declaim (inline aref-card8 aset-card8 aref-int8 aset-int8))
  155.  
  156. #-(or Genera lcl3.0 excl)
  157. (progn
  158.  
  159. (defun aref-card8 (a i)
  160.   (declare (type buffer-bytes a)
  161.        (type array-index i))
  162.   (declare (values card8))
  163.   #.(declare-buffun)
  164.   (the card8 (aref a i)))
  165.  
  166. (defun aset-card8 (v a i)
  167.   (declare (type card8 v)
  168.        (type buffer-bytes a)
  169.        (type array-index i))
  170.   #.(declare-buffun)
  171.   (setf (aref a i) v))
  172.  
  173. (defun aref-int8 (a i)
  174.   (declare (type buffer-bytes a)
  175.        (type array-index i))
  176.   (declare (values int8))
  177.   #.(declare-buffun)
  178.   (card8->int8 (aref a i)))
  179.  
  180. (defun aset-int8 (v a i)
  181.   (declare (type int8 v)
  182.        (type buffer-bytes a)
  183.        (type array-index i))
  184.   #.(declare-buffun)
  185.   (setf (aref a i) (int8->card8 v)))
  186.  
  187. )
  188.  
  189. #+Genera
  190. (progn
  191.  
  192. (defun aref-card8 (a i)
  193.   (aref a i))
  194.  
  195. (defun aset-card8 (v a i)
  196.   (zl:aset v a i))
  197.  
  198. (defun aref-int8 (a i)
  199.   (card8->int8 (aref a i)))
  200.  
  201. (defun aset-int8 (v a i)
  202.   (zl:aset (int8->card8 v) a i))
  203.  
  204. )
  205.  
  206. #+(or excl lcl3.0 clx-overlapping-arrays)
  207. (declaim (inline aref-card16 aref-int16 aref-card32 aref-int32 aref-card29
  208.          aset-card16 aset-int16 aset-card32 aset-int32 aset-card29))
  209.  
  210. #+(and clx-overlapping-arrays Genera)
  211. (progn
  212.  
  213. (defun aref-card16 (a i)
  214.   (aref a i))
  215.  
  216. (defun aset-card16 (v a i)
  217.   (zl:aset v a i))
  218.  
  219. (defun aref-int16 (a i)
  220.   (card16->int16 (aref a i)))
  221.  
  222. (defun aset-int16 (v a i)
  223.   (zl:aset (int16->card16 v) a i)
  224.   v)
  225.  
  226. (defun aref-card32 (a i)
  227.   (int32->card32 (aref a i)))
  228.  
  229. (defun aset-card32 (v a i)
  230.   (zl:aset (card32->int32 v) a i))
  231.  
  232. (defun aref-int32 (a i) (aref a i))
  233.  
  234. (defun aset-int32 (v a i)
  235.   (zl:aset v a i))
  236.  
  237. (defun aref-card29 (a i)
  238.   (aref a i))
  239.  
  240. (defun aset-card29 (v a i)
  241.   (zl:aset v a i))
  242.  
  243. )
  244.  
  245. #+(and clx-overlapping-arrays (not Genera))
  246. (progn
  247.  
  248. (defun aref-card16 (a i)
  249.   (aref a i))
  250.  
  251. (defun aset-card16 (v a i)
  252.   (setf (aref a i) v))
  253.  
  254. (defun aref-int16 (a i)
  255.   (card16->int16 (aref a i)))
  256.  
  257. (defun aset-int16 (v a i)
  258.   (setf (aref a i) (int16->card16 v))
  259.   v)
  260.  
  261. (defun aref-card32 (a i)
  262.   (aref a i))
  263.  
  264. (defun aset-card32 (v a i)
  265.   (setf (aref a i) v))
  266.  
  267. (defun aref-int32 (a i)
  268.   (card32->int32 (aref a i)))
  269.  
  270. (defun aset-int32 (v a i)
  271.   (setf (aref a i) (int32->card32 v))
  272.   v)
  273.  
  274. (defun aref-card29 (a i)
  275.   (aref a i))
  276.  
  277. (defun aset-card29 (v a i)
  278.   (setf (aref a i) v))
  279.  
  280. )
  281.  
  282. #+excl
  283. (progn
  284.   
  285. (defun aref-card8 (a i)
  286.   (declare (type buffer-bytes a)
  287.        (type array-index i))
  288.   (declare (values card8))
  289.   #.(declare-buffun)
  290.   (the card8 (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  291.              :unsigned-byte)))
  292.  
  293. (defun aset-card8 (v a i)
  294.   (declare (type card8 v)
  295.        (type buffer-bytes a)
  296.        (type array-index i))
  297.   #.(declare-buffun)
  298.   (setf (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  299.             :unsigned-byte) v))
  300.  
  301. (defun aref-int8 (a i)
  302.   (declare (type buffer-bytes a)
  303.        (type array-index i))
  304.   (declare (values int8))
  305.   #.(declare-buffun)
  306.   (the int8 (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  307.             :signed-byte)))
  308.  
  309. (defun aset-int8 (v a i)
  310.   (declare (type int8 v)
  311.        (type buffer-bytes a)
  312.        (type array-index i))
  313.   #.(declare-buffun)
  314.   (setf (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  315.             :signed-byte) v))
  316.  
  317. (defun aref-card16 (a i)
  318.   (declare (type buffer-bytes a)
  319.        (type array-index i))
  320.   (declare (values card16))
  321.   #.(declare-buffun)
  322.   (the card16 (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  323.               :unsigned-word)))
  324.   
  325. (defun aset-card16 (v a i)
  326.   (declare (type card16 v)
  327.        (type buffer-bytes a)
  328.        (type array-index i))
  329.   #.(declare-buffun)
  330.   (setf (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  331.             :unsigned-word) v))
  332.   
  333. (defun aref-int16 (a i)
  334.   (declare (type buffer-bytes a)
  335.        (type array-index i))
  336.   (declare (values int16))
  337.   #.(declare-buffun)
  338.   (the int16 (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  339.              :signed-word)))
  340.   
  341. (defun aset-int16 (v a i)
  342.   (declare (type int16 v)
  343.        (type buffer-bytes a)
  344.        (type array-index i))
  345.   #.(declare-buffun)
  346.   (setf (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  347.             :signed-word) v))
  348.   
  349. (defun aref-card32 (a i)
  350.   (declare (type buffer-bytes a)
  351.        (type array-index i))
  352.   (declare (values card32))
  353.   #.(declare-buffun)
  354.   (the card32 (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  355.               :unsigned-long)))
  356.     
  357. (defun aset-card32 (v a i)
  358.   (declare (type card32 v)
  359.        (type buffer-bytes a)
  360.        (type array-index i))
  361.   #.(declare-buffun)
  362.   (setf (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  363.             :unsigned-long) v))
  364.  
  365. (defun aref-int32 (a i)
  366.   (declare (type buffer-bytes a)
  367.        (type array-index i))
  368.   (declare (values int32))
  369.   #.(declare-buffun)
  370.   (the int32 (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  371.              :signed-long)))
  372.     
  373. (defun aset-int32 (v a i)
  374.   (declare (type int32 v)
  375.        (type buffer-bytes a)
  376.        (type array-index i))
  377.   #.(declare-buffun)
  378.   (setf (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  379.             :signed-long) v))
  380.  
  381. (defun aref-card29 (a i)
  382.   (declare (type buffer-bytes a)
  383.        (type array-index i))
  384.   (declare (values card29))
  385.   #.(declare-buffun)
  386.   (the card29 (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  387.               :unsigned-long)))
  388.  
  389. (defun aset-card29 (v a i)
  390.   (declare (type card29 v)
  391.        (type buffer-bytes a)
  392.        (type array-index i))
  393.   #.(declare-buffun)
  394.   (setf (sys:memref a #.(comp::mdparam 'comp::md-svector-data0-adj) i
  395.             :unsigned-long) v))
  396.   
  397. )
  398.  
  399. #+lcl3.0
  400. (progn
  401.  
  402. (defun aref-card8 (a i)
  403.   (declare (type buffer-bytes a)
  404.        (type array-index i)
  405.        (values card8))
  406.   #.(declare-buffun)
  407.   (the card8 (lucid::%svref-8bit a i)))
  408.  
  409. (defun aset-card8 (v a i)
  410.   (declare (type card8 v)
  411.        (type buffer-bytes a)
  412.        (type array-index i))
  413.   #.(declare-buffun)
  414.   (setf (lucid::%svref-8bit a i) v))
  415.  
  416. (defun aref-int8 (a i)
  417.   (declare (type buffer-bytes a)
  418.        (type array-index i)
  419.        (values int8))
  420.   #.(declare-buffun)
  421.   (the int8 (lucid::%svref-signed-8bit a i)))
  422.  
  423. (defun aset-int8 (v a i)
  424.   (declare (type int8 v)
  425.        (type buffer-bytes a)
  426.        (type array-index i))
  427.   #.(declare-buffun)
  428.   (setf (lucid::%svref-signed-8bit a i) v))
  429.  
  430. (defun aref-card16 (a i)
  431.   (declare (type buffer-bytes a)
  432.        (type array-index i)
  433.        (values card16))
  434.   #.(declare-buffun)
  435.   (the card16 (lucid::%svref-16bit a (index-ash i -1))))
  436.   
  437. (defun aset-card16 (v a i)
  438.   (declare (type card16 v)
  439.        (type buffer-bytes a)
  440.        (type array-index i))
  441.   #.(declare-buffun)
  442.   (setf (lucid::%svref-16bit a (index-ash i -1)) v))
  443.   
  444. (defun aref-int16 (a i)
  445.   (declare (type buffer-bytes a)
  446.        (type array-index i)
  447.        (values int16))
  448.   #.(declare-buffun)
  449.   (the int16 (lucid::%svref-signed-16bit a (index-ash i -1))))
  450.   
  451. (defun aset-int16 (v a i)
  452.   (declare (type int16 v)
  453.        (type buffer-bytes a)
  454.        (type array-index i))
  455.   #.(declare-buffun)
  456.   (setf (lucid::%svref-signed-16bit a (index-ash i -1)) v))
  457.  
  458. (defun aref-card32 (a i)
  459.   (declare (type buffer-bytes a)
  460.        (type array-index i)
  461.        (values card32))
  462.   #.(declare-buffun)
  463.   (the card32 (lucid::%svref-32bit a (index-ash i -2))))
  464.     
  465. (defun aset-card32 (v a i)
  466.   (declare (type card32 v)
  467.        (type buffer-bytes a)
  468.        (type array-index i))
  469.   #.(declare-buffun)
  470.   (setf (lucid::%svref-32bit a (index-ash i -2)) v))
  471.  
  472. (defun aref-int32 (a i)
  473.   (declare (type buffer-bytes a)
  474.        (type array-index i)
  475.        (values int32))
  476.   #.(declare-buffun)
  477.   (the int32 (lucid::%svref-signed-32bit a (index-ash i -2))))
  478.     
  479. (defun aset-int32 (v a i)
  480.   (declare (type int32 v)
  481.        (type buffer-bytes a)
  482.        (type array-index i))
  483.   #.(declare-buffun)
  484.   (setf (lucid::%svref-signed-32bit a (index-ash i -2)) v))
  485.  
  486. (defun aref-card29 (a i)
  487.   (declare (type buffer-bytes a)
  488.        (type array-index i)
  489.        (values card29))
  490.   #.(declare-buffun)
  491.   (the card29 (lucid::%svref-32bit a (index-ash i -2))))
  492.  
  493. (defun aset-card29 (v a i)
  494.   (declare (type card29 v)
  495.        (type buffer-bytes a)
  496.        (type array-index i))
  497.   #.(declare-buffun)
  498.   (setf (lucid::%svref-32bit a (index-ash i -2)) v))
  499.  
  500. )
  501.  
  502.  
  503.  
  504. #-(or excl lcl3.0 clx-overlapping-arrays)
  505. (progn
  506.  
  507. (defun aref-card16 (a i)
  508.   (declare (type buffer-bytes a)
  509.        (type array-index i))
  510.   (declare (values card16))
  511.   #.(declare-buffun)
  512.   (the card16
  513.        (logior (the card16
  514.             (ash (the card8 (aref a (index+ i *word-1*))) 8))
  515.            (the card8
  516.             (aref a (index+ i *word-0*))))))
  517.  
  518. (defun aset-card16 (v a i)
  519.   (declare (type card16 v)
  520.        (type buffer-bytes a)
  521.        (type array-index i))
  522.   #.(declare-buffun)
  523.   (setf (aref a (index+ i *word-1*)) (the card8 (ldb (byte 8 8) v))
  524.     (aref a (index+ i *word-0*)) (the card8 (ldb (byte 8 0) v)))
  525.   v)
  526.  
  527. (defun aref-int16 (a i)
  528.   (declare (type buffer-bytes a)
  529.        (type array-index i))
  530.   (declare (values int16))
  531.   #.(declare-buffun)
  532.   (the int16
  533.        (logior (the int16
  534.             (ash (the int8 (aref-int8 a (index+ i *word-1*))) 8))
  535.            (the card8
  536.             (aref a (index+ i *word-0*))))))
  537.  
  538. (defun aset-int16 (v a i)
  539.   (declare (type int16 v)
  540.        (type buffer-bytes a)
  541.        (type array-index i))
  542.   #.(declare-buffun)
  543.   (setf (aref a (index+ i *word-1*)) (the card8 (ldb (byte 8 8) v))
  544.     (aref a (index+ i *word-0*)) (the card8 (ldb (byte 8 0) v)))
  545.   v)
  546.  
  547. (defun aref-card32 (a i)
  548.   (declare (type buffer-bytes a)
  549.        (type array-index i))
  550.   (declare (values card32))
  551.   #.(declare-buffun)
  552.   (the card32
  553.        (logior (the card32
  554.             (ash (the card8 (aref a (index+ i *long-3*))) 24))
  555.            (the card29
  556.             (ash (the card8 (aref a (index+ i *long-2*))) 16))
  557.            (the card16
  558.             (ash (the card8 (aref a (index+ i *long-1*))) 8))
  559.            (the card8
  560.             (aref a (index+ i *long-0*))))))
  561.  
  562. (defun aset-card32 (v a i)
  563.   (declare (type card32 v)
  564.        (type buffer-bytes a)
  565.        (type array-index i))
  566.   #.(declare-buffun)
  567.   (setf (aref a (index+ i *long-3*)) (the card8 (ldb (byte 8 24) v))
  568.     (aref a (index+ i *long-2*)) (the card8 (ldb (byte 8 16) v))
  569.     (aref a (index+ i *long-1*)) (the card8 (ldb (byte 8 8) v))
  570.     (aref a (index+ i *long-0*)) (the card8 (ldb (byte 8 0) v)))
  571.   v)
  572.  
  573. (defun aref-int32 (a i)
  574.   (declare (type buffer-bytes a)
  575.        (type array-index i))
  576.   (declare (values int32))
  577.   #.(declare-buffun)
  578.   (the int32
  579.        (logior (the int32
  580.             (ash (the int8 (aref-int8 a (index+ i *long-3*))) 24))
  581.            (the card29
  582.             (ash (the card8 (aref a (index+ i *long-2*))) 16))
  583.            (the card16
  584.             (ash (the card8 (aref a (index+ i *long-1*))) 8))
  585.            (the card8
  586.             (aref a (index+ i *long-0*))))))
  587.  
  588. (defun aset-int32 (v a i)
  589.   (declare (type int32 v)
  590.        (type buffer-bytes a)
  591.        (type array-index i))
  592.   #.(declare-buffun)
  593.   (setf (aref a (index+ i *long-3*)) (the card8 (ldb (byte 8 24) v))
  594.     (aref a (index+ i *long-2*)) (the card8 (ldb (byte 8 16) v))
  595.     (aref a (index+ i *long-1*)) (the card8 (ldb (byte 8 8) v))
  596.     (aref a (index+ i *long-0*)) (the card8 (ldb (byte 8 0) v)))
  597.   v)
  598.  
  599. (defun aref-card29 (a i)
  600.   (declare (type buffer-bytes a)
  601.        (type array-index i))
  602.   (declare (values card29))
  603.   #.(declare-buffun)
  604.   (the card29
  605.        (logior (the card29
  606.             (ash (the card8 (aref a (index+ i *long-3*))) 24))
  607.            (the card29
  608.             (ash (the card8 (aref a (index+ i *long-2*))) 16))
  609.            (the card16
  610.             (ash (the card8 (aref a (index+ i *long-1*))) 8))
  611.            (the card8
  612.             (aref a (index+ i *long-0*))))))
  613.  
  614. (defun aset-card29 (v a i)
  615.   (declare (type card29 v)
  616.        (type buffer-bytes a)
  617.        (type array-index i))
  618.   #.(declare-buffun)
  619.   (setf (aref a (index+ i *long-3*)) (the card8 (ldb (byte 8 24) v))
  620.     (aref a (index+ i *long-2*)) (the card8 (ldb (byte 8 16) v))
  621.     (aref a (index+ i *long-1*)) (the card8 (ldb (byte 8 8) v))
  622.     (aref a (index+ i *long-0*)) (the card8 (ldb (byte 8 0) v)))
  623.   v)
  624.  
  625. )
  626.  
  627. (defsetf aref-card8 (a i) (v)
  628.   `(aset-card8 ,v ,a ,i))
  629.  
  630. (defsetf aref-int8 (a i) (v)
  631.   `(aset-int8 ,v ,a ,i))
  632.  
  633. (defsetf aref-card16 (a i) (v)
  634.   `(aset-card16 ,v ,a ,i))
  635.  
  636. (defsetf aref-int16 (a i) (v)
  637.   `(aset-int16 ,v ,a ,i))
  638.  
  639. (defsetf aref-card32 (a i) (v)
  640.   `(aset-card32 ,v ,a ,i))
  641.  
  642. (defsetf aref-int32 (a i) (v)
  643.   `(aset-int32 ,v ,a ,i))
  644.  
  645. (defsetf aref-card29 (a i) (v)
  646.   `(aset-card29 ,v ,a ,i))
  647.  
  648. ;;; Other random conversions
  649.  
  650. (defun rgb-val->card16 (value)
  651.   ;; Short floats are good enough
  652.   (declare (type rgb-val value))
  653.   (declare (values card16))
  654.   #.(declare-buffun)
  655.   ;; Convert VALUE from float to card16
  656.   (the card16 (values (round (the rgb-val value) #.(/ 1.0s0 #xffff)))))
  657.  
  658. (defun card16->rgb-val (value) 
  659.   ;; Short floats are good enough
  660.   (declare (type card16 value))
  661.   (declare (values short-float))
  662.   #.(declare-buffun)
  663.   ;; Convert VALUE from card16 to float
  664.   (the short-float (* (the card16 value) #.(/ 1.0s0 #xffff))))
  665.  
  666. (defun radians->int16 (value)
  667.   ;; Short floats are good enough
  668.   (declare (type angle value))
  669.   (declare (values int16))
  670.   #.(declare-buffun)
  671.   (the int16 (values (round (the angle value) #.(float (/ pi 180.0s0 64.0s0) 0.0s0)))))
  672.  
  673. (defun int16->radians (value)
  674.   ;; Short floats are good enough
  675.   (declare (type int16 value))
  676.   (declare (values short-float))
  677.   #.(declare-buffun)
  678.   (the short-float (* (the int16 value) #.(coerce (/ pi 180.0 64.0) 'short-float))))
  679.  
  680.  
  681. ;;-----------------------------------------------------------------------------
  682. ;; Character transformation
  683. ;;-----------------------------------------------------------------------------
  684.  
  685.  
  686. ;;; This stuff transforms chars to ascii codes in card8's and back.
  687. ;;; You might have to hack it a little to get it to work for your machine.
  688.  
  689. (declaim (inline char->card8 card8->char))
  690.  
  691. (macrolet ((char-translators ()
  692.          (let ((alist
  693.              `(#-lispm
  694.                ;; The normal ascii codes for the control characters.
  695.                ,@`((#\Return . 13)
  696.                (#\Linefeed . 10)
  697.                (#\Rubout . 127)
  698.                (#\Page . 12)
  699.                (#\Tab . 9)
  700.                (#\Backspace . 8)
  701.                (#\Newline . 10)
  702.                (#\Space . 32))
  703.                ;; One the lispm, #\Newline is #\Return, but we'd really like
  704.                ;; #\Newline to translate to ascii code 10, so we swap the
  705.                ;; Ascii codes for #\Return and #\Linefeed. We also provide
  706.                ;; mappings from the counterparts of these control characters
  707.                ;; so that the character mapping from the lisp machine
  708.                ;; character set to ascii is invertible.
  709.                #+lispm
  710.                ,@`((#\Return . 10)   (,(code-char  10) . ,(char-code #\Return))
  711.                (#\Linefeed . 13) (,(code-char  13) . ,(char-code #\Linefeed))
  712.                (#\Rubout . 127)  (,(code-char 127) . ,(char-code #\Rubout))
  713.                (#\Page . 12)     (,(code-char  12) . ,(char-code #\Page))
  714.                (#\Tab . 9)       (,(code-char   9) . ,(char-code #\Tab))
  715.                (#\Backspace . 8) (,(code-char   8) . ,(char-code #\Backspace))
  716.                (#\Newline . 10)  (,(code-char  10) . ,(char-code #\Newline))
  717.                (#\Space . 32)    (,(code-char  32) . ,(char-code #\Space)))
  718.                ;; The rest of the common lisp charater set with the normal
  719.                ;; ascii codes for them.
  720.                (#\! . 33) (#\" . 34) (#\# . 35) (#\$ . 36)
  721.                (#\% . 37) (#\& . 38) (#\' . 39) (#\( . 40)
  722.                (#\) . 41) (#\* . 42) (#\+ . 43) (#\, . 44)
  723.                (#\- . 45) (#\. . 46) (#\/ . 47) (#\0 . 48)
  724.                (#\1 . 49) (#\2 . 50) (#\3 . 51) (#\4 . 52)
  725.                (#\5 . 53) (#\6 . 54) (#\7 . 55) (#\8 . 56)
  726.                (#\9 . 57) (#\: . 58) (#\; . 59) (#\< . 60)
  727.                (#\= . 61) (#\> . 62) (#\? . 63) (#\@ . 64)
  728.                (#\A . 65) (#\B . 66) (#\C . 67) (#\D . 68)
  729.                (#\E . 69) (#\F . 70) (#\G . 71) (#\H . 72)
  730.                (#\I . 73) (#\J . 74) (#\K . 75) (#\L . 76)
  731.                (#\M . 77) (#\N . 78) (#\O . 79) (#\P . 80)
  732.                (#\Q . 81) (#\R . 82) (#\S . 83) (#\T . 84)
  733.                (#\U . 85) (#\V . 86) (#\W . 87) (#\X . 88)
  734.                (#\Y . 89) (#\Z . 90) (#\[ . 91) (#\\ . 92)
  735.                (#\] . 93) (#\^ . 94) (#\_ . 95) (#\` . 96)
  736.                (#\a . 97) (#\b . 98) (#\c . 99) (#\d . 100)
  737.                (#\e . 101) (#\f . 102) (#\g . 103) (#\h . 104)
  738.                (#\i . 105) (#\j . 106) (#\k . 107) (#\l . 108)
  739.                (#\m . 109) (#\n . 110) (#\o . 111) (#\p . 112)
  740.                (#\q . 113) (#\r . 114) (#\s . 115) (#\t . 116)
  741.                (#\u . 117) (#\v . 118) (#\w . 119) (#\x . 120)
  742.                (#\y . 121) (#\z . 122) (#\{ . 123) (#\| . 124)
  743.                (#\} . 125) (#\~ . 126))))
  744.            (cond ((dolist (pair alist nil)
  745.             (when (not (= (char-code (car pair)) (cdr pair)))
  746.               (return t)))
  747.               `(progn
  748.              (defconstant *char-to-card8-translation-table*
  749.                       ',(let ((array (make-array
  750.                                (let ((max-char-code 255))
  751.                              (dolist (pair alist)
  752.                                (setq max-char-code
  753.                                  (max max-char-code
  754.                                       (char-code (car pair)))))
  755.                              (1+ max-char-code))
  756.                                :element-type 'card8)))
  757.                       (dotimes (i (length array))
  758.                         (setf (aref array i) (mod i 256)))
  759.                       (dolist (pair alist)
  760.                         (setf (aref array (char-code (car pair)))
  761.                           (cdr pair)))
  762.                       array))
  763.              (defconstant *card8-to-char-translation-table*
  764.                       ',(let ((array (make-string 256)))
  765.                       (dotimes (i (length array))
  766.                         (setf (aref array i) (code-char i)))
  767.                       (dolist (pair alist)
  768.                         (setf (aref array (cdr pair)) (car pair)))
  769.                       array))
  770.              #-Genera
  771.              (progn
  772.                  (defun char->card8 (char)
  773.                  (declare (type base-char char))
  774.                  #.(declare-buffun)
  775.                  (the card8 (aref (the (simple-array card8 (*))
  776.                            *char-to-card8-translation-table*)
  777.                           (the array-index (char-code char)))))
  778.                (defun card8->char (card8)
  779.                  (declare (type card8 card8))
  780.                  #.(declare-buffun)
  781.                  (the base-char
  782.                   (aref (the simple-string *card8-to-char-translation-table*)
  783.                     card8)))
  784.                )
  785.              #+Genera
  786.              (progn
  787.                (defun char->card8 (char)
  788.                  (declare lt:(side-effects reader reducible))
  789.                  (aref *char-to-card8-translation-table* (char-code char)))
  790.                (defun card8->char (card8)
  791.                  (declare lt:(side-effects reader reducible))
  792.                  (aref *card8-to-char-translation-table* card8))
  793.                )
  794.              (dotimes (i 256)
  795.                (unless (= i (char->card8 (card8->char i)))
  796.                  (warn "The card8->char mapping is not invertible through char->card8.  Info:~%~S"
  797.                    (list i
  798.                      (card8->char i)
  799.                      (char->card8 (card8->char i))))
  800.                  (return nil)))
  801.              (dotimes (i (length *char-to-card8-translation-table*))
  802.                (let ((char (code-char i)))
  803.                  (unless (eql char (card8->char (char->card8 char)))
  804.                    (warn "The char->card8 mapping is not invertible through card8->char.  Info:~%~S"
  805.                      (list char
  806.                        (char->card8 char)
  807.                        (card8->char (char->card8 char))))
  808.                    (return nil))))))
  809.              (t
  810.               `(progn
  811.              (defun char->card8 (char)
  812.                (declare (type base-char char))
  813.                #.(declare-buffun)
  814.                (the card8 (char-code char)))
  815.              (defun card8->char (card8)
  816.                (declare (type card8 card8))
  817.                #.(declare-buffun)
  818.                (the base-char (code-char card8)))
  819.              ))))))
  820.   (char-translators))
  821.  
  822. ;;-----------------------------------------------------------------------------
  823. ;; Process Locking
  824. ;;
  825. ;;    Common-Lisp doesn't provide process locking primitives, so we define
  826. ;;    our own here, based on Zetalisp primitives.  Holding-Lock is very
  827. ;;    similar to with-lock on The TI Explorer, and a little more efficient
  828. ;;    than with-process-lock on a Symbolics.
  829. ;;-----------------------------------------------------------------------------
  830.  
  831. ;;; MAKE-PROCESS-LOCK: Creating a process lock.
  832.  
  833. #-(or LispM excl Minima)
  834. (defun make-process-lock (name)
  835.   (declare (ignore name))
  836.   nil)
  837.  
  838. #+excl
  839. (defun make-process-lock (name)
  840.   (mp:make-process-lock :name name))
  841.  
  842. #+(and LispM (not Genera))
  843. (defun make-process-lock (name)
  844.   (vector nil name))
  845.  
  846. #+Genera
  847. (defun make-process-lock (name)
  848.   (process:make-lock name :flavor 'clx-lock))
  849.  
  850. #+Minima
  851. (defun make-process-lock (name)
  852.   (minima:make-lock name))
  853.  
  854. ;;; HOLDING-LOCK: Execute a body of code with a lock held.
  855.  
  856. ;;; The holding-lock macro takes a timeout keyword argument.  EVENT-LISTEN
  857. ;;; passes its timeout to the holding-lock macro, so any timeout you want to
  858. ;;; work for event-listen you should do for holding-lock.
  859.  
  860. ;; If you're not sharing DISPLAY objects within a multi-processing
  861. ;; shared-memory environment, this is sufficient
  862. #-(or lispm excl lcl3.0 Minima)
  863. (defmacro holding-lock ((locator display &optional whostate &key timeout) &body body)
  864.   (declare (ignore locator display whostate timeout))
  865.   `(progn ,@body))
  866.  
  867. #+Genera
  868. (defmacro holding-lock ((locator display &optional whostate &key timeout)
  869.             &body body)
  870.   (declare (ignore whostate))
  871.   `(process:with-lock (,locator :timeout ,timeout)
  872.      (let ((.debug-io. (buffer-debug-io ,display)))
  873.        (scl:let-if .debug-io. ((*debug-io* .debug-io.))
  874.      ,@body))))
  875.  
  876. #+(and lispm (not Genera))
  877. (defmacro holding-lock ((locator display &optional whostate &key timeout)
  878.             &body body)
  879.   (declare (ignore display))
  880.   ;; This macro is for use in a multi-process environment.
  881.   (let ((lock (gensym))
  882.     (have-lock (gensym))
  883.     (timeo (gensym)))
  884.     `(let* ((,lock (zl:locf (svref ,locator 0)))
  885.         (,have-lock (eq (car ,lock) sys:current-process))
  886.         (,timeo ,timeout))
  887.        (unwind-protect 
  888.        (when (cond (,have-lock)
  889.                ((#+explorer si:%store-conditional
  890.              #-explorer sys:store-conditional
  891.              ,lock nil sys:current-process))
  892.                ((null ,timeo)
  893.             (sys:process-lock ,lock nil ,(or whostate "CLX Lock")))
  894.                ((sys:process-wait-with-timeout
  895.                 ,(or whostate "CLX Lock") (round (* ,timeo 60.))
  896.               #'(lambda (lock process)
  897.                   (#+explorer si:%store-conditional
  898.                    #-explorer sys:store-conditional
  899.                    lock nil process))
  900.               ,lock sys:current-process)))
  901.          ,@body)
  902.      (unless ,have-lock
  903.        (#+explorer si:%store-conditional
  904.         #-explorer sys:store-conditional
  905.         ,lock sys:current-process nil))))))
  906.  
  907. ;; Lucid has a process locking mechanism as well under release 3.0
  908. #+lcl3.0
  909. (defmacro holding-lock ((locator display &optional whostate &key timeout)
  910.             &body body)
  911.   (declare (ignore display))
  912.   (if timeout
  913.       ;; Hair to support timeout.
  914.       `(let ((.have-lock. (eq ,locator lcl:*current-process*))
  915.          (.timeout. ,timeout))
  916.      (unwind-protect
  917.          (when (cond (.have-lock.)
  918.              ((conditional-store ,locator nil lcl:*current-process*))
  919.              ((null .timeout.)
  920.               (lcl:process-lock ,locator)
  921.               t)
  922.              ((lcl:process-wait-with-timeout ,whostate .timeout.
  923.                 #'(lambda ()
  924.                 (conditional-store ,locator nil lcl:*current-process*))))
  925.              ;; abort the PROCESS-UNLOCK if actually timing out
  926.              (t
  927.               (setf .have-lock. :abort)
  928.               nil))
  929.            ,@body)
  930.        (unless .have-lock. 
  931.          (lcl:process-unlock ,locator))))
  932.     `(lcl:with-process-lock (,locator)
  933.        ,@body)))
  934.  
  935.  
  936. #+excl
  937. (defmacro holding-lock ((locator display &optional whostate &key timeout)
  938.             &body body)
  939.   (declare (ignore display))
  940.   `(let (.hl-lock. .hl-obtained-lock. .hl-curproc.)
  941.      (unwind-protect
  942.      (block .hl-doit.
  943.        (when mp::*scheduler-stack-group* ; fast test for scheduler running
  944.          (setq .hl-lock. ,locator
  945.            .hl-curproc. mp::*current-process*)
  946.          (when (and .hl-curproc.    ; nil if in process-wait fun
  947.             (not (eq (mp::process-lock-locker .hl-lock.)
  948.                  .hl-curproc.)))
  949.            ;; Then we need to grab the lock.
  950.            ,(if timeout
  951.             `(if (not (mp::process-lock .hl-lock. .hl-curproc.
  952.                         ,whostate ,timeout))
  953.              (return-from .hl-doit. nil))
  954.           `(mp::process-lock .hl-lock. .hl-curproc.
  955.                      ,@(when whostate `(,whostate))))
  956.            ;; There is an apparent race condition here.  However, there is
  957.            ;; no actual race condition -- our implementation of mp:process-
  958.            ;; lock guarantees that the lock will still be held when it
  959.            ;; returns, and no interrupt can happen between that and the
  960.            ;; execution of the next form.  -- jdi 2/27/91
  961.            (setq .hl-obtained-lock. t)))
  962.        ,@body)
  963.        (if (and .hl-obtained-lock.
  964.         ;; Note -- next form added to allow error handler inside
  965.         ;; body to unlock the lock prematurely if it knows that
  966.         ;; the current process cannot possibly continue but will
  967.         ;; throw out (or is it throw up?).
  968.         (eq (mp::process-lock-locker .hl-lock.) .hl-curproc.))
  969.        (mp::process-unlock .hl-lock. .hl-curproc.)))))
  970.  
  971. #+Minima
  972. (defmacro holding-lock ((locator display &optional whostate &key timeout) &body body)
  973.   `(holding-lock-1 #'(lambda () ,@body) ,locator ,display
  974.            ,@(and whostate `(:whostate ,whostate))
  975.            ,@(and timeout `(:timeout ,timeout))))
  976.  
  977. #+Minima
  978. (defun holding-lock-1 (continuation lock display &key (whostate "Lock") timeout)
  979.   (declare (dynamic-extent continuation))
  980.   (declare (ignore display whostate timeout))
  981.   (minima:with-lock (lock)
  982.     (funcall continuation)))
  983.  
  984. ;;; WITHOUT-ABORTS
  985.  
  986. ;;; If you can inhibit asynchronous keyboard aborts inside the body of this
  987. ;;; macro, then it is a good idea to do this.  This macro is wrapped around
  988. ;;; request writing and reply reading to ensure that requests are atomically
  989. ;;; written and replies are atomically read from the stream.
  990.  
  991. #-(or Genera excl lcl3.0)
  992. (defmacro without-aborts (&body body)
  993.   `(progn ,@body))
  994.  
  995. #+Genera
  996. (defmacro without-aborts (&body body)
  997.   `(sys:without-aborts (clx "CLX is in the middle of an operation that should be atomic.")
  998.      ,@body))
  999.  
  1000. #+excl
  1001. (defmacro without-aborts (&body body)
  1002.   `(without-interrupts ,@body))
  1003.     
  1004. #+lcl3.0
  1005. (defmacro without-aborts (&body body)
  1006.   `(lcl:with-interruptions-inhibited ,@body))
  1007.  
  1008. ;;; PROCESS-BLOCK: Wait until a given predicate returns a non-NIL value.
  1009. ;;; Caller guarantees that PROCESS-WAKEUP will be called after the predicate's
  1010. ;;; value changes.
  1011.  
  1012. #-(or lispm excl lcl3.0 Minima)
  1013. (defun process-block (whostate predicate &rest predicate-args)
  1014.   (declare (ignore whostate))
  1015.   (or (apply predicate predicate-args)
  1016.       (error "Program tried to wait with no scheduler.")))
  1017.  
  1018. #+Genera
  1019. (defun process-block (whostate predicate &rest predicate-args)
  1020.   (declare (type function predicate)
  1021.        #+clx-ansi-common-lisp
  1022.        (dynamic-extent predicate)
  1023.        #-clx-ansi-common-lisp
  1024.        (sys:downward-funarg predicate))
  1025.   (apply #'process:block-process whostate predicate predicate-args))
  1026.  
  1027. #+(and lispm (not Genera))
  1028. (defun process-block (whostate predicate &rest predicate-args)
  1029.   (declare (type function predicate)
  1030.        #+clx-ansi-common-lisp
  1031.        (dynamic-extent predicate)
  1032.        #-clx-ansi-common-lisp 
  1033.        (sys:downward-funarg predicate))
  1034.   (apply #'global:process-wait whostate predicate predicate-args))
  1035.  
  1036. #+excl
  1037. (defun process-block (whostate predicate &rest predicate-args)
  1038.   (if mp::*scheduler-stack-group*
  1039.       (apply #'mp::process-wait whostate predicate predicate-args)
  1040.       (or (apply predicate predicate-args)
  1041.       (error "Program tried to wait with no scheduler."))))
  1042.  
  1043. #+lcl3.0
  1044. (defun process-block (whostate predicate &rest predicate-args)
  1045.   (declare (dynamic-extent predicate-args))
  1046.   (apply #'lcl:process-wait whostate predicate predicate-args))
  1047.  
  1048. #+Minima
  1049. (defun process-block (whostate predicate &rest predicate-args)
  1050.   (declare (type function predicate)
  1051.        (dynamic-extent predicate))
  1052.   (apply #'minima:process-wait whostate predicate predicate-args))
  1053.  
  1054. ;;; PROCESS-WAKEUP: Check some other process' wait function.
  1055.  
  1056. (declaim (inline process-wakeup))
  1057.  
  1058. #-(or excl Genera Minima)
  1059. (defun process-wakeup (process)
  1060.   (declare (ignore process))
  1061.   nil)
  1062.  
  1063. #+excl
  1064. (defun process-wakeup (process)
  1065.   (let ((curproc mp::*current-process*))
  1066.     (when (and curproc process)
  1067.       (unless (mp::process-p curproc)
  1068.     (error "~s is not a process" curproc))
  1069.       (unless (mp::process-p process)
  1070.     (error "~s is not a process" process))
  1071.       (if (> (mp::process-priority process) (mp::process-priority curproc))
  1072.       (mp::process-allow-schedule process)))))
  1073.  
  1074. #+Genera
  1075. (defun process-wakeup (process)
  1076.   (process:wakeup process))
  1077.  
  1078. #+Minima
  1079. (defun process-wakeup (process)
  1080.   (minima:process-wakeup process))
  1081.  
  1082. ;;; CURRENT-PROCESS: Return the current process object for input locking and
  1083. ;;; for calling PROCESS-WAKEUP.
  1084.  
  1085. (declaim (inline current-process))
  1086.  
  1087. ;;; Default return NIL, which is acceptable even if there is a scheduler.
  1088.  
  1089. #-(or lispm excl lcl3.0 Minima)
  1090. (defun current-process ()
  1091.   nil)
  1092.  
  1093. #+lispm
  1094. (defun current-process ()
  1095.   sys:current-process)
  1096.  
  1097. #+excl
  1098. (defun current-process ()
  1099.   (and mp::*scheduler-stack-group*
  1100.        mp::*current-process*))
  1101.  
  1102. #+lcl3.0
  1103. (defun current-process ()
  1104.   lcl:*current-process*)
  1105.  
  1106. #+Minima
  1107. (defun current-process ()
  1108.   (minima:current-process))
  1109.  
  1110. ;;; WITHOUT-INTERRUPTS -- provide for atomic operations.
  1111.  
  1112. #-(or lispm excl lcl3.0 Minima)
  1113. (defmacro without-interrupts (&body body)
  1114.   `(progn ,@body))
  1115.  
  1116. #+(and lispm (not Genera))
  1117. (defmacro without-interrupts (&body body)
  1118.   `(sys:without-interrupts ,@body))
  1119.  
  1120. #+Genera
  1121. (defmacro without-interrupts (&body body)
  1122.   `(process:with-no-other-processes ,@body))
  1123.  
  1124. #+LCL3.0
  1125. (defmacro without-interrupts (&body body)
  1126.   `(lcl:with-scheduling-inhibited ,@body))
  1127.  
  1128. #+Minima
  1129. (defmacro without-interrupts (&body body)
  1130.   `(minima:with-no-other-processes ,@body))
  1131.  
  1132. ;;; CONDITIONAL-STORE:
  1133.  
  1134. ;; This should use GET-SETF-METHOD to avoid evaluating subforms multiple times.
  1135. ;; It doesn't because CLtL doesn't pass the environment to GET-SETF-METHOD.
  1136. (defmacro conditional-store (place old-value new-value)
  1137.   `(without-interrupts
  1138.      (cond ((eq ,place ,old-value)
  1139.         (setf ,place ,new-value)
  1140.         t))))
  1141.  
  1142. ;;;----------------------------------------------------------------------------
  1143. ;;; IO Error Recovery
  1144. ;;;    All I/O operations are done within a WRAP-BUF-OUTPUT macro.
  1145. ;;;    It prevents multiple mindless errors when the network craters.
  1146. ;;;
  1147. ;;;----------------------------------------------------------------------------
  1148.  
  1149. #-Genera
  1150. (defmacro wrap-buf-output ((buffer) &body body)
  1151.   ;; Error recovery wrapper
  1152.   `(unless (buffer-dead ,buffer)
  1153.      ,@body))
  1154.  
  1155. #+Genera
  1156. (defmacro wrap-buf-output ((buffer) &body body)
  1157.   ;; Error recovery wrapper
  1158.   `(let ((.buffer. ,buffer))
  1159.      (unless (buffer-dead .buffer.)
  1160.        (scl:condition-bind
  1161.      (((sys:network-error)
  1162.        #'(lambda (error)
  1163.            (scl:condition-case () 
  1164.             (funcall (buffer-close-function .buffer.) .buffer. :abort t)
  1165.           (sys:network-error))
  1166.            (setf (buffer-dead .buffer.) error)
  1167.            (setf (buffer-output-stream .buffer.) nil)
  1168.            (setf (buffer-input-stream .buffer.) nil)
  1169.            nil)))
  1170.      ,@body))))
  1171.  
  1172. #-Genera
  1173. (defmacro wrap-buf-input ((buffer) &body body)
  1174.   (declare (ignore buffer))
  1175.   ;; Error recovery wrapper
  1176.   `(progn ,@body))
  1177.  
  1178. #+Genera
  1179. (defmacro wrap-buf-input ((buffer) &body body)
  1180.   ;; Error recovery wrapper
  1181.   `(let ((.buffer. ,buffer))
  1182.      (scl:condition-bind
  1183.        (((sys:network-error)
  1184.      #'(lambda (error)
  1185.          (scl:condition-case () 
  1186.           (funcall (buffer-close-function .buffer.) .buffer. :abort t)
  1187.         (sys:network-error))
  1188.          (setf (buffer-dead .buffer.) error)
  1189.          (setf (buffer-output-stream .buffer.) nil)
  1190.          (setf (buffer-input-stream .buffer.) nil)
  1191.          nil)))
  1192.        ,@body)))
  1193.  
  1194.  
  1195. ;;;----------------------------------------------------------------------------
  1196. ;;; System dependent IO primitives
  1197. ;;;    Functions for opening, reading writing forcing-output and closing 
  1198. ;;;    the stream to the server.
  1199. ;;;----------------------------------------------------------------------------
  1200.  
  1201. ;;; OPEN-X-STREAM - create a stream for communicating to the appropriate X
  1202. ;;; server
  1203.  
  1204. #-(or explorer Genera lucid kcl ibcl excl Minima)
  1205. (defun open-x-stream (host display protocol)
  1206.   host display protocol ;; unused
  1207.   (error "OPEN-X-STREAM not implemented yet."))
  1208.  
  1209. ;;; Genera:
  1210.  
  1211. ;;; TCP and DNA are both layered products, so try to work with either one.
  1212.  
  1213. #+Genera
  1214. (when (fboundp 'tcp:add-tcp-port-for-protocol)
  1215.   (tcp:add-tcp-port-for-protocol :x-window-system 6000))
  1216.  
  1217. #+Genera
  1218. (when (fboundp 'dna:add-dna-contact-id-for-protocol)
  1219.   (dna:add-dna-contact-id-for-protocol :x-window-system "X$X0"))
  1220.  
  1221. #+Genera
  1222. (net:define-protocol :x-window-system (:x-window-system :byte-stream)
  1223.   (:invoke-with-stream ((stream :characters nil :ascii-translation nil))
  1224.     stream))
  1225.  
  1226. #+Genera
  1227. (eval-when (compile)
  1228.   (compiler:function-defined 'tcp:open-tcp-stream)
  1229.   (compiler:function-defined 'dna:open-dna-bidirectional-stream))
  1230.  
  1231. #+Genera
  1232. (defun open-x-stream (host display protocol)
  1233.   (let ((host (net:parse-host host)))
  1234.     (if (or protocol (plusp display))
  1235.     ;; The protocol was specified or the display isn't 0, so we
  1236.     ;; can't use the Generic Network System.  If the protocol was
  1237.     ;; specified, then use that protocol, otherwise, blindly use
  1238.     ;; TCP.
  1239.     (ccase protocol
  1240.       ((:tcp nil)
  1241.        (tcp:open-tcp-stream
  1242.          host (+ *x-tcp-port* display) nil
  1243.          :direction :io
  1244.          :characters nil
  1245.          :ascii-translation nil))
  1246.       ((:dna)
  1247.        (dna:open-dna-bidirectional-stream
  1248.          host (format nil "X$X~D" display)
  1249.          :characters nil
  1250.          :ascii-translation nil)))
  1251.       (let ((neti:*invoke-service-automatic-retry* t))
  1252.     (net:invoke-service-on-host :x-window-system host)))))
  1253.  
  1254. #+explorer
  1255. (defun open-x-stream (host display protocol)
  1256.   (declare (ignore protocol))
  1257.   (net:open-connection-on-medium
  1258.     (net:parse-host host)            ;Host
  1259.     :byte-stream                ;Medium
  1260.     "X11"                    ;Logical contact name
  1261.     :stream-type :character-stream
  1262.     :direction :bidirectional
  1263.     :timeout-after-open nil
  1264.     :remote-port (+ *x-tcp-port* display)))
  1265.  
  1266. #+explorer
  1267. (net:define-logical-contact-name
  1268.   "X11"
  1269.   `((:local "X11")
  1270.     (:chaos "X11")
  1271.     (:nsp-stream "X11")
  1272.     (:tcp ,*x-tcp-port*)))
  1273.  
  1274. #+lucid
  1275. (defun open-x-stream (host display protocol)
  1276.   protocol ;; unused
  1277.   (let ((fd (connect-to-server host display)))
  1278.     (when (minusp fd)
  1279.       (error "Failed to connect to server: ~A ~D" host display))
  1280.     (user::make-lisp-stream :input-handle fd
  1281.                 :output-handle fd
  1282.                 :element-type 'unsigned-byte
  1283.                 #-lcl3.0 :stream-type #-lcl3.0 :ephemeral)))
  1284.  
  1285. #+(or kcl ibcl)
  1286. (defun open-x-stream (host display protocol)
  1287.   protocol ;; unused
  1288.   (let ((stream (open-socket-stream host display)))
  1289.     (if (streamp stream)
  1290.     stream
  1291.       (error "Cannot connect to server: ~A:~D" host display))))
  1292.  
  1293. #+excl
  1294. ;;
  1295. ;; Note that since we don't use the CL i/o facilities to do i/o, the display
  1296. ;; input and output "stream" is really a file descriptor (fixnum).
  1297. ;;
  1298. (defun open-x-stream (host display protocol)
  1299.   (declare (ignore protocol));; unused
  1300.   (let ((fd (connect-to-server (string host) display)))
  1301.     (when (minusp fd)
  1302.       (error "Failed to connect to server: ~A ~D" host display))
  1303.     fd))
  1304.  
  1305. #+Minima
  1306. (defun open-x-stream (host display protocol)
  1307.   (declare (ignore protocol));; unused
  1308.   (minima:open-tcp-stream (minima:gensym-tcp-port)
  1309.               (apply #'minima:make-ip-address (cdr (host-address host)))
  1310.               (+ *x-tcp-port* display) :element-type '(unsigned-byte 8)))
  1311.  
  1312. ;;; BUFFER-READ-DEFAULT - read data from the X stream
  1313.  
  1314. #+(or Genera explorer)
  1315. (defun buffer-read-default (display vector start end timeout)
  1316.   ;; returns non-NIL if EOF encountered
  1317.   ;; Returns :TIMEOUT when timeout exceeded
  1318.   (declare (type display display)
  1319.        (type buffer-bytes vector)
  1320.        (type array-index start end)
  1321.        (type (or null number) timeout))
  1322.   #.(declare-buffun)
  1323.   (let ((stream (display-input-stream display)))
  1324.     (or (cond ((null stream))
  1325.           ((funcall stream :listen) nil)
  1326.           ((eql timeout 0) :timeout)
  1327.           ((buffer-input-wait-default display timeout)))
  1328.     (multiple-value-bind (ignore eofp)
  1329.         (funcall stream :string-in nil vector start end)
  1330.       eofp))))
  1331.  
  1332.  
  1333. #+excl
  1334. ;;
  1335. ;; Rewritten 10/89 to not use foreign function interface to do I/O.
  1336. ;;
  1337. (defun buffer-read-default (display vector start end timeout)
  1338.   (declare (type display display)
  1339.        (type buffer-bytes vector)
  1340.        (type array-index start end)
  1341.        (type (or null number) timeout))
  1342.   #.(declare-buffun)
  1343.     
  1344.   (let* ((howmany (- end start))
  1345.      (fd (display-input-stream display)))
  1346.     (declare (type array-index howmany)
  1347.          (fixnum fd))
  1348.       
  1349.     (or (cond ((fd-char-avail-p fd) nil)
  1350.           ((eql timeout 0) :timeout)
  1351.           ((buffer-input-wait-default display timeout)))
  1352.     (fd-read-bytes fd vector start howmany))))
  1353.  
  1354.  
  1355. #+lcl3.0
  1356. (defmacro with-underlying-stream ((variable stream display direction) &body body)
  1357.   `(let ((,variable
  1358.       (or (getf (display-plist ,display) ',direction)
  1359.           (setf (getf (display-plist ,display) ',direction)
  1360.             (lucid::underlying-stream
  1361.               ,stream ,(if (eq direction 'input) :input :output))))))
  1362.      ,@body))
  1363.  
  1364. #+lcl3.0
  1365. (defun buffer-read-default (display vector start end timeout)
  1366.   ;;Note that LISTEN must still be done on "slow stream" or the I/O system
  1367.   ;;gets confused.  But reading should be done from "fast stream" for speed.
  1368.   ;;We used to inhibit scheduling because there were races in Lucid's
  1369.   ;;multitasking system.  Empirical evidence suggests they may be gone now.
  1370.   ;;Should you decide you need to inhibit scheduling, do it around the
  1371.   ;;lcl:read-array.
  1372.   (declare (type display display)
  1373.        (type buffer-bytes vector)
  1374.        (type array-index start end)
  1375.        (type (or null number) timeout))
  1376.   #.(declare-buffun)
  1377.   (let ((stream (display-input-stream display)))
  1378.     (declare (type (or null stream) stream))
  1379.     (or (cond ((null stream))
  1380.           ((listen stream) nil)
  1381.           ((eql timeout 0) :timeout)
  1382.           ((buffer-input-wait-default display timeout)))
  1383.     (with-underlying-stream (stream stream display input)
  1384.       (eq (lcl:read-array stream vector start end nil :eof) :eof)))))
  1385.  
  1386. #+Minima
  1387. (defun buffer-read-default (display vector start end timeout)
  1388.   ;; returns non-NIL if EOF encountered
  1389.   ;; Returns :TIMEOUT when timeout exceeded
  1390.   (declare (type display display)
  1391.        (type buffer-bytes vector)
  1392.        (type array-index start end)
  1393.        (type (or null number) timeout))
  1394.   #.(declare-buffun)
  1395.   (let ((stream (display-input-stream display)))
  1396.     (or (cond ((null stream))
  1397.           ((listen stream) nil)
  1398.           ((eql timeout 0) :timeout)
  1399.           ((buffer-input-wait-default display timeout)))
  1400.     (loop while (< start end) do
  1401.       (multiple-value-bind (buffer bstart bend)
  1402.           (minima:get-input-buffer stream nil)
  1403.         (when (null buffer) (return t))
  1404.         (let ((n (min (- end start) (- bend bstart))))
  1405.           (replace vector buffer
  1406.                :start1 start :end1 (incf start n)
  1407.                :start2 bstart :end2 (incf bstart n)))
  1408.         (minima:advance-input-buffer stream bstart)))
  1409.     nil)))
  1410.  
  1411. ;;; WARNING:
  1412. ;;;    CLX performance will suffer if your lisp uses read-byte for
  1413. ;;;    receiving all data from the X Window System server.
  1414. ;;;    You are encouraged to write a specialized version of
  1415. ;;;    buffer-read-default that does block transfers.
  1416. #-(or Genera explorer excl lcl3.0 Minima)
  1417. (defun buffer-read-default (display vector start end timeout)
  1418.   (declare (type display display)
  1419.        (type buffer-bytes vector)
  1420.        (type array-index start end)
  1421.        (type (or null (rational 0 *) (float 0.0 *)) timeout))
  1422.   #.(declare-buffun)
  1423.   (let ((stream (display-input-stream display)))
  1424.     (declare (type (or null stream) stream))
  1425.     (or (cond ((null stream))
  1426.           ((listen stream) nil)
  1427.           ((eql timeout 0) :timeout)
  1428.           ((buffer-input-wait-default display timeout)))
  1429.     (do* ((index start (index1+ index)))
  1430.          ((index>= index end) nil)
  1431.       (declare (type array-index index))
  1432.       (let ((c (read-byte stream nil nil)))
  1433.         (declare (type (or null card8) c))
  1434.         (if (null c)
  1435.         (return t)
  1436.           (setf (aref vector index) (the card8 c))))))))
  1437.  
  1438. ;;; BUFFER-WRITE-DEFAULT - write data to the X stream
  1439.  
  1440. #+(or Genera explorer)
  1441. (defun buffer-write-default (vector display start end)
  1442.   ;; The default buffer write function for use with common-lisp streams
  1443.   (declare (type buffer-bytes vector)
  1444.        (type display display)
  1445.        (type array-index start end))
  1446.   #.(declare-buffun)
  1447.   (let ((stream (display-output-stream display)))
  1448.     (declare (type (or null stream) stream))
  1449.     (unless (null stream) 
  1450.       (write-string vector stream :start start :end end))))
  1451.  
  1452. #+excl
  1453. (defun buffer-write-default (vector display start end)
  1454.   (declare (type buffer-bytes vector)
  1455.        (type display display)
  1456.        (type array-index start end))
  1457.   #.(declare-buffun)
  1458.   (excl::filesys-write-bytes (display-output-stream display) vector start
  1459.                  (- end start)))
  1460.   
  1461. #+lcl3.0
  1462. (defun buffer-write-default (vector display start end)
  1463.   ;;We used to inhibit scheduling because there were races in Lucid's
  1464.   ;;multitasking system.  Empirical evidence suggests they may be gone now.
  1465.   ;;Should you decide you need to inhibit scheduling, do it around the
  1466.   ;;lcl:write-array.
  1467.   (declare (type display display)
  1468.        (type buffer-bytes vector)
  1469.        (type array-index start end))
  1470.   #.(declare-buffun)
  1471.   (let ((stream (display-output-stream display)))
  1472.     (declare (type (or null stream) stream))
  1473.     (unless (null stream) 
  1474.       (with-underlying-stream (stream stream display output)
  1475.     (lcl:write-array stream vector start end)))))
  1476.  
  1477. #+Minima
  1478. (defun buffer-write-default (vector display start end)
  1479.   ;; The default buffer write function for use with common-lisp streams
  1480.   (declare (type buffer-bytes vector)
  1481.        (type display display)
  1482.        (type array-index start end))
  1483.   #.(declare-buffun)
  1484.   (let ((stream (display-output-stream display)))
  1485.     (declare (type (or null stream) stream))
  1486.     (unless (null stream) 
  1487.       (loop while (< start end) do
  1488.     (multiple-value-bind (buffer bstart bend)
  1489.         (minima:get-output-buffer stream)
  1490.       (let ((n (min (- end start) (- bend bstart))))
  1491.         (replace buffer vector
  1492.              :start1 bstart :end1 (incf bstart n) :start2 start :end2 (incf start n) ))
  1493.         (minima:advance-output-buffer stream bstart))))))
  1494.  
  1495. ;;; WARNING:
  1496. ;;;    CLX performance will be severely degraded if your lisp uses
  1497. ;;;    write-byte to send all data to the X Window System server.
  1498. ;;;    You are STRONGLY encouraged to write a specialized version
  1499. ;;;    of buffer-write-default that does block transfers.
  1500.  
  1501. #-(or Genera explorer excl lcl3.0 Minima)
  1502. (defun buffer-write-default (vector display start end)
  1503.   ;; The default buffer write function for use with common-lisp streams
  1504.   (declare (type buffer-bytes vector)
  1505.        (type display display)
  1506.        (type array-index start end))
  1507.   #.(declare-buffun)
  1508.   (let ((stream (display-output-stream display)))
  1509.     (declare (type (or null stream) stream))
  1510.     (unless (null stream)
  1511.       (with-vector (vector buffer-bytes)
  1512.     (do ((index start (index1+ index)))
  1513.         ((index>= index end))
  1514.       (declare (type array-index index))
  1515.       (write-byte (aref vector index) stream))))))
  1516.  
  1517. ;;; buffer-force-output-default - force output to the X stream
  1518.  
  1519. #+excl
  1520. (defun buffer-force-output-default (display)
  1521.   ;; buffer-write-default does the actual writing.
  1522.   (declare (ignore display)))
  1523.  
  1524. #-excl
  1525. (defun buffer-force-output-default (display)
  1526.   ;; The default buffer force-output function for use with common-lisp streams
  1527.   (declare (type display display))
  1528.   (let ((stream (display-output-stream display)))
  1529.     (declare (type (or null stream) stream))
  1530.     (unless (null stream)
  1531.       (force-output stream))))
  1532.  
  1533. ;;; BUFFER-CLOSE-DEFAULT - close the X stream
  1534.  
  1535. #+excl
  1536. (defun buffer-close-default (display &key abort)
  1537.   ;; The default buffer close function for use with common-lisp streams
  1538.   (declare (type display display)
  1539.        (ignore abort))
  1540.   #.(declare-buffun)
  1541.   (excl::filesys-checking-close (display-output-stream display)))
  1542.  
  1543. #-excl
  1544. (defun buffer-close-default (display &key abort)
  1545.   ;; The default buffer close function for use with common-lisp streams
  1546.   (declare (type display display))
  1547.   #.(declare-buffun)
  1548.   (let ((stream (display-output-stream display)))
  1549.     (declare (type (or null stream) stream))
  1550.     (unless (null stream)
  1551.       (close stream :abort abort))))
  1552.  
  1553. ;;; BUFFER-INPUT-WAIT-DEFAULT - wait for for input to be available for the
  1554. ;;; buffer.  This is called in read-input between requests, so that a process
  1555. ;;; waiting for input is abortable when between requests.  Should return
  1556. ;;; :TIMEOUT if it times out, NIL otherwise.
  1557.  
  1558. ;;; The default implementation
  1559.  
  1560. ;; Poll for input every *buffer-read-polling-time* SECONDS.
  1561. #-(or Genera explorer excl lcl3.0)
  1562. (defparameter *buffer-read-polling-time* 0.5)
  1563.  
  1564. #-(or Genera explorer excl lcl3.0)
  1565. (defun buffer-input-wait-default (display timeout)
  1566.   (declare (type display display)
  1567.        (type (or null number) timeout))
  1568.   (declare (values timeout))
  1569.   
  1570.   (let ((stream (display-input-stream display)))
  1571.     (declare (type (or null stream) stream))
  1572.     (cond ((null stream))
  1573.       ((listen stream) nil)
  1574.       ((eql timeout 0) :timeout)
  1575.       ((not (null timeout))
  1576.        (multiple-value-bind (npoll fraction)
  1577.            (truncate timeout *buffer-read-polling-time*)
  1578.          (dotimes (i npoll)            ; Sleep for a time, then listen again
  1579.            (sleep *buffer-read-polling-time*)
  1580.            (when (listen stream)
  1581.          (return-from buffer-input-wait-default nil)))
  1582.          (when (plusp fraction)
  1583.            (sleep fraction)            ; Sleep a fraction of a second
  1584.            (when (listen stream)        ; and listen one last time
  1585.          (return-from buffer-input-wait-default nil)))
  1586.          :timeout)))))
  1587.  
  1588. #+Genera
  1589. (defun buffer-input-wait-default (display timeout)
  1590.   (declare (type display display)
  1591.        (type (or null number) timeout))
  1592.   (declare (values timeout))
  1593.   (let ((stream (display-input-stream display)))
  1594.     (declare (type (or null stream) stream))
  1595.     (cond ((null stream))
  1596.       ((scl:send stream :listen) nil)
  1597.       ((eql timeout 0) :timeout)
  1598.       ((null timeout) (si:stream-input-block stream "CLX Input"))
  1599.       (t
  1600.        (scl:condition-bind ((neti:protocol-timeout
  1601.                   #'(lambda (error)
  1602.                       (when (eq stream (scl:send error :stream))
  1603.                     (return-from buffer-input-wait-default :timeout)))))
  1604.          (neti:with-stream-timeout (stream :input timeout)
  1605.            (si:stream-input-block stream "CLX Input")))))
  1606.     nil))
  1607.  
  1608. #+explorer
  1609. (defun buffer-input-wait-default (display timeout)
  1610.   (declare (type display display)
  1611.        (type (or null number) timeout))
  1612.   (declare (values timeout))
  1613.   (let ((stream (display-input-stream display)))
  1614.     (declare (type (or null stream) stream))
  1615.     (cond ((null stream))
  1616.       ((zl:send stream :listen) nil)
  1617.       ((eql timeout 0) :timeout)
  1618.       ((null timeout)
  1619.        (si:process-wait "CLX Input" stream :listen))
  1620.       (t
  1621.        (unless (si:process-wait-with-timeout
  1622.                "CLX Input" (round (* timeout 60.)) stream :listen)
  1623.          (return-from buffer-input-wait-default :timeout))))
  1624.     nil))
  1625.  
  1626. #+excl
  1627. ;;
  1628. ;; This is used so an 'eq' test may be used to find out whether or not we can
  1629. ;; safely throw this process out of the CLX read loop.
  1630. ;;
  1631. (defparameter *read-whostate* "waiting for input from X server")
  1632.  
  1633. ;;
  1634. ;; Note that this function returns nil on error if the scheduler is running,
  1635. ;; t on error if not.  This is ok since buffer-read will detect the error.
  1636. ;;
  1637. #+excl
  1638. (defun buffer-input-wait-default (display timeout)
  1639.   (declare (type display display)
  1640.        (type (or null number) timeout))
  1641.   (declare (values timeout))
  1642.   (let ((fd (display-input-stream display)))
  1643.     (declare (fixnum fd))
  1644.     (when (>= fd 0)
  1645.       (cond ((fd-char-avail-p fd)
  1646.          nil)
  1647.         
  1648.         ;; Otherwise no bytes were available on the socket
  1649.         ((and timeout (zerop timeout))
  1650.          ;; If there aren't enough and timeout == 0, timeout.
  1651.          :timeout)
  1652.       
  1653.         ;; If the scheduler is running let it do timeouts.
  1654.         (mp::*scheduler-stack-group*
  1655.          #+allegro
  1656.          (if (not
  1657.           (mp:wait-for-input-available fd :whostate *read-whostate*
  1658.                            :wait-function #'fd-char-avail-p
  1659.                            :timeout timeout))
  1660.          (return-from buffer-input-wait-default :timeout))
  1661.          #-allegro
  1662.          (mp::wait-for-input-available fd :whostate *read-whostate*
  1663.                        :wait-function #'fd-char-avail-p))
  1664.         
  1665.         ;; Otherwise we have to handle timeouts by hand, and call select()
  1666.         ;; to block until input is available.  Note we don't really handle
  1667.         ;; the interaction of interrupts and (numberp timeout) here.  XX
  1668.         (t
  1669.          (let ((res 0))
  1670.            (declare (fixnum res))
  1671.            (with-interrupt-checking-on
  1672.         (loop
  1673.           (setq res (fd-wait-for-input fd (if (null timeout) 0
  1674.                             (truncate timeout))))
  1675.           (cond ((plusp res)    ; success
  1676.              (return nil))
  1677.             ((eq res 0)    ; timeout
  1678.              (return :timeout))
  1679.             ((eq res -1)    ; error
  1680.              (return t))
  1681.             ;; Otherwise we got an interrupt -- go around again.
  1682.             )))))))))
  1683.  
  1684.        
  1685. #+lcl3.0
  1686. (defun buffer-input-wait-default (display timeout)
  1687.   (declare (type display display)
  1688.        (type (or null number) timeout)
  1689.        (values timeout))
  1690.   #.(declare-buffun)
  1691.   (let ((stream (display-input-stream display)))
  1692.     (declare (type (or null stream) stream))
  1693.     (cond ((null stream))
  1694.       ((listen stream) nil)
  1695.       ((eql timeout 0) :timeout)
  1696.       ((with-underlying-stream (stream stream display input)
  1697.          (lucid::waiting-for-input-from-stream stream
  1698.                (lucid::with-io-unlocked
  1699.          (if (null timeout)
  1700.              (lcl:process-wait "CLX Input" #'listen stream)
  1701.            (lcl:process-wait-with-timeout
  1702.              "CLX Input" timeout #'listen stream)))))
  1703.        nil)
  1704.       (:timeout))))
  1705.  
  1706.  
  1707. ;;; BUFFER-LISTEN-DEFAULT - returns T if there is input available for the
  1708. ;;; buffer. This should never block, so it can be called from the scheduler.
  1709.  
  1710. ;;; The default implementation is to just use listen.
  1711. #-excl
  1712. (defun buffer-listen-default (display)
  1713.   (declare (type display display))
  1714.   (let ((stream (display-input-stream display)))
  1715.     (declare (type (or null stream) stream))
  1716.     (if (null stream)
  1717.     t
  1718.       (listen stream))))
  1719.  
  1720. #+excl 
  1721. (defun buffer-listen-default (display)
  1722.   (declare (type display display))
  1723.   (let ((fd (display-input-stream display)))
  1724.     (declare (type fixnum fd))
  1725.     (if (= fd -1)
  1726.     t
  1727.       (fd-char-avail-p fd))))
  1728.  
  1729.  
  1730. ;;;----------------------------------------------------------------------------
  1731. ;;; System dependent speed hacks
  1732. ;;;----------------------------------------------------------------------------
  1733.  
  1734. ;;
  1735. ;; WITH-STACK-LIST is used by WITH-STATE as a memory saving feature.
  1736. ;; If your lisp doesn't have stack-lists, and you're worried about
  1737. ;; consing garbage, you may want to re-write this to allocate and
  1738. ;; initialize lists from a resource.
  1739. ;;
  1740. #-lispm
  1741. (defmacro with-stack-list ((var &rest elements) &body body)
  1742.   ;; SYNTAX: (WITH-STACK-LIST (var exp1 ... expN) body)
  1743.   ;; Equivalent to (LET ((var (MAPCAR #'EVAL '(exp1 ... expN)))) body)
  1744.   ;; except that the list produced by MAPCAR resides on the stack and
  1745.   ;; therefore DISAPPEARS when WITH-STACK-LIST is exited.
  1746.   `(let ((,var (list ,@elements)))
  1747.      (declare (type cons ,var)
  1748.           #+clx-ansi-common-lisp (dynamic-extent ,var))
  1749.      ,@body))
  1750.  
  1751. #-lispm
  1752. (defmacro with-stack-list* ((var &rest elements) &body body)
  1753.   ;; SYNTAX: (WITH-STACK-LIST* (var exp1 ... expN) body)
  1754.   ;; Equivalent to (LET ((var (APPLY #'LIST* (MAPCAR #'EVAL '(exp1 ... expN))))) body)
  1755.   ;; except that the list produced by MAPCAR resides on the stack and
  1756.   ;; therefore DISAPPEARS when WITH-STACK-LIST is exited.
  1757.   `(let ((,var (list* ,@elements)))
  1758.      (declare (type cons ,var)
  1759.           #+clx-ansi-common-lisp (dynamic-extent ,var))
  1760.      ,@body))
  1761.  
  1762. (declaim (inline buffer-replace))
  1763.  
  1764. #+lispm
  1765. (defun buffer-replace (buf1 buf2 start1 end1 &optional (start2 0))
  1766.   (declare (type vector buf1 buf2)
  1767.        (type array-index start1 end1 start2))
  1768.   (sys:copy-array-portion buf2 start2 (length buf2) buf1 start1 end1))
  1769.  
  1770. #+excl
  1771. (defun buffer-replace (target-sequence source-sequence target-start
  1772.                        target-end &optional (source-start 0))
  1773.   (declare (type buffer-bytes target-sequence source-sequence)
  1774.        (type array-index target-start target-end source-start)
  1775.        (optimize (speed 3) (safety 0)))
  1776.   
  1777.   (let ((source-end (length source-sequence)))
  1778.     (declare (type array-index source-end))
  1779.     
  1780.     (excl:if* (and (eq target-sequence source-sequence)
  1781.            (> target-start source-start))
  1782.        then (let ((nelts (min (- target-end target-start)
  1783.                   (- source-end source-start))))
  1784.           (do ((target-index (+ target-start nelts -1) (1- target-index))
  1785.            (source-index (+ source-start nelts -1) (1- source-index)))
  1786.           ((= target-index (1- target-start)) target-sequence)
  1787.         (declare (type array-index target-index source-index))
  1788.         
  1789.         (setf (aref target-sequence target-index)
  1790.           (aref source-sequence source-index))))
  1791.        else (do ((target-index target-start (1+ target-index))
  1792.          (source-index source-start (1+ source-index)))
  1793.         ((or (= target-index target-end) (= source-index source-end))
  1794.          target-sequence)
  1795.           (declare (type array-index target-index source-index))
  1796.  
  1797.           (setf (aref target-sequence target-index)
  1798.         (aref source-sequence source-index))))))
  1799.  
  1800. #+lucid
  1801. ;;;The compiler is *supposed* to optimize calls to replace, but in actual
  1802. ;;;fact it does not.
  1803. (defun buffer-replace (buf1 buf2 start1 end1 &optional (start2 0))
  1804.   (declare (type buffer-bytes buf1 buf2)
  1805.        (type array-index start1 end1 start2))
  1806.   #.(declare-buffun)
  1807.   (let ((end2 (lucid::%simple-8bit-vector-length buf2)))
  1808.     (declare (type array-index end2))
  1809.     (lucid::simple-8bit-vector-replace-internal
  1810.       buf1 buf2 start1 end1 start2 end2)))
  1811.  
  1812. #+(and clx-overlapping-arrays (not (or lispm excl)))
  1813. (defun buffer-replace (buf1 buf2 start1 end1 &optional (start2 0))
  1814.   (declare (type vector buf1 buf2)
  1815.        (type array-index start1 end1 start2))
  1816.   (replace buf1 buf2 :start1 start1 :end1 end1 :start2 start2))
  1817.  
  1818. #-(or lispm lucid excl clx-overlapping-arrays)
  1819. (defun buffer-replace (buf1 buf2 start1 end1 &optional (start2 0))
  1820.   (declare (type buffer-bytes buf1 buf2)
  1821.        (type array-index start1 end1 start2))
  1822.   (replace buf1 buf2 :start1 start1 :end1 end1 :start2 start2))
  1823.  
  1824. #+ti
  1825. (defun with-location-bindings (sys:"e bindings &rest body)
  1826.   (do ((bindings bindings (cdr bindings)))
  1827.       ((null bindings)
  1828.        (sys:eval-body-as-progn body))
  1829.     (sys:bind (sys:*eval `(sys:locf ,(caar bindings)))
  1830.           (sys:*eval (cadar bindings)))))
  1831.  
  1832. #+ti
  1833. (compiler:defoptimizer with-location-bindings with-l-b-compiler nil (form)
  1834.   (let ((bindings (cadr form))
  1835.     (body (cddr form)))
  1836.     `(let ()
  1837.        ,@(loop for (accessor value) in bindings
  1838.            collect `(si:bind (si:locf ,accessor) ,value))
  1839.        ,@body)))
  1840.  
  1841. #+ti
  1842. (defun (:property with-location-bindings compiler::cw-handler) (exp)
  1843.   (let* ((bindlist (mapcar #'compiler::cw-clause (second exp)))
  1844.      (body (compiler::cw-clause (cddr exp))))
  1845.     (and compiler::cw-return-expansion-flag
  1846.      (list* (first exp) bindlist body))))
  1847.  
  1848. #+(and lispm (not ti))
  1849. (defmacro with-location-bindings (bindings &body body)
  1850.   `(sys:letf* ,bindings ,@body))
  1851.  
  1852. #+lispm
  1853. (defmacro with-gcontext-bindings ((gc saved-state indexes ts-index temp-mask temp-gc)
  1854.                   &body body)
  1855.   ;; don't use svref on LHS because Symbolics didn't define locf for it
  1856.   (let* ((local-state (gensym))
  1857.      (bindings `(((aref ,local-state ,ts-index) 0))))    ; will become zero anyway
  1858.     (dolist (index indexes)
  1859.       (push `((aref ,local-state ,index) (svref ,saved-state ,index))
  1860.         bindings))
  1861.     `(let ((,local-state (gcontext-local-state ,gc)))
  1862.        (declare (type gcontext-state ,local-state))
  1863.        (unwind-protect
  1864.        (with-location-bindings ,bindings
  1865.          ,@body)
  1866.      (setf (svref ,local-state ,ts-index) 0)
  1867.      (when ,temp-gc
  1868.        (restore-gcontext-temp-state ,gc ,temp-mask ,temp-gc))
  1869.      (deallocate-gcontext-state ,saved-state)))))
  1870.  
  1871. #-lispm
  1872. (defmacro with-gcontext-bindings ((gc saved-state indexes ts-index temp-mask temp-gc)
  1873.                   &body body)
  1874.   (let ((local-state (gensym))
  1875.     (resets nil))
  1876.     (dolist (index indexes)
  1877.       (push `(setf (svref ,local-state ,index) (svref ,saved-state ,index))
  1878.         resets))
  1879.     `(unwind-protect
  1880.      (progn
  1881.        ,@body)
  1882.        (let ((,local-state (gcontext-local-state ,gc)))
  1883.      (declare (type gcontext-state ,local-state))
  1884.      ,@resets
  1885.      (setf (svref ,local-state ,ts-index) 0))
  1886.        (when ,temp-gc
  1887.      (restore-gcontext-temp-state ,gc ,temp-mask ,temp-gc))
  1888.        (deallocate-gcontext-state ,saved-state))))
  1889.  
  1890. ;;;----------------------------------------------------------------------------
  1891. ;;; How error detection should CLX do?
  1892. ;;; Several levels are possible:
  1893. ;;;
  1894. ;;; 1. Do the equivalent of check-type on every argument.
  1895. ;;; 
  1896. ;;; 2. Simply report TYPE-ERROR.  This eliminates overhead of all the format
  1897. ;;;    strings generated by check-type.
  1898. ;;; 
  1899. ;;; 3. Do error checking only on arguments that are likely to have errors
  1900. ;;;    (like keyword names)
  1901. ;;; 
  1902. ;;; 4. Do error checking only where not doing so may dammage the envirnment
  1903. ;;;    on a non-tagged machine (i.e. when storing into a structure that has
  1904. ;;;    been passed in)
  1905. ;;; 
  1906. ;;; 5. No extra error detection code.  On lispm's, ASET may barf trying to
  1907. ;;;    store a non-integer into a number array. 
  1908. ;;; 
  1909. ;;; How extensive should the error checking be?  For example, if the server
  1910. ;;; expects a CARD16, is is sufficient for CLX to check for integer, or
  1911. ;;; should it also check for non-negative and less than 65536?
  1912. ;;;----------------------------------------------------------------------------
  1913.  
  1914. ;; The *TYPE-CHECK?* constant controls how much error checking is done.
  1915. ;; Possible values are:
  1916. ;;    NIL      - Don't do any error checking
  1917. ;;    t        - Do the equivalent of checktype on every argument
  1918. ;;    :minimal - Do error checking only where errors are likely
  1919.  
  1920. ;;; This controls macro expansion, and isn't changable at run-time You will
  1921. ;;; probably want to set this to nil if you want good performance at
  1922. ;;; production time.
  1923. (defconstant *type-check?* #+(or Genera Minima) nil #-(or Genera Minima) t)
  1924.  
  1925. ;; TYPE? is used to allow the code to do error checking at a different level from
  1926. ;; the declarations.  It also does some optimizations for systems that don't have
  1927. ;; good compiler support for TYPEP.  The definitions for CARD32, CARD16, INT16, etc.
  1928. ;; include range checks.  You can modify TYPE? to do less extensive checking
  1929. ;; for these types if you desire.
  1930.  
  1931. (defmacro type? (object type)
  1932.   (if (not (constantp type))
  1933.       `(typep ,object ,type)
  1934.     (progn
  1935.       (setq type (eval type))
  1936.       #+(or Genera explorer Minima)
  1937.       (if *type-check?*
  1938.       `(locally (declare (optimize safety)) (typep ,object ',type))
  1939.     `(typep ,object ',type))
  1940.       #-(or Genera explorer Minima)
  1941.       (let ((predicate (assoc type
  1942.                   '((drawable drawable-p) (window window-p)
  1943.                 (pixmap pixmap-p) (cursor cursor-p)
  1944.                 (font font-p) (gcontext gcontext-p)
  1945.                 (colormap colormap-p) (null null)
  1946.                 (integer integerp)))))
  1947.     (cond (predicate
  1948.            `(,(second predicate) ,object))
  1949.           ((eq type 'boolean)
  1950.            't)            ; Everything is a boolean.
  1951.           (*type-check?*
  1952.            `(locally (declare (optimize safety)) (typep ,object ',type)))
  1953.           (t
  1954.            `(typep ,object ',type)))))))
  1955.  
  1956. ;; X-TYPE-ERROR is the function called for type errors.
  1957. ;; If you want lots of checking, but are concerned about code size,
  1958. ;; this can be made into a macro that ignores some parameters.
  1959.  
  1960. (defun x-type-error (object type &optional error-string)
  1961.   (x-error 'x-type-error
  1962.        :datum object
  1963.        :expected-type type
  1964.        :type-string error-string))
  1965.  
  1966.  
  1967. ;;-----------------------------------------------------------------------------
  1968. ;; Error handlers
  1969. ;;    Hack up KMP error signaling using zetalisp until the real thing comes 
  1970. ;;    along
  1971. ;;-----------------------------------------------------------------------------
  1972.  
  1973. (defun default-error-handler (display error-key &rest key-vals
  1974.                   &key asynchronous &allow-other-keys)
  1975.   (declare (type boolean asynchronous)
  1976.        (dynamic-extent key-vals))
  1977.   ;; The default display-error-handler.
  1978.   ;; It signals the conditions listed in the DISPLAY file.
  1979.   (if asynchronous
  1980.       (apply #'x-cerror "Ignore" error-key :display display :error-key error-key key-vals)
  1981.       (apply #'x-error error-key :display display :error-key error-key key-vals)))
  1982.  
  1983. #+(and lispm (not Genera) (not clx-ansi-common-lisp))
  1984. (defun x-error (condition &rest keyargs)
  1985.   (apply #'sys:signal condition keyargs))
  1986.  
  1987. #+(and lispm (not Genera) (not clx-ansi-common-lisp))
  1988. (defun x-cerror (proceed-format-string condition &rest keyargs)
  1989.   (sys:signal (apply #'zl:make-condition condition keyargs)
  1990.           :proceed-types proceed-format-string))
  1991.  
  1992. #+(and Genera (not clx-ansi-common-lisp))
  1993. (defun x-error (condition &rest keyargs)
  1994.   (declare (dbg:error-reporter))
  1995.   (apply #'sys:signal condition keyargs))
  1996.  
  1997. #+(and Genera (not clx-ansi-common-lisp))
  1998. (defun x-cerror (proceed-format-string condition &rest keyargs)
  1999.   (declare (dbg:error-reporter))
  2000.   (apply #'sys:signal condition :continue-format-string proceed-format-string keyargs))
  2001.  
  2002. #+(or clx-ansi-common-lisp excl lcl3.0)
  2003. (defun x-error (condition &rest keyargs)
  2004.   (declare (dynamic-extent keyargs))
  2005.   (apply #'error condition keyargs))
  2006.  
  2007. #+(or clx-ansi-common-lisp excl lcl3.0)
  2008. (defun x-cerror (proceed-format-string condition &rest keyargs)
  2009.   (declare (dynamic-extent keyargs))
  2010.   (apply #'cerror proceed-format-string condition keyargs))
  2011.  
  2012. #-(or lispm clx-ansi-common-lisp excl lcl3.0)
  2013. (defun x-error (condition &rest keyargs)
  2014.   (error "X-Error: ~a"
  2015.      (princ-to-string (apply #'make-condition condition keyargs))))
  2016.  
  2017. #-(or lispm clx-ansi-common-lisp excl lcl3.0)
  2018. (defun x-cerror (proceed-format-string condition &rest keyargs)
  2019.   (cerror proceed-format-string "X-Error: ~a"
  2020.      (princ-to-string (apply #'make-condition condition keyargs))))
  2021.  
  2022. ;; version 15 of Pitman error handling defines the syntax for define-condition to be:
  2023. ;; DEFINE-CONDITION name (parent-type) [({slot}*) {option}*]
  2024. ;; Where option is one of: (:documentation doc-string) (:conc-name symbol-or-string)
  2025. ;; or (:report exp)
  2026.  
  2027. #+lcl3.0 
  2028. (defmacro define-condition (name parent-types &optional slots &rest args)
  2029.   `(lcl:define-condition
  2030.      ,name (,(first parent-types))
  2031.      ,(mapcar #'(lambda (slot) (if (consp slot) (car slot) slot))
  2032.           slots)
  2033.      ,@args))
  2034.  
  2035. #+(and excl (not clx-ansi-common-lisp))
  2036. (defmacro define-condition (name parent-types &optional slots &rest args)
  2037.   `(excl::define-condition
  2038.      ,name (,(first parent-types))
  2039.      ,(mapcar #'(lambda (slot) (if (consp slot) (car slot) slot))
  2040.           slots)
  2041.      ,@args))
  2042.  
  2043. #+(and lispm (not clx-ansi-common-lisp))
  2044. (defmacro define-condition (name parent-types &body options)
  2045.   (let ((slot-names
  2046.       (mapcar #'(lambda (slot) (if (consp slot) (car slot) slot))
  2047.           (pop options)))
  2048.     (documentation nil)
  2049.     (conc-name (concatenate 'string (string name) "-"))           
  2050.     (reporter nil))
  2051.     (dolist (item options)
  2052.       (ecase (first item)
  2053.     (:documentation (setq documentation (second item)))
  2054.     (:conc-name (setq conc-name (string (second item))))
  2055.     (:report (setq reporter (second item)))))
  2056.     `(within-definition (,name define-condition)
  2057.        (zl:defflavor ,name ,slot-names ,parent-types
  2058.      :initable-instance-variables
  2059.      #-Genera
  2060.      (:accessor-prefix ,conc-name)
  2061.      #+Genera
  2062.      (:conc-name ,conc-name)
  2063.      #-Genera
  2064.      (:outside-accessible-instance-variables ,@slot-names)
  2065.      #+Genera
  2066.      (:readable-instance-variables ,@slot-names))
  2067.        ,(when reporter ;; when no reporter, parent's is inherited
  2068.       `(zl:defmethod #-Genera (,name :report)
  2069.                      #+Genera (dbg:report ,name) (stream)
  2070.           ,(if (stringp reporter)
  2071.            `(write-string ,reporter stream)
  2072.          `(,reporter global:self stream))
  2073.           global:self))
  2074.        (zl:compile-flavor-methods ,name)
  2075.        ,(when documentation
  2076.       `(setf (documentation name 'type) ,documentation))
  2077.        ',name)))
  2078.  
  2079. #+(and lispm (not Genera) (not clx-ansi-common-lisp))
  2080. (zl:defflavor x-error () (global:error))
  2081.  
  2082. #+(and Genera (not clx-ansi-common-lisp))
  2083. (scl:defflavor x-error
  2084.     ((dbg:proceed-types '(:continue))    ;
  2085.      continue-format-string)
  2086.     (sys:error)
  2087.   (:initable-instance-variables continue-format-string))
  2088.  
  2089. #+(and Genera (not clx-ansi-common-lisp))
  2090. (scl:defmethod (scl:make-instance x-error) (&rest ignore)
  2091.   (when (not (sys:variable-boundp continue-format-string))
  2092.     (setf dbg:proceed-types (remove :continue dbg:proceed-types))))
  2093.  
  2094. #+(and Genera (not clx-ansi-common-lisp))
  2095. (scl:defmethod (dbg:proceed x-error :continue) ()
  2096.   :continue)
  2097.  
  2098. #+(and Genera (not clx-ansi-common-lisp))
  2099. (sys:defmethod (dbg:document-proceed-type x-error :continue) (stream)
  2100.   (format stream continue-format-string))
  2101.  
  2102. #+(or clx-ansi-common-lisp excl lcl3.0)
  2103. (define-condition x-error (error) ())
  2104.  
  2105. #-(or lispm clx-ansi-common-lisp excl lcl3.0)
  2106. (defstruct x-error
  2107.   report-function)
  2108.  
  2109. #-(or lispm clx-ansi-common-lisp excl lcl3.0)
  2110. (defmacro define-condition (name parent-types &body options)
  2111.   ;; Define a structure that when printed displays an error message
  2112.   (flet ((reporter-for-condition (name)
  2113.        (xintern "." name '-reporter.)))
  2114.     (let ((slot-names
  2115.         (mapcar #'(lambda (slot) (if (consp slot) (car slot) slot))
  2116.             (pop options)))
  2117.       (documentation nil)
  2118.       (conc-name (concatenate 'string (string name) "-"))           
  2119.       (reporter nil)
  2120.       (condition (gensym))
  2121.       (stream (gensym))
  2122.       (report-function (reporter-for-condition name)))
  2123.       (dolist (item options)
  2124.     (ecase (first item)
  2125.       (:documentation (setq documentation (second item)))
  2126.       (:conc-name (setq conc-name (string (second item))))
  2127.       (:report (setq reporter (second item)))))
  2128.       (unless reporter
  2129.     (setq report-function (reporter-for-condition (first parent-types))))
  2130.       `(within-definition (,name define-condition)
  2131.      (defstruct (,name (:conc-name ,(intern conc-name))
  2132.              (:print-function condition-print)
  2133.              (:include ,(first parent-types)
  2134.               (report-function ',report-function)))
  2135.        ,@slot-names)
  2136.      ,(when documentation
  2137.         `(setf (documentation name 'type) ,documentation))
  2138.      ,(when reporter
  2139.         `(defun ,report-function (,condition ,stream)
  2140.            ,(if (stringp reporter)
  2141.             `(write-string ,reporter ,stream)
  2142.           `(,reporter ,condition ,stream))
  2143.            ,condition))
  2144.      ',name))))
  2145.  
  2146. #-(or lispm clx-ansi-common-lisp excl lcl3.0)
  2147. (defun condition-print (condition stream depth)
  2148.   (declare (type x-error condition)
  2149.        (type stream stream)
  2150.        (ignore depth))
  2151.   (if *print-escape*
  2152.       (print-unreadable-object (condition stream :type t))
  2153.     (funcall (x-error-report-function condition) condition stream))
  2154.   condition)
  2155.   
  2156. #-(or lispm clx-ansi-common-lisp excl lcl3.0)
  2157. (defun make-condition (type &rest slot-initializations)
  2158.   (declare (dynamic-extent slot-initializations))
  2159.   (let ((make-function (intern (concatenate 'string (string 'make-) (string type))
  2160.                    (symbol-package type))))
  2161.     (apply make-function slot-initializations)))
  2162.  
  2163. #-(or clx-ansi-common-lisp excl lcl3.0)
  2164. (define-condition type-error (x-error)
  2165.   ((datum :reader type-error-datum :initarg :datum)
  2166.    (expected-type :reader type-error-expected-type :initarg :expected-type))
  2167.   (:report
  2168.     (lambda (condition stream)
  2169.       (format stream "~s isn't a ~a"
  2170.           (type-error-datum condition)
  2171.           (type-error-expected-type condition)))))
  2172.  
  2173.  
  2174. ;;-----------------------------------------------------------------------------
  2175. ;;  HOST hacking
  2176. ;;-----------------------------------------------------------------------------
  2177.  
  2178. #-(or explorer Genera Minima)
  2179. (defun host-address (host &optional (family :internet))
  2180.   ;; Return a list whose car is the family keyword (:internet :DECnet :Chaos)
  2181.   ;; and cdr is a list of network address bytes.
  2182.   (declare (type (or stringable list) host)
  2183.        (type (or null (member :internet :decnet :chaos) card8) family))
  2184.   (declare (values list))
  2185.   host family
  2186.   (error "HOST-ADDRESS not implemented yet."))
  2187.  
  2188. #+explorer
  2189. (defun host-address (host &optional (family :internet))
  2190.   ;; Return a list whose car is the family keyword (:internet :DECnet :Chaos)
  2191.   ;; and cdr is a list of network address bytes.
  2192.   (declare (type (or stringable list) host)
  2193.        (type (or null (member :internet :decnet :chaos) card8) family))
  2194.   (declare (values list))
  2195.   (ecase family
  2196.     (:internet
  2197.      (let ((addr (ip:get-ip-address host)))
  2198.        (unless addr (error "~s isn't an internet host name" host))
  2199.        (list :internet
  2200.          (ldb (byte 8 24) addr)
  2201.          (ldb (byte 8 16) addr)
  2202.          (ldb (byte 8 8) addr)
  2203.          (ldb (byte 8 0) addr))))
  2204.     (:chaos
  2205.      (let ((addr (first (chaos:chaos-addresses host))))
  2206.        (unless addr (error "~s isn't a chaos host name" host))
  2207.        (list :chaos
  2208.          (ldb (byte 8 0) addr)
  2209.          (ldb (byte 8 8) addr))))))
  2210.  
  2211. #+Genera
  2212. (defun host-address (host &optional (family :internet))
  2213.   ;; Return a list whose car is the family keyword (:internet :DECnet :Chaos)
  2214.   ;; and cdr is a list of network address bytes.
  2215.   (declare (type (or stringable list) host)
  2216.        (type (or null (member :internet :decnet :chaos) card8) family))
  2217.   (declare (values list))
  2218.   (let ((net-type (if (eq family :DECnet)
  2219.               :DNA
  2220.               family)))
  2221.     (dolist (addr
  2222.           (sys:send (net:parse-host host) :network-addresses)
  2223.           (error "~s isn't a valid ~(~A~) host name" host family))
  2224.       (let ((network (car addr))
  2225.         (address (cadr addr)))
  2226.     (when (sys:send network :network-typep net-type)
  2227.       (return (ecase family
  2228.             (:internet
  2229.               (multiple-value-bind (a b c d) (tcp:explode-internet-address address)
  2230.             (list :internet a b c d)))
  2231.             ((:chaos :DECnet)
  2232.              (list family (ldb (byte 8 0) address) (ldb (byte 8 8) address))))))))))
  2233.  
  2234. #+Minima
  2235. (defun host-address (host &optional (family :internet))
  2236.   ;; Return a list whose car is the family keyword (:internet :DECnet :Chaos)
  2237.   ;; and cdr is a list of network address bytes.
  2238.   (declare (type (or stringable list) host)
  2239.        (type (or null (member :internet :decnet :chaos) card8) family))
  2240.   (declare (values list))
  2241.   (check-type family (member :internet))
  2242.   (or (loop with (num delim)
  2243.         repeat 4
  2244.         for idx = 0 then (1+ delim)
  2245.         when (and delim (or (= delim (length host))
  2246.                 (char-not-equal (char host delim) #\.)))
  2247.           return nil
  2248.         do (multiple-value-setq (num delim)
  2249.              (parse-integer host :start idx :junk-allowed t))
  2250.         when (or (null num) (< num 0) (> num 255))
  2251.           return nil
  2252.         else collect num into nums
  2253.         finally (return (when (= delim (length host))
  2254.                   (cons :internet nums))))
  2255.       (error "Invalid internet address [~A]." host)))
  2256.  
  2257. #+explorer ;; This isn't required, but it helps make sense of the results from access-hosts
  2258. (defun get-host (host-object)
  2259.   ;; host-object is a list whose car is the family keyword (:internet :DECnet :Chaos)
  2260.   ;; and cdr is a list of network address bytes.
  2261.   (declare (type list host-object))
  2262.   (declare (values string family))
  2263.   (let* ((family (first host-object))
  2264.      (address (ecase family
  2265.             (:internet
  2266.              (dpb (second host-object)
  2267.               (byte 8 24)
  2268.               (dpb (third host-object)
  2269.                    (byte 8 16)
  2270.                    (dpb (fourth host-object)
  2271.                     (byte 8 8)
  2272.                     (fifth host-object)))))
  2273.             (:chaos
  2274.              (dpb (third host-object) (byte 8 8) (second host-object))))))
  2275.     (when (eq family :internet) (setq family :ip))
  2276.     (let ((host (si:get-host-from-address address family)))
  2277.       (values (and host (funcall host :name)) family))))
  2278.  
  2279. ;;; This isn't required, but it helps make sense of the results from access-hosts
  2280. #+Genera
  2281. (defun get-host (host-object)
  2282.   ;; host-object is a list whose car is the family keyword (:internet :DECnet :Chaos)
  2283.   ;; and cdr is a list of network address bytes.
  2284.   (declare (type list host-object))
  2285.   (declare (values string family))
  2286.   (let ((family (first host-object)))
  2287.     (values (sys:send (net:get-host-from-address 
  2288.             (ecase family
  2289.               (:internet
  2290.                 (apply #'tcp:build-internet-address (rest host-object)))
  2291.               ((:chaos :DECnet)
  2292.                (dpb (third host-object) (byte 8 8) (second host-object))))
  2293.             (net:local-network-of-type (if (eq family :DECnet)
  2294.                                :DNA
  2295.                                family)))
  2296.               :name)
  2297.         family)))
  2298.  
  2299.  
  2300. ;;-----------------------------------------------------------------------------
  2301. ;; Whether to use closures for requests or not.
  2302. ;;-----------------------------------------------------------------------------
  2303.  
  2304. ;;; If this macro expands to non-NIL, then request and locking code is
  2305. ;;; compiled in a much more compact format, as the common code is shared, and
  2306. ;;; the specific code is built into a closure that is funcalled by the shared
  2307. ;;; code.  If your compiler makes efficient use of closures then you probably
  2308. ;;; want to make this expand to T, as it makes the code more compact.
  2309.  
  2310. (defmacro use-closures ()
  2311.   #+(or lispm Minima) t
  2312.   #-(or lispm Minima) nil)
  2313.  
  2314.  
  2315. ;;-----------------------------------------------------------------------------
  2316. ;; Resource stuff
  2317. ;;-----------------------------------------------------------------------------
  2318.  
  2319.  
  2320. ;;; DEFAULT-RESOURCES-PATHNAME - The pathname of the resources file to load if
  2321. ;;; a resource manager isn't running.
  2322.  
  2323. (defun default-resources-pathname ()
  2324.   (when #+unix t #-unix (search "Unix" (software-type) :test #'char-equal)
  2325.     (merge-pathnames (user-homedir-pathname) (pathname ".Xdefaults"))))
  2326.  
  2327.  
  2328.  
  2329. ;;; RESOURCES-PATHNAME - The pathname of the resources file to load after the
  2330. ;;; defaults have been loaded.
  2331.  
  2332. (defun resources-pathname ()
  2333.   (when #+unix t #-unix (search "Unix" (software-type) :test #'char-equal)
  2334.     (or #+(or excl (and lcl3.0 (not vax-vms)))
  2335.     (let ((string (#+excl sys:getenv
  2336.                #+lcl3.0 lcl:environment-variable
  2337.                "XENVIRONMENT")))
  2338.       (when string
  2339.         (pathname string)))
  2340.     (merge-pathnames
  2341.       (user-homedir-pathname)
  2342.       (pathname 
  2343.         (concatenate 'simple-string ".Xdefaults-"
  2344.              #+excl (short-site-name)
  2345.              #-excl (machine-instance)))))))
  2346.  
  2347.  
  2348. ;;-----------------------------------------------------------------------------
  2349. ;; GC stuff
  2350. ;;-----------------------------------------------------------------------------
  2351.  
  2352. (defun gc-cleanup ()
  2353.   (declare (special *event-free-list*
  2354.             *pending-command-free-list*
  2355.             *reply-buffer-free-lists*
  2356.             *gcontext-local-state-cache*
  2357.             *temp-gcontext-cache*))
  2358.   (setq *event-free-list* nil)
  2359.   (setq *pending-command-free-list* nil)
  2360.   (when (boundp '*reply-buffer-free-lists*)
  2361.     (fill *reply-buffer-free-lists* nil))
  2362.   (setq *gcontext-local-state-cache* nil)
  2363.   (setq *temp-gcontext-cache* nil)
  2364.   nil)
  2365.  
  2366. #+Genera
  2367. (si:define-gc-cleanup clx-cleanup ("CLX Cleanup")
  2368.   (gc-cleanup))
  2369.  
  2370.  
  2371. ;;-----------------------------------------------------------------------------
  2372. ;; WITH-STANDARD-IO-SYNTAX equivalent, used in (SETF WM-COMMAND)
  2373. ;;-----------------------------------------------------------------------------
  2374.  
  2375. #-(or clx-ansi-common-lisp Genera)
  2376. (defun with-standard-io-syntax-function (function)
  2377.   (declare #+lispm
  2378.        (sys:downward-funarg function))
  2379.   (let ((*package* (find-package :user))
  2380.     (*print-array* t)
  2381.     (*print-base* 10)
  2382.     (*print-case* :upcase)
  2383.     (*print-circle* nil)
  2384.     (*print-escape* t)
  2385.     (*print-gensym* t)
  2386.     (*print-length* nil)
  2387.     (*print-level* nil)
  2388.     (*print-pretty* nil)
  2389.     (*print-radix* nil)
  2390.     (*read-base* 10)
  2391.     (*read-default-float-format* 'single-float)
  2392.     (*read-suppress* nil)
  2393.     #+ticl (ticl:*print-structure* t)
  2394.     #+lucid (lucid::*print-structure* t))
  2395.     (funcall function)))
  2396.  
  2397. #-(or clx-ansi-common-lisp Genera)
  2398. (defmacro with-standard-io-syntax (&body body)
  2399.   `(flet ((.with-standard-io-syntax-body. () ,@body))
  2400.      (with-standard-io-syntax-function #'.with-standard-io-syntax-body.)))
  2401.  
  2402.  
  2403. ;;-----------------------------------------------------------------------------
  2404. ;; DEFAULT-KEYSYM-TRANSLATE
  2405. ;;-----------------------------------------------------------------------------
  2406.  
  2407. ;;; If object is a character, char-bits are set from state.
  2408. ;;;
  2409. ;;; [the following isn't implemented (should it be?)]
  2410. ;;; If object is a list, it is an alist with entries:
  2411. ;;; (base-char [modifiers] [mask-modifiers])
  2412. ;;; When MODIFIERS are specified, this character translation
  2413. ;;; will only take effect when the specified modifiers are pressed.
  2414. ;;; MASK-MODIFIERS can be used to specify a set of modifiers to ignore.
  2415. ;;; When MASK-MODIFIERS is missing, all other modifiers are ignored.
  2416. ;;; In ambiguous cases, the most specific translation is used.
  2417.  
  2418. #+(or (not clx-ansi-common-lisp) lispm allegro)
  2419. (defun default-keysym-translate (display state object)
  2420.   (declare (type display display)
  2421.        (type card16 state)
  2422.        (type t object)
  2423.        (values t)
  2424.        (special left-meta-keysym right-meta-keysym
  2425.             left-super-keysym right-super-keysym
  2426.             left-hyper-keysym right-hyper-keysym))
  2427.   (when (characterp object)
  2428.     (when (logbitp (position :control *state-mask-vector*) state)
  2429.       (setf (char-bit object :control) 1))
  2430.     (when (or (state-keysymp display state left-meta-keysym)
  2431.           (state-keysymp display state right-meta-keysym))
  2432.       (setf (char-bit object :meta) 1))
  2433.     (when (or (state-keysymp display state left-super-keysym)
  2434.           (state-keysymp display state right-super-keysym))
  2435.       (setf (char-bit object :super) 1))
  2436.     (when (or (state-keysymp display state left-hyper-keysym)
  2437.           (state-keysymp display state right-hyper-keysym))
  2438.       (setf (char-bit object :hyper) 1)))
  2439.   object)
  2440.  
  2441. #+(and clx-ansi-common-lisp (not lispm) (not allegro))
  2442. (defun default-keysym-translate (display state object)
  2443.   (declare (type display display)
  2444.        (type card16 state)
  2445.        (type t object)
  2446.        (ignore display state)
  2447.        (values t))
  2448.   object)
  2449.  
  2450.  
  2451. ;;-----------------------------------------------------------------------------
  2452. ;; Image stuff
  2453. ;;-----------------------------------------------------------------------------
  2454.  
  2455. ;;; Types
  2456.  
  2457. (deftype pixarray-1-element-type ()
  2458.   'bit)
  2459.  
  2460. (deftype pixarray-4-element-type ()
  2461.   '(unsigned-byte 4))
  2462.  
  2463. (deftype pixarray-8-element-type ()
  2464.   '(unsigned-byte 8))
  2465.  
  2466. (deftype pixarray-16-element-type ()
  2467.   '(unsigned-byte 16))
  2468.  
  2469. (deftype pixarray-24-element-type ()
  2470.   '(unsigned-byte 24))
  2471.  
  2472. (deftype pixarray-32-element-type ()
  2473.   #-(or Genera Minima) '(unsigned-byte 32)
  2474.   #+(or Genera Minima) 'fixnum)
  2475.  
  2476. (deftype pixarray-1  ()
  2477.   '(array pixarray-1-element-type (* *)))
  2478.  
  2479. (deftype pixarray-4  ()
  2480.   '(array pixarray-4-element-type (* *)))
  2481.  
  2482. (deftype pixarray-8  ()
  2483.   '(array pixarray-8-element-type (* *)))
  2484.  
  2485. (deftype pixarray-16 ()
  2486.   '(array pixarray-16-element-type (* *)))
  2487.  
  2488. (deftype pixarray-24 ()
  2489.   '(array pixarray-24-element-type (* *)))
  2490.  
  2491. (deftype pixarray-32 ()
  2492.   '(array pixarray-32-element-type (* *)))
  2493.  
  2494. (deftype pixarray ()
  2495.   '(or pixarray-1 pixarray-4 pixarray-8 pixarray-16 pixarray-24 pixarray-32))
  2496.  
  2497. (deftype bitmap ()
  2498.   'pixarray-1)
  2499.  
  2500. ;;; WITH-UNDERLYING-SIMPLE-VECTOR 
  2501.  
  2502. #+Genera
  2503. (defmacro with-underlying-simple-vector
  2504.       ((variable element-type pixarray) &body body)
  2505.   (let ((bits-per-element
  2506.       (sys:array-bits-per-element
  2507.         (symbol-value (sys:type-array-element-type element-type)))))
  2508.     `(scl:stack-let ((,variable
  2509.               (make-array
  2510.             (index-ceiling
  2511.               (index* (array-total-size ,pixarray)
  2512.                   (sys:array-element-size ,pixarray))
  2513.               ,bits-per-element)
  2514.             :element-type ',element-type
  2515.             :displaced-to ,pixarray)))
  2516.        (declare (array-register ,variable))
  2517.        ,@body)))
  2518.  
  2519. #+lcl3.0
  2520. (defmacro with-underlying-simple-vector
  2521.       ((variable element-type pixarray) &body body)
  2522.   `(let ((,variable (sys:underlying-simple-vector ,pixarray)))
  2523.      (declare (type (simple-array ,element-type (*)) ,variable))
  2524.      ,@body))
  2525.  
  2526. #+excl
  2527. (defmacro with-underlying-simple-vector
  2528.       ((variable element-type pixarray) &body body)
  2529.   `(let ((,variable (cdr (excl::ah_data ,pixarray))))
  2530.      (declare (type (simple-array ,element-type (*)) ,variable))
  2531.      ,@body))
  2532.  
  2533. ;;; These are used to read and write pixels from and to CARD8s.
  2534.  
  2535. ;;; READ-IMAGE-LOAD-BYTE is used to extract 1 and 4 bit pixels from CARD8s.
  2536.  
  2537. (defmacro read-image-load-byte (size position integer)
  2538.   (unless *image-bit-lsb-first-p* (setq position (- 7 position)))
  2539.   `(the (unsigned-byte ,size)
  2540.     (#-Genera ldb #+Genera sys:%logldb
  2541.      (byte ,size ,position)
  2542.      (the card8 ,integer))))
  2543.  
  2544. ;;; READ-IMAGE-ASSEMBLE-BYTES is used to build 16, 24 and 32 bit pixels from
  2545. ;;; the appropriate number of CARD8s.
  2546.  
  2547. (defmacro read-image-assemble-bytes (&rest bytes)
  2548.   (unless *image-byte-lsb-first-p* (setq bytes (reverse bytes)))
  2549.   (let ((it (first bytes))
  2550.     (count 0))
  2551.     (dolist (byte (rest bytes))
  2552.       (setq it
  2553.         `(#-Genera dpb #+Genera sys:%logdpb 
  2554.           (the card8 ,byte)
  2555.           (byte 8 ,(incf count 8))
  2556.           (the (unsigned-byte ,count) ,it))))
  2557.     #-Genera `(the (unsigned-byte ,(* (length bytes) 8)) ,it)
  2558.     #+Genera it))
  2559.  
  2560. ;;; WRITE-IMAGE-LOAD-BYTE is used to extract a CARD8 from a 16, 24 or 32 bit
  2561. ;;; pixel.
  2562.  
  2563. (defmacro write-image-load-byte (position integer integer-size)
  2564.   integer-size
  2565.   (unless *image-byte-lsb-first-p* (setq position (- integer-size 8 position)))
  2566.   `(the card8
  2567.     (#-Genera ldb #+Genera sys:%logldb
  2568.      (byte 8 ,position)
  2569.      #-Genera (the (unsigned-byte ,integer-size) ,integer)
  2570.      #+Genera ,integer
  2571.      )))
  2572.  
  2573. ;;; WRITE-IMAGE-ASSEMBLE-BYTES is used to build a CARD8 from 1 or 4 bit
  2574. ;;; pixels.
  2575.  
  2576. (defmacro write-image-assemble-bytes (&rest bytes)
  2577.   (unless *image-bit-lsb-first-p* (setq bytes (reverse bytes)))
  2578.   (let ((size (floor 8 (length bytes)))
  2579.     (it (first bytes))
  2580.     (count 0))
  2581.     (dolist (byte (rest bytes))
  2582.       (setq it `(#-Genera dpb #+Genera sys:%logdpb
  2583.          (the (unsigned-byte ,size) ,byte)
  2584.          (byte ,size ,(incf count size))
  2585.          (the (unsigned-byte ,count) ,it))))
  2586.     `(the card8 ,it)))
  2587.  
  2588. #+(or Genera lcl3.0 excl)
  2589. (defvar *computed-image-byte-lsb-first-p* *image-byte-lsb-first-p*)
  2590.  
  2591. #+(or Genera lcl3.0 excl)
  2592. (defvar *computed-image-bit-lsb-first-p* *image-bit-lsb-first-p*)
  2593.  
  2594. ;;; The following table gives the bit ordering within bytes (when accessed
  2595. ;;; sequentially) for a scanline containing 32 bits, with bits numbered 0 to
  2596. ;;; 31, where bit 0 should be leftmost on the display.  For a given byte
  2597. ;;; labelled A-B, A is for the most significant bit of the byte, and B is
  2598. ;;; for the least significant bit.
  2599. ;;; 
  2600. ;;; legend:
  2601. ;;;     1   scanline-unit = 8
  2602. ;;;     2   scanline-unit = 16
  2603. ;;;     4   scanline-unit = 32
  2604. ;;;     M   byte-order = MostSignificant
  2605. ;;;     L   byte-order = LeastSignificant
  2606. ;;;     m   bit-order = MostSignificant
  2607. ;;;     l   bit-order = LeastSignificant
  2608. ;;; 
  2609. ;;; 
  2610. ;;; format    ordering
  2611. ;;; 
  2612. ;;; 1Mm    00-07 08-15 16-23 24-31
  2613. ;;; 2Mm    00-07 08-15 16-23 24-31
  2614. ;;; 4Mm    00-07 08-15 16-23 24-31
  2615. ;;; 1Ml    07-00 15-08 23-16 31-24
  2616. ;;; 2Ml    15-08 07-00 31-24 23-16
  2617. ;;; 4Ml    31-24 23-16 15-08 07-00
  2618. ;;; 1Lm    00-07 08-15 16-23 24-31
  2619. ;;; 2Lm    08-15 00-07 24-31 16-23
  2620. ;;; 4Lm    24-31 16-23 08-15 00-07
  2621. ;;; 1Ll    07-00 15-08 23-16 31-24
  2622. ;;; 2Ll    07-00 15-08 23-16 31-24
  2623. ;;; 4Ll    07-00 15-08 23-16 31-24
  2624.  
  2625. #+(or Genera lcl3.0 excl) 
  2626. (defconstant
  2627.   *image-bit-ordering-table*
  2628.   '(((1 (00 07) (08 15) (16 23) (24 31)) (nil nil))
  2629.     ((2 (00 07) (08 15) (16 23) (24 31)) (nil nil))
  2630.     ((4 (00 07) (08 15) (16 23) (24 31)) (nil nil))
  2631.     ((1 (07 00) (15 08) (23 16) (31 24)) (nil t))
  2632.     ((2 (15 08) (07 00) (31 24) (23 16)) (nil t))
  2633.     ((4 (31 24) (23 16) (15 08) (07 00)) (nil t))
  2634.     ((1 (00 07) (08 15) (16 23) (24 31)) (t   nil))
  2635.     ((2 (08 15) (00 07) (24 31) (16 23)) (t   nil))
  2636.     ((4 (24 31) (16 23) (08 15) (00 07)) (t   nil))
  2637.     ((1 (07 00) (15 08) (23 16) (31 24)) (t   t))
  2638.     ((2 (07 00) (15 08) (23 16) (31 24)) (t   t))
  2639.     ((4 (07 00) (15 08) (23 16) (31 24)) (t   t))))
  2640.   
  2641. #+(or Genera lcl3.0 excl) 
  2642. (defun compute-image-byte-and-bit-ordering ()
  2643.   (declare (values image-byte-lsb-first-p image-bit-lsb-first-p))
  2644.   ;; First compute the ordering 
  2645.   (let ((ordering nil)
  2646.     (a (make-array '(1 32) :element-type 'bit :initial-element 0)))
  2647.     (dotimes (i 4)
  2648.       (push (flet ((bitpos (a i n)
  2649.              (declare (optimize (speed 3) (safety 0) (space 0)))
  2650.              (declare (type (simple-array bit (* *)) a)
  2651.                   (type fixnum i n))
  2652.              (with-underlying-simple-vector (v (unsigned-byte 8) a)
  2653.                (prog2
  2654.              (setf (aref v i) n)
  2655.              (dotimes (i 32)
  2656.                (unless (zerop (aref a 0 i))
  2657.                  (return i)))
  2658.              (setf (aref v i) 0)))))
  2659.           (list (bitpos a i #b10000000)
  2660.             (bitpos a i #b00000001)))
  2661.         ordering))
  2662.     (setq ordering (cons (floor *image-unit* 8) (nreverse ordering)))
  2663.     ;; Now from the ordering, compute byte-lsb-first-p and bit-lsb-first-p
  2664.     (let ((byte-and-bit-ordering
  2665.         (second (assoc ordering *image-bit-ordering-table*
  2666.                :test #'equal))))
  2667.       (unless byte-and-bit-ordering
  2668.     (error "Couldn't determine image byte and bit ordering~@
  2669.                 measured image ordering = ~A"
  2670.            ordering))
  2671.       (values-list byte-and-bit-ordering))))
  2672.  
  2673. #+(or Genera lcl3.0 excl) 
  2674. (multiple-value-setq
  2675.   (*computed-image-byte-lsb-first-p* *computed-image-bit-lsb-first-p*)
  2676.   (compute-image-byte-and-bit-ordering))
  2677.  
  2678. ;;; If you can write fast routines that can read and write pixarrays out of a
  2679. ;;; buffer-bytes, do it!  It makes the image code a lot faster.  The
  2680. ;;; FAST-READ-PIXARRAY, FAST-WRITE-PIXARRAY and FAST-COPY-PIXARRAY routines
  2681. ;;; return T if they can do it, NIL if they can't.
  2682.  
  2683. ;;; FAST-READ-PIXARRAY - fill part of a pixarray from a buffer of card8s
  2684.  
  2685. #+(or lcl3.0 excl)
  2686. (defun fast-read-pixarray-1 (buffer-bbuf index array x y width height  
  2687.                  padded-bytes-per-line bits-per-pixel)
  2688.   (declare (type buffer-bytes buffer-bbuf)
  2689.        (type pixarray-1 array)
  2690.        (type card16 x y width height)
  2691.        (type array-index index padded-bytes-per-line)
  2692.        (type (member 1 4 8 16 24 32) bits-per-pixel)
  2693.        (ignore bits-per-pixel))
  2694.   #.(declare-buffun)
  2695.   (with-vector (buffer-bbuf buffer-bytes)
  2696.     (with-underlying-simple-vector (vector pixarray-1-element-type array)
  2697.       (do* ((start (index+ index
  2698.                (index* y padded-bytes-per-line)
  2699.                (index-ceiling x 8))
  2700.            (index+ start padded-bytes-per-line))
  2701.         (y 0 (index1+ y))
  2702.         (left-bits (index-mod (index- x) 8))
  2703.         (right-bits (index-mod (index- width left-bits) 8))
  2704.         (middle-bits (index- width left-bits right-bits))
  2705.         (middle-bytes (index-floor middle-bits 8)))
  2706.        ((index>= y height))
  2707.     (declare (type array-index start y
  2708.                left-bits right-bits middle-bits middle-bytes))
  2709.     (cond ((index< middle-bits 0)
  2710.            (let ((byte (aref buffer-bbuf (index1- start)))
  2711.              (x (array-row-major-index array y left-bits)))
  2712.          (declare (type card8 byte)
  2713.               (type array-index x))
  2714.          (when (index> right-bits 6)
  2715.            (setf (aref vector (index- x 1))
  2716.              (read-image-load-byte 1 7 byte)))
  2717.          (when (and (index> left-bits 1)
  2718.                 (index> right-bits 5))
  2719.            (setf (aref vector (index- x 2))
  2720.              (read-image-load-byte 1 6 byte)))
  2721.          (when (and (index> left-bits 2)
  2722.                 (index> right-bits 4))
  2723.            (setf (aref vector (index- x 3))
  2724.              (read-image-load-byte 1 5 byte)))
  2725.          (when (and (index> left-bits 3)
  2726.                 (index> right-bits 3))
  2727.            (setf (aref vector (index- x 4))
  2728.              (read-image-load-byte 1 4 byte)))
  2729.          (when (and (index> left-bits 4)
  2730.                 (index> right-bits 2))
  2731.            (setf (aref vector (index- x 5))
  2732.              (read-image-load-byte 1 3 byte)))
  2733.          (when (and (index> left-bits 5)
  2734.                 (index> right-bits 1))
  2735.            (setf (aref vector (index- x 6))
  2736.              (read-image-load-byte 1 2 byte)))
  2737.          (when (index> left-bits 6)
  2738.            (setf (aref vector (index- x 7))
  2739.              (read-image-load-byte 1 1 byte)))))
  2740.           (t
  2741.            (unless (index-zerop left-bits)
  2742.          (let ((byte (aref buffer-bbuf (index1- start)))
  2743.                (x (array-row-major-index array y left-bits)))
  2744.            (declare (type card8 byte)
  2745.                 (type array-index x))
  2746.            (setf (aref vector (index- x 1))
  2747.              (read-image-load-byte 1 7 byte))
  2748.            (when (index> left-bits 1)
  2749.              (setf (aref vector (index- x 2))
  2750.                (read-image-load-byte 1 6 byte))
  2751.              (when (index> left-bits 2)
  2752.                (setf (aref vector (index- x 3))
  2753.                  (read-image-load-byte 1 5 byte))
  2754.                (when (index> left-bits 3)
  2755.              (setf (aref vector (index- x 4))
  2756.                    (read-image-load-byte 1 4 byte))
  2757.              (when (index> left-bits 4)
  2758.                (setf (aref vector (index- x 5))
  2759.                  (read-image-load-byte 1 3 byte))
  2760.                (when (index> left-bits 5)
  2761.                  (setf (aref vector (index- x 6))
  2762.                    (read-image-load-byte 1 2 byte))
  2763.                  (when (index> left-bits 6)
  2764.                    (setf (aref vector (index- x 7))
  2765.                      (read-image-load-byte 1 1 byte))
  2766.                    ))))))))
  2767.            (do* ((end (index+ start middle-bytes))
  2768.              (i start (index1+ i))
  2769.              (x (array-row-major-index array y left-bits) (index+ x 8)))
  2770.             ((index>= i end)
  2771.              (unless (index-zerop right-bits)
  2772.                (let ((byte (aref buffer-bbuf end))
  2773.                  (x (array-row-major-index
  2774.                   array y (index+ left-bits middle-bits))))
  2775.              (declare (type card8 byte)
  2776.                   (type array-index x))
  2777.              (setf (aref vector (index+ x 0))
  2778.                    (read-image-load-byte 1 0 byte))
  2779.              (when (index> right-bits 1)
  2780.                (setf (aref vector (index+ x 1))
  2781.                  (read-image-load-byte 1 1 byte))
  2782.                (when (index> right-bits 2)
  2783.                  (setf (aref vector (index+ x 2))
  2784.                    (read-image-load-byte 1 2 byte))
  2785.                  (when (index> right-bits 3)
  2786.                    (setf (aref vector (index+ x 3))
  2787.                      (read-image-load-byte 1 3 byte))
  2788.                    (when (index> right-bits 4)
  2789.                  (setf (aref vector (index+ x 4))
  2790.                        (read-image-load-byte 1 4 byte))
  2791.                  (when (index> right-bits 5)
  2792.                    (setf (aref vector (index+ x 5))
  2793.                      (read-image-load-byte 1 5 byte))
  2794.                    (when (index> right-bits 6)
  2795.                      (setf (aref vector (index+ x 6))
  2796.                        (read-image-load-byte 1 6 byte))
  2797.                      )))))))))
  2798.          (declare (type array-index end i x))
  2799.          (let ((byte (aref buffer-bbuf i)))
  2800.            (declare (type card8 byte))
  2801.            (setf (aref vector (index+ x 0))
  2802.              (read-image-load-byte 1 0 byte))
  2803.            (setf (aref vector (index+ x 1))
  2804.              (read-image-load-byte 1 1 byte))
  2805.            (setf (aref vector (index+ x 2))
  2806.              (read-image-load-byte 1 2 byte))
  2807.            (setf (aref vector (index+ x 3))
  2808.              (read-image-load-byte 1 3 byte))
  2809.            (setf (aref vector (index+ x 4))
  2810.              (read-image-load-byte 1 4 byte))
  2811.            (setf (aref vector (index+ x 5))
  2812.              (read-image-load-byte 1 5 byte))
  2813.            (setf (aref vector (index+ x 6))
  2814.              (read-image-load-byte 1 6 byte))
  2815.            (setf (aref vector (index+ x 7))
  2816.              (read-image-load-byte 1 7 byte))))
  2817.            )))))
  2818.   t)
  2819.  
  2820. #+(or lcl3.0 excl)
  2821. (defun fast-read-pixarray-4 (buffer-bbuf index array x y width height 
  2822.                  padded-bytes-per-line bits-per-pixel)
  2823.   (declare (type buffer-bytes buffer-bbuf)
  2824.        (type pixarray-4 array)
  2825.        (type card16 x y width height)
  2826.        (type array-index index padded-bytes-per-line)
  2827.        (type (member 1 4 8 16 24 32) bits-per-pixel)
  2828.        (ignore bits-per-pixel))
  2829.   #.(declare-buffun)
  2830.   (with-vector (buffer-bbuf buffer-bytes)
  2831.     (with-underlying-simple-vector (vector pixarray-4-element-type array)
  2832.       (do* ((start (index+ index
  2833.                (index* y padded-bytes-per-line)
  2834.                (index-ceiling x 2))
  2835.            (index+ start padded-bytes-per-line))
  2836.         (y 0 (index1+ y))
  2837.         (left-nibbles (index-mod (index- x) 2))
  2838.         (right-nibbles (index-mod (index- width left-nibbles) 2))
  2839.         (middle-nibbles (index- width left-nibbles right-nibbles))
  2840.         (middle-bytes (index-floor middle-nibbles 2)))
  2841.        ((index>= y height))
  2842.     (declare (type array-index start y
  2843.                left-nibbles right-nibbles middle-nibbles middle-bytes))
  2844.     (unless (index-zerop left-nibbles)
  2845.       (setf (aref array y 0)
  2846.         (read-image-load-byte
  2847.           4 4 (aref buffer-bbuf (index1- start)))))
  2848.     (do* ((end (index+ start middle-bytes))
  2849.           (i start (index1+ i))
  2850.           (x (array-row-major-index array y left-nibbles) (index+ x 2)))
  2851.          ((index>= i end)
  2852.           (unless (index-zerop right-nibbles)
  2853.         (setf (aref array y (index+ left-nibbles middle-nibbles))
  2854.               (read-image-load-byte 4 0 (aref buffer-bbuf end)))))
  2855.       (declare (type array-index end i x))
  2856.       (let ((byte (aref buffer-bbuf i)))
  2857.         (declare (type card8 byte))
  2858.         (setf (aref vector (index+ x 0))
  2859.           (read-image-load-byte 4 0 byte))
  2860.         (setf (aref vector (index+ x 1))
  2861.           (read-image-load-byte 4 4 byte))))
  2862.     )))
  2863.   t)
  2864.  
  2865. #+(or Genera lcl3.0 excl)
  2866. (defun fast-read-pixarray-24 (buffer-bbuf index array x y width height 
  2867.                   padded-bytes-per-line bits-per-pixel)
  2868.   (declare (type buffer-bytes buffer-bbuf)
  2869.        (type pixarray-24 array)
  2870.        (type card16 width height)
  2871.        (type array-index index padded-bytes-per-line)
  2872.        (type (member 1 4 8 16 24 32) bits-per-pixel)
  2873.        (ignore bits-per-pixel))
  2874.   #.(declare-buffun)
  2875.   (with-vector (buffer-bbuf buffer-bytes)
  2876.     (with-underlying-simple-vector (vector pixarray-24-element-type array)
  2877.       (do* ((start (index+ index
  2878.                (index* y padded-bytes-per-line)
  2879.                (index* x 3))
  2880.            (index+ start padded-bytes-per-line))
  2881.         (y 0 (index1+ y)))
  2882.        ((index>= y height))
  2883.     (declare (type array-index start y))
  2884.     (do* ((end (index+ start (index* width 3)))
  2885.           (i start (index+ i 3))
  2886.           (x (array-row-major-index array y 0) (index1+ x)))
  2887.          ((index>= i end))
  2888.       (declare (type array-index end i x))
  2889.       (setf (aref vector x)
  2890.         (read-image-assemble-bytes
  2891.           (aref buffer-bbuf (index+ i 0))
  2892.           (aref buffer-bbuf (index+ i 1))
  2893.           (aref buffer-bbuf (index+ i 2))))))))
  2894.   t)
  2895.  
  2896. #+lispm
  2897. (defun fast-read-pixarray-using-bitblt
  2898.        (bbuf boffset pixarray x y width height padded-bytes-per-line
  2899.     bits-per-pixel)
  2900.   (#+Genera sys:stack-let* #-Genera let*
  2901.    ((dimensions (list (+ y height)
  2902.               (floor (* padded-bytes-per-line 8) bits-per-pixel)))
  2903.     (a (make-array
  2904.      dimensions
  2905.      :element-type (array-element-type pixarray)
  2906.      :displaced-to bbuf
  2907.      :displaced-index-offset (floor (* boffset 8) bits-per-pixel))))
  2908.    (sys:bitblt boole-1 width height a x y pixarray 0 0))
  2909.   t)
  2910.  
  2911. #+(or Genera lcl3.0 excl)
  2912. (defun fast-read-pixarray-with-swap
  2913.        (bbuf boffset pixarray x y width height padded-bytes-per-line
  2914.     bits-per-pixel unit byte-lsb-first-p bit-lsb-first-p)
  2915.   (declare (type buffer-bytes bbuf)
  2916.        (type array-index boffset
  2917.          padded-bytes-per-line)
  2918.        (type pixarray pixarray)
  2919.        (type card16 x y width height)
  2920.        (type (member 1 4 8 16 24 32) bits-per-pixel)
  2921.        (type (member 8 16 32) unit)
  2922.        (type boolean byte-lsb-first-p bit-lsb-first-p))
  2923.   (unless (index= bits-per-pixel 24)
  2924.     (let ((pixarray-padded-bits-per-line
  2925.         (if (index= height 1) 0
  2926.           (index* (index- (array-row-major-index pixarray 1 0)
  2927.                   (array-row-major-index pixarray 0 0))
  2928.               bits-per-pixel)))
  2929.       (x-bits (index* x bits-per-pixel)))
  2930.       (declare (type array-index pixarray-padded-bits-per-line x-bits))
  2931.       (when (if (eq *computed-image-byte-lsb-first-p* *computed-image-bit-lsb-first-p*)
  2932.         (and (index-zerop (index-mod pixarray-padded-bits-per-line 8))
  2933.              (index-zerop (index-mod x-bits 8)))
  2934.           (and (index-zerop (index-mod pixarray-padded-bits-per-line *image-unit*))
  2935.            (index-zerop (index-mod x-bits *image-unit*))))
  2936.     (multiple-value-bind (image-swap-function image-swap-lsb-first-p)
  2937.         (image-swap-function
  2938.           bits-per-pixel 
  2939.           unit byte-lsb-first-p bit-lsb-first-p
  2940.           *image-unit* *computed-image-byte-lsb-first-p*
  2941.           *computed-image-bit-lsb-first-p*)
  2942.       (declare (type symbol image-swap-function)
  2943.            (type boolean image-swap-lsb-first-p))
  2944.       (with-underlying-simple-vector (dst card8 pixarray)
  2945.         (funcall
  2946.           (symbol-function image-swap-function) bbuf dst
  2947.           (index+ boffset
  2948.               (index* y padded-bytes-per-line)
  2949.               (index-floor x-bits 8))
  2950.           0 (index-ceiling (index* width bits-per-pixel) 8)
  2951.           padded-bytes-per-line
  2952.           (index-floor pixarray-padded-bits-per-line 8)
  2953.           height image-swap-lsb-first-p)))
  2954.     t))))
  2955.  
  2956. (defun fast-read-pixarray (bbuf boffset pixarray
  2957.                x y width height padded-bytes-per-line
  2958.                bits-per-pixel
  2959.                unit byte-lsb-first-p bit-lsb-first-p)
  2960.   (declare (type buffer-bytes bbuf)
  2961.        (type array-index boffset
  2962.          padded-bytes-per-line)
  2963.        (type pixarray pixarray)
  2964.        (type card16 x y width height)
  2965.        (type (member 1 4 8 16 24 32) bits-per-pixel)
  2966.        (type (member 8 16 32) unit)
  2967.        (type boolean byte-lsb-first-p bit-lsb-first-p))
  2968.   (progn bbuf boffset pixarray x y width height padded-bytes-per-line
  2969.      bits-per-pixel unit byte-lsb-first-p bit-lsb-first-p)
  2970.   (or
  2971.     #+(or Genera lcl3.0 excl)
  2972.     (fast-read-pixarray-with-swap
  2973.       bbuf boffset pixarray x y width height padded-bytes-per-line
  2974.       bits-per-pixel unit byte-lsb-first-p bit-lsb-first-p)
  2975.     (let ((function
  2976.         (or #+lispm
  2977.         (and (= (sys:array-element-size pixarray) bits-per-pixel)
  2978.              (zerop (index-mod padded-bytes-per-line 4))
  2979.              (zerop (index-mod
  2980.                   (* #+Genera (sys:array-row-span pixarray)
  2981.                  #-Genera (array-dimension pixarray 1)
  2982.                  bits-per-pixel)
  2983.                   32))
  2984.              #'fast-read-pixarray-using-bitblt)
  2985.         #+(or lcl3.0 excl)
  2986.         (and (index= bits-per-pixel 1)
  2987.              #'fast-read-pixarray-1)
  2988.         #+(or lcl3.0 excl)
  2989.         (and (index= bits-per-pixel 4)
  2990.              #'fast-read-pixarray-4)
  2991.         #+(or Genera lcl3.0 excl)
  2992.         (and (index= bits-per-pixel 24)
  2993.              #'fast-read-pixarray-24))))
  2994.       (when function
  2995.     (read-pixarray-internal
  2996.       bbuf boffset pixarray x y width height padded-bytes-per-line
  2997.       bits-per-pixel function
  2998.       unit byte-lsb-first-p bit-lsb-first-p
  2999.       *image-unit* *image-byte-lsb-first-p* *image-bit-lsb-first-p*)))))
  3000.  
  3001. ;;; FAST-WRITE-PIXARRAY - copy part of a pixarray into an array of CARD8s
  3002.  
  3003. #+(or lcl3.0 excl)
  3004. (defun fast-write-pixarray-1 (buffer-bbuf index array x y width height
  3005.                   padded-bytes-per-line bits-per-pixel)
  3006.   (declare (type buffer-bytes buffer-bbuf)
  3007.        (type pixarray-1 array)
  3008.        (type card16 x y width height)
  3009.        (type array-index index padded-bytes-per-line)
  3010.        (type (member 1 4 8 16 24 32) bits-per-pixel)
  3011.        (ignore bits-per-pixel))
  3012.   #.(declare-buffun)
  3013.   (with-vector (buffer-bbuf buffer-bytes)
  3014.     (with-underlying-simple-vector (vector pixarray-1-element-type array)
  3015.       (do* ((h 0 (index1+ h))
  3016.         (y y (index1+ y))
  3017.         (right-bits (index-mod width 8))
  3018.         (middle-bits (index- width right-bits))
  3019.         (middle-bytes (index-ceiling middle-bits 8))
  3020.         (start index (index+ start padded-bytes-per-line)))
  3021.        ((index>= h height))
  3022.     (declare (type array-index h y right-bits middle-bits
  3023.                middle-bytes start))
  3024.     (do* ((end (index+ start middle-bytes))
  3025.           (i start (index1+ i))
  3026.           (start-x x)
  3027.           (x (array-row-major-index array y start-x) (index+ x 8)))
  3028.          ((index>= i end)
  3029.           (unless (index-zerop right-bits)
  3030.         (let ((x (array-row-major-index
  3031.                array y (index+ start-x middle-bits))))
  3032.           (declare (type array-index x))
  3033.           (setf (aref buffer-bbuf end)
  3034.             (write-image-assemble-bytes
  3035.               (aref vector (index+ x 0))
  3036.               (if (index> right-bits 1)
  3037.                   (aref vector (index+ x 1))
  3038.                 0)
  3039.               (if (index> right-bits 2)
  3040.                   (aref vector (index+ x 2))
  3041.                 0)
  3042.               (if (index> right-bits 3)
  3043.                   (aref vector (index+ x 3))
  3044.                 0)
  3045.               (if (index> right-bits 4)
  3046.                   (aref vector (index+ x 4))
  3047.                 0)
  3048.               (if (index> right-bits 5)
  3049.                   (aref vector (index+ x 5))
  3050.                 0)
  3051.               (if (index> right-bits 6)
  3052.                   (aref vector (index+ x 6))
  3053.                 0)
  3054.               0)))))
  3055.       (declare (type array-index end i start-x x))
  3056.       (setf (aref buffer-bbuf i)
  3057.         (write-image-assemble-bytes
  3058.           (aref vector (index+ x 0))
  3059.           (aref vector (index+ x 1))
  3060.           (aref vector (index+ x 2))
  3061.           (aref vector (index+ x 3))
  3062.           (aref vector (index+ x 4))
  3063.           (aref vector (index+ x 5))
  3064.           (aref vector (index+ x 6))
  3065.           (aref vector (index+ x 7))))))))
  3066.   t)
  3067.  
  3068. #+(or lcl3.0 excl)
  3069. (defun fast-write-pixarray-4 (buffer-bbuf index array x y width height
  3070.                   padded-bytes-per-line bits-per-pixel)
  3071.   (declare (type buffer-bytes buffer-bbuf)
  3072.        (type pixarray-4 array)
  3073.        (type int16 x y)
  3074.        (type card16 width height)
  3075.        (type array-index index padded-bytes-per-line)
  3076.        (type (member 1 4 8 16 24 32) bits-per-pixel)
  3077.        (ignore bits-per-pixel))
  3078.   #.(declare-buffun)
  3079.   (with-vector (buffer-bbuf buffer-bytes)
  3080.     (with-underlying-simple-vector (vector pixarray-4-element-type array)
  3081.       (do* ((h 0 (index1+ h))
  3082.         (y y (index1+ y))
  3083.         (right-nibbles (index-mod width 2))
  3084.         (middle-nibbles (index- width right-nibbles))
  3085.         (middle-bytes (index-ceiling middle-nibbles 2))
  3086.         (start index (index+ start padded-bytes-per-line)))
  3087.        ((index>= h height))
  3088.     (declare (type array-index h y right-nibbles middle-nibbles
  3089.                middle-bytes start))
  3090.     (do* ((end (index+ start middle-bytes))
  3091.           (i start (index1+ i))
  3092.           (start-x x)
  3093.           (x (array-row-major-index array y start-x) (index+ x 2)))
  3094.          ((index>= i end)
  3095.           (unless (index-zerop right-nibbles)
  3096.         (setf (aref buffer-bbuf end)
  3097.               (write-image-assemble-bytes
  3098.             (aref array y (index+ start-x middle-nibbles))
  3099.             0))))
  3100.       (declare (type array-index end i start-x x))
  3101.       (setf (aref buffer-bbuf i)
  3102.         (write-image-assemble-bytes
  3103.           (aref vector (index+ x 0))
  3104.           (aref vector (index+ x 1))))))))
  3105.   t)
  3106.  
  3107. #+(or Genera lcl3.0 excl)
  3108. (defun fast-write-pixarray-24 (buffer-bbuf index array x y width height
  3109.                    padded-bytes-per-line bits-per-pixel)
  3110.   (declare (type buffer-bytes buffer-bbuf)
  3111.        (type pixarray-24 array)
  3112.        (type int16 x y)
  3113.        (type card16 width height)
  3114.        (type array-index index padded-bytes-per-line)
  3115.        (type (member 1 4 8 16 24 32) bits-per-pixel)
  3116.        (ignore bits-per-pixel))
  3117.   #.(declare-buffun)
  3118.   (with-vector (buffer-bbuf buffer-bytes)
  3119.     (with-underlying-simple-vector (vector pixarray-24-element-type array)
  3120.       (do* ((h 0 (index1+ h))
  3121.         (y y (index1+ y))
  3122.         (start index (index+ start padded-bytes-per-line)))
  3123.        ((index>= h height))
  3124.     (declare (type array-index y start))
  3125.     (do* ((end (index+ start (index* width 3)))
  3126.           (i start (index+ i 3))
  3127.           (x (array-row-major-index array y x) (index1+ x)))
  3128.          ((index>= i end))
  3129.       (declare (type array-index end i x))
  3130.       (let ((pixel (aref vector x)))
  3131.         (declare (type pixarray-24-element-type pixel))
  3132.         (setf (aref buffer-bbuf (index+ i 0))
  3133.           (write-image-load-byte 0 pixel 24))
  3134.         (setf (aref buffer-bbuf (index+ i 1))
  3135.           (write-image-load-byte 8 pixel 24))
  3136.         (setf (aref buffer-bbuf (index+ i 2))
  3137.           (write-image-load-byte 16 pixel 24)))))))
  3138.   t)
  3139.  
  3140. #+lispm
  3141. (defun fast-write-pixarray-using-bitblt
  3142.        (bbuf boffset pixarray x y width height padded-bytes-per-line
  3143.     bits-per-pixel)
  3144.   (#+Genera sys:stack-let* #-Genera let*
  3145.    ((dimensions (list (+ y height)
  3146.               (floor (* padded-bytes-per-line 8) bits-per-pixel)))
  3147.     (a (make-array
  3148.      dimensions
  3149.      :element-type (array-element-type pixarray)
  3150.      :displaced-to bbuf
  3151.      :displaced-index-offset (floor (* boffset 8) bits-per-pixel))))
  3152.    (sys:bitblt boole-1 width height pixarray x y a 0 0))
  3153.   t)
  3154.  
  3155. #+(or Genera lcl3.0 excl)
  3156. (defun fast-write-pixarray-with-swap
  3157.        (bbuf boffset pixarray x y width height padded-bytes-per-line
  3158.     bits-per-pixel unit byte-lsb-first-p bit-lsb-first-p)
  3159.   (declare (type buffer-bytes bbuf)
  3160.        (type pixarray pixarray)
  3161.        (type card16 x y width height)
  3162.        (type array-index boffset padded-bytes-per-line)
  3163.        (type (member 1 4 8 16 24 32) bits-per-pixel)
  3164.        (type (member 8 16 32) unit)
  3165.        (type boolean byte-lsb-first-p bit-lsb-first-p))
  3166.   (unless (index= bits-per-pixel 24)
  3167.     (let ((pixarray-padded-bits-per-line
  3168.         (if (index= height 1) 0
  3169.           (index* (index- (array-row-major-index pixarray 1 0)
  3170.                   (array-row-major-index pixarray 0 0))
  3171.               bits-per-pixel)))
  3172.       (pixarray-start-bit-offset
  3173.         (index* (array-row-major-index pixarray y x)
  3174.             bits-per-pixel)))
  3175.       (declare (type array-index pixarray-padded-bits-per-line
  3176.              pixarray-start-bit-offset))
  3177.       (when (if (eq *computed-image-byte-lsb-first-p* *computed-image-bit-lsb-first-p*)
  3178.         (and (index-zerop (index-mod pixarray-padded-bits-per-line 8))
  3179.              (index-zerop (index-mod pixarray-start-bit-offset 8)))
  3180.           (and (index-zerop (index-mod pixarray-padded-bits-per-line *image-unit*))
  3181.            (index-zerop (index-mod pixarray-start-bit-offset *image-unit*))))
  3182.     (multiple-value-bind (image-swap-function image-swap-lsb-first-p)
  3183.         (image-swap-function
  3184.           bits-per-pixel
  3185.           *image-unit* *computed-image-byte-lsb-first-p*
  3186.           *computed-image-bit-lsb-first-p*
  3187.           unit byte-lsb-first-p bit-lsb-first-p)
  3188.       (declare (type symbol image-swap-function)
  3189.            (type boolean image-swap-lsb-first-p))
  3190.       (with-underlying-simple-vector (src card8 pixarray)
  3191.         (funcall
  3192.           (symbol-function image-swap-function)
  3193.           src bbuf (index-floor pixarray-start-bit-offset 8) boffset
  3194.           (index-ceiling (index* width bits-per-pixel) 8)
  3195.           (index-floor pixarray-padded-bits-per-line 8)
  3196.           padded-bytes-per-line height image-swap-lsb-first-p))
  3197.       t)))))
  3198.  
  3199. (defun fast-write-pixarray (bbuf boffset pixarray x y width height
  3200.                 padded-bytes-per-line bits-per-pixel
  3201.                 unit byte-lsb-first-p bit-lsb-first-p)
  3202.   (declare (type buffer-bytes bbuf)
  3203.        (type pixarray pixarray)
  3204.        (type card16 x y width height)
  3205.        (type array-index boffset padded-bytes-per-line)
  3206.        (type (member 1 4 8 16 24 32) bits-per-pixel)
  3207.        (type (member 8 16 32) unit)
  3208.        (type boolean byte-lsb-first-p bit-lsb-first-p))
  3209.   (progn bbuf boffset pixarray x y width height padded-bytes-per-line
  3210.      bits-per-pixel unit byte-lsb-first-p bit-lsb-first-p)
  3211.   (or
  3212.     #+(or Genera lcl3.0 excl)
  3213.     (fast-write-pixarray-with-swap
  3214.       bbuf boffset pixarray x y width height padded-bytes-per-line
  3215.       bits-per-pixel unit byte-lsb-first-p bit-lsb-first-p)
  3216.     (let ((function
  3217.         (or #+lispm
  3218.         (and (= (sys:array-element-size pixarray) bits-per-pixel)
  3219.              (zerop (index-mod padded-bytes-per-line 4))
  3220.              (zerop (index-mod
  3221.                   (* #+Genera (sys:array-row-span pixarray)
  3222.                  #-Genera (array-dimension pixarray 1)
  3223.                  bits-per-pixel)
  3224.                   32))
  3225.              #'fast-write-pixarray-using-bitblt)
  3226.         #+(or lcl3.0 excl)
  3227.         (and (index= bits-per-pixel 1)
  3228.              #'fast-write-pixarray-1)
  3229.         #+(or lcl3.0 excl)
  3230.         (and (index= bits-per-pixel 4)
  3231.              #'fast-write-pixarray-4)
  3232.         #+(or Genera lcl3.0 excl)
  3233.         (and (index= bits-per-pixel 24)
  3234.              #'fast-write-pixarray-24))))
  3235.       (when function
  3236.     (write-pixarray-internal
  3237.       bbuf boffset pixarray x y width height padded-bytes-per-line
  3238.       bits-per-pixel function
  3239.       *image-unit* *image-byte-lsb-first-p* *image-bit-lsb-first-p*
  3240.       unit byte-lsb-first-p bit-lsb-first-p)))))
  3241.  
  3242. ;;; FAST-COPY-PIXARRAY - copy part of a pixarray into another
  3243.  
  3244. (defun fast-copy-pixarray (pixarray copy x y width height bits-per-pixel)
  3245.   (declare (type pixarray pixarray copy)
  3246.        (type card16 x y width height)
  3247.        (type (member 1 4 8 16 24 32) bits-per-pixel))
  3248.   (progn pixarray copy x y width height bits-per-pixel nil)
  3249.   (or
  3250.     #+lispm
  3251.     (let* ((pixarray-padded-pixels-per-line
  3252.          #+Genera (sys:array-row-span pixarray)
  3253.          #-Genera (array-dimension pixarray 1))
  3254.        (pixarray-padded-bits-per-line
  3255.          (* pixarray-padded-pixels-per-line bits-per-pixel))
  3256.        (copy-padded-pixels-per-line
  3257.          #+Genera (sys:array-row-span copy)
  3258.          #-Genera (array-dimension copy 1))
  3259.        (copy-padded-bits-per-line
  3260.          (* copy-padded-pixels-per-line bits-per-pixel)))
  3261.       (when (and (= (sys:array-element-size pixarray) bits-per-pixel)
  3262.          (zerop (index-mod pixarray-padded-bits-per-line 32))
  3263.          (zerop (index-mod copy-padded-bits-per-line 32)))
  3264.     (sys:bitblt boole-1 width height pixarray x y copy 0 0)
  3265.     t))
  3266.     #+(or lcl3.0 excl)
  3267.     (unless (index= bits-per-pixel 24)
  3268.       (let ((pixarray-padded-bits-per-line
  3269.           (if (index= height 1) 0
  3270.         (index* (index- (array-row-major-index pixarray 1 0)
  3271.                 (array-row-major-index pixarray 0 0))
  3272.             bits-per-pixel)))
  3273.         (copy-padded-bits-per-line
  3274.           (if (index= height 1) 0
  3275.         (index* (index- (array-row-major-index copy 1 0)
  3276.                 (array-row-major-index copy 0 0))
  3277.             bits-per-pixel)))
  3278.         (pixarray-start-bit-offset
  3279.           (index* (array-row-major-index pixarray y x)
  3280.               bits-per-pixel)))
  3281.     (declare (type array-index pixarray-padded-bits-per-line
  3282.                copy-padded-bits-per-line pixarray-start-bit-offset))
  3283.     (when (if (eq *computed-image-byte-lsb-first-p* *computed-image-bit-lsb-first-p*)
  3284.           (and (index-zerop (index-mod pixarray-padded-bits-per-line 8))
  3285.                (index-zerop (index-mod copy-padded-bits-per-line 8))
  3286.                (index-zerop (index-mod pixarray-start-bit-offset 8)))
  3287.         (and (index-zerop (index-mod pixarray-padded-bits-per-line *image-unit*))
  3288.              (index-zerop (index-mod copy-padded-bits-per-line *image-unit*))
  3289.              (index-zerop (index-mod pixarray-start-bit-offset *image-unit*))))
  3290.       (with-underlying-simple-vector (src card8 pixarray)
  3291.         (with-underlying-simple-vector (dst card8 copy)
  3292.           (image-noswap
  3293.         src dst
  3294.         (index-floor pixarray-start-bit-offset 8) 0
  3295.         (index-ceiling (index* width bits-per-pixel) 8)
  3296.         (index-floor pixarray-padded-bits-per-line 8)
  3297.         (index-floor copy-padded-bits-per-line 8)
  3298.         height nil)))
  3299.       t)))
  3300.     #+(or lcl3.0 excl)
  3301.     (macrolet
  3302.       ((copy (type element-type)
  3303.      `(let ((pixarray pixarray)
  3304.         (copy copy))
  3305.         (declare (type ,type pixarray copy))
  3306.         #.(declare-buffun)
  3307.         (with-underlying-simple-vector (src ,element-type pixarray)
  3308.           (with-underlying-simple-vector (dst ,element-type copy)
  3309.         (do* ((dst-y 0 (index1+ dst-y))
  3310.               (src-y y (index1+ src-y)))
  3311.              ((index>= dst-y height))
  3312.           (declare (type card16 dst-y src-y))
  3313.           (do* ((dst-idx (array-row-major-index copy dst-y 0)
  3314.                  (index1+ dst-idx))
  3315.             (dst-end (index+ dst-idx width))
  3316.             (src-idx (array-row-major-index pixarray src-y x)
  3317.                  (index1+ src-idx)))
  3318.                ((index>= dst-idx dst-end))
  3319.             (declare (type array-index dst-idx src-idx dst-end))
  3320.             (setf (aref dst dst-idx)
  3321.               (the ,element-type (aref src src-idx))))))))))
  3322.       (ecase bits-per-pixel
  3323.     (1  (copy pixarray-1  pixarray-1-element-type))
  3324.     (4  (copy pixarray-4  pixarray-4-element-type))
  3325.     (8  (copy pixarray-8  pixarray-8-element-type))
  3326.     (16 (copy pixarray-16 pixarray-16-element-type))
  3327.     (24 (copy pixarray-24 pixarray-24-element-type))
  3328.     (32 (copy pixarray-32 pixarray-32-element-type)))
  3329.       t)))
  3330.