forked from sharplispers/clx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
excldep.lisp
207 lines (185 loc) · 6.25 KB
/
excldep.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
;;; -*- Mode: common-lisp; Package: xlib; Base: 10; Lowercase: Yes -*-
;;;
;;; CLX -- excldep.cl
;;;
;;; Copyright (c) 1987, 1988, 1989 Franz Inc, Berkeley, Ca.
;;;
;;; Permission is granted to any individual or institution to use, copy,
;;; modify, and distribute this software, provided that this complete
;;; copyright and permission notice is maintained, intact, in all copies and
;;; supporting documentation.
;;;
;;; Franz Incorporated provides this software "as is" without
;;; express or implied warranty.
;;;
(in-package :xlib)
(eval-when (:compile-toplevel :execute :load-toplevel)
(require :foreign)
(require :process) ; Needed even if scheduler is not
; running. (Must be able to make
; a process-lock.)
)
(eval-when (:load-toplevel)
(provide :clx))
#-(or little-endian big-endian)
(eval-when (eval compile load)
(let ((x '#(1)))
(if (not (eq 0 (sys::memref x
#.(sys::mdparam 'comp::md-lvector-data0-norm)
0 :unsigned-byte)))
(pushnew :little-endian *features*)
(pushnew :big-endian *features*))))
(defmacro correct-case (string)
;; This macro converts the given string to the
;; current preferred case, or leaves it alone in a case-sensitive mode.
(let ((str (gensym)))
`(let ((,str ,string))
(case excl::*current-case-mode*
(:case-insensitive-lower
(string-downcase ,str))
(:case-insensitive-upper
(string-upcase ,str))
((:case-sensitive-lower :case-sensitive-upper)
,str)))))
(defconstant type-pred-alist
'((card8 . card8p)
(card16 . card16p)
(card29 . card29p)
(card32 . card32p)
(int8 . int8p)
(int16 . int16p)
(int32 . int32p)
(mask16 . card16p)
(mask32 . card32p)
(pixel . card32p)
(resource-id . card29p)
(keysym . card32p)
(angle . anglep)
(color . color-p)
(bitmap-format . bitmap-format-p)
(pixmap-format . pixmap-format-p)
(display . display-p)
(drawable . drawable-p)
(window . window-p)
(pixmap . pixmap-p)
(visual-info . visual-info-p)
(colormap . colormap-p)
(cursor . cursor-p)
(gcontext . gcontext-p)
(screen . screen-p)
(font . font-p)
(image-x . image-x-p)
(image-xy . image-xy-p)
(image-z . image-z-p)
(wm-hints . wm-hints-p)
(wm-size-hints . wm-size-hints-p)
))
;; This (if (and ...) t nil) stuff has a purpose -- it lets the old
;; sun4 compiler opencode the `and'.
(defun card8p (x)
(declare (optimize (speed 3) (safety 0))
(fixnum x))
(if (and (excl:fixnump x) (> #.(expt 2 8) x) (>= x 0))
t
nil))
(defun card16p (x)
(declare (optimize (speed 3) (safety 0))
(fixnum x))
(if (and (excl:fixnump x) (> #.(expt 2 16) x) (>= x 0))
t
nil))
(defun card29p (x)
(declare (optimize (speed 3) (safety 0)))
(if (or (and (excl:fixnump x) (>= (the fixnum x) 0))
(and (excl:bignump x) (> #.(expt 2 29) (the bignum x))
(>= (the bignum x) 0)))
t
nil))
(defun card32p (x)
(declare (optimize (speed 3) (safety 0)))
(if (or (and (excl:fixnump x) (>= (the fixnum x) 0))
(and (excl:bignump x) (> #.(expt 2 32) (the bignum x))
(>= (the bignum x) 0)))
t
nil))
(defun int8p (x)
(declare (optimize (speed 3) (safety 0))
(fixnum x))
(if (and (excl:fixnump x) (> #.(expt 2 7) x) (>= x #.(expt -2 7)))
t
nil))
(defun int16p (x)
(declare (optimize (speed 3) (safety 0))
(fixnum x))
(if (and (excl:fixnump x) (> #.(expt 2 15) x) (>= x #.(expt -2 15)))
t
nil))
(defun int32p (x)
(declare (optimize (speed 3) (safety 0)))
(if (or (excl:fixnump x)
(and (excl:bignump x) (> #.(expt 2 31) (the bignum x))
(>= (the bignum x) #.(expt -2 31))))
t
nil))
;; This one can be handled better by knowing a little about what we're
;; testing for. Plus this version can handle (single-float pi), which
;; is otherwise larger than pi!
(defun anglep (x)
(declare (optimize (speed 3) (safety 0)))
(if (or (and (excl:fixnump x) (>= (the fixnum x) #.(truncate (* -2 pi)))
(<= (the fixnum x) #.(truncate (* 2 pi))))
(and (excl:single-float-p x)
(>= (the single-float x) #.(float (* -2 pi) 0.0s0))
(<= (the single-float x) #.(float (* 2 pi) 0.0s0)))
(and (excl:double-float-p x)
(>= (the double-float x) #.(float (* -2 pi) 0.0d0))
(<= (the double-float x) #.(float (* 2 pi) 0.0d0))))
t
nil))
(eval-when (:load-toplevel :execute :compile-toplevel)
(mapcar #'(lambda (elt) (excl:add-typep-transformer (car elt) (cdr elt)))
type-pred-alist))
(defun fd-char-avail-p (socket-stream)
(excl:read-no-hang-p socket-stream))
(defun stream-char-avail-p (socket-stream)
(excl:read-no-hang-p socket-stream))
(defmacro with-interrupt-checking-on (&body body)
`(locally (declare (optimize (safety 1)))
,@body))
(defun fd-read-bytes (fd vector start-index length)
;; Read from the given stream fd into 'vector', which has element type card8.
;; Start storing at index 'start-index' and read exactly 'length' bytes.
;; Return t if an error or eof occurred, nil otherwise.
(declare (fixnum start-index length))
(with-interrupt-checking-on
(let ((end-index (+ start-index length)))
(loop
(let ((next-index (excl:read-vector vector fd
:start start-index
:end end-index)))
(excl:if* (eq next-index start-index)
then ; end of file before was all filled up
(return t)
elseif (eq next-index end-index)
then ; we're all done
(return nil)
else (setq start-index next-index)))))))
(defun stream-read-bytes (stream vector start-index length)
;; Read from the given stream fd into 'vector', which has element type card8.
;; Start storing at index 'start-index' and read exactly 'length' bytes.
;; Return t if an error or eof occurred, nil otherwise.
(declare (fixnum start-index length))
(declare (type (or null stream) stream))
(with-interrupt-checking-on
(let ((end-index (+ start-index length)))
(loop
(let ((next-index (excl:read-vector vector stream
:start start-index
:end end-index)))
(excl:if* (eq next-index start-index)
then ; end of file before was all filled up
(return t)
elseif (eq next-index end-index)
then ; we're all done
(return nil)
else (setq start-index next-index)))))))