Debian Codegraph @

nx-X11/include/Xpoll.h.in

raw
1 /**************************************************************************/
2 /* */
3 /* Copyright (c) 2001, 2011 NoMachine (http://www.nomachine.com) */
4 /* Copyright (c) 2008-2017 Oleksandr Shneyder <[email protected]> */
5 /* Copyright (c) 2011-2022 Mike Gabriel <[email protected]>*/
6 /* Copyright (c) 2014-2019 Mihai Moldovan <[email protected]> */
7 /* Copyright (c) 2014-2022 Ulrich Sibiller <[email protected]> */
8 /* Copyright (c) 2015-2016 Qindel Group (http://www.qindel.com) */
9 /* */
10 /* nx-X11, NX protocol compression and NX extensions to this software */
11 /* are copyright of the aforementioned persons and companies. */
12 /* */
13 /* Redistribution and use of the present software is allowed according */
14 /* to terms specified in the file LICENSE which comes in the source */
15 /* distribution. */
16 /* */
17 /* All rights reserved. */
18 /* */
19 /**************************************************************************/
20
21 /*
22
23 Copyright 1994, 1998 The Open Group
24
25 Permission to use, copy, modify, distribute, and sell this software and its
26 documentation for any purpose is hereby granted without fee, provided that
27 the above copyright notice appear in all copies and that both that
28 copyright notice and this permission notice appear in supporting
29 documentation.
30
31 The above copyright notice and this permission notice shall be included
32 in all copies or substantial portions of the Software.
33
34 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
35 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
36 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
37 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
38 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
39 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
40 OTHER DEALINGS IN THE SOFTWARE.
41
42 Except as contained in this notice, the name of The Open Group shall
43 not be used in advertising or otherwise to promote the sale, use or
44 other dealings in this Software without prior written authorization
45 from The Open Group.
46
47 */
48
49 /*
50 * Copyright © 2005 Daniel Stone
51 *
52 * Permission to use, copy, modify, distribute, and sell this software and its
53 * documentation for any purpose is hereby granted without fee, provided that
54 * the above copyright notice appear in all copies and that both that
55 * copyright notice and this permission notice appear in supporting
56 * documentation, and that the name of Daniel Stone not be used in advertising
57 * or publicity pertaining to distribution of the software without specific,
58 * written prior permission. Daniel Stone makes no representations about the
59 * suitability of this software for any purpose. It is provided "as is"
60 * without express or implied warranty.
61 *
62 * DANIEL STONE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
63 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
64 * DANIEL STONE BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
65 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
66 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
67 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
68 */
69
70 #ifndef _XPOLL_H_
71 #define _XPOLL_H_
72
73 #ifndef WIN32
74
75 #ifndef USE_POLL
76
77 #include <nx-X11/Xos.h>
78
79 /* Below is the monster branch from hell. Basically, most systems will drop to
80 * 'the branch below is the fallthrough for halfway modern systems', and include
81 * <sys/select.h>, so we get the FD_* macros. */
82
83 #if defined(SVR4) && !defined(FD_SETSIZE)
84 # include <sys/select.h>
85 # ifdef luna
86 # include <sysent.h>
87 # endif
88 #else /* not SVR4 */
89 # include <sys/select.h>
90 #endif
91
92 #include <nx-X11/Xmd.h>
93
94 #ifdef CSRG_BASED
95 #include <sys/param.h>
96 # if BSD < 199103
97 typedef long fd_mask;
98 # endif
99 #endif
100
101 #define XFD_SETSIZE 256
102
103 #ifndef FD_SETSIZE
104 #define FD_SETSIZE XFD_SETSIZE
105 #endif
106
107 #ifndef NBBY
108 #define NBBY 8 /* number of bits in a byte */
109 #endif
110
111 #ifndef NFDBITS
112 #define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
113 #endif
114
115 #ifndef howmany
116 #define howmany(x,y) (((x)+((y)-1))/(y))
117 #endif
118
119 #if defined(BSD) && BSD < 198911 && !defined(luna)
120 typedef struct fd_set {
121 fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
122 } fd_set;
123 #endif
124
125 /*
126 * Replace the standard Select with a version giving NX a
127 * chance to check its own descriptors. This doesn't cover
128 * the cases where the system is using poll or when system-
129 * specific defines override the Select definition (OS/2).
130 * See XlibInt.c for _XSelect().
131 */
132
133 #ifdef NX_TRANS_SOCKET
134
135 extern int _XSelect(int maxfds, fd_set *readfds, fd_set *writefds,
136 fd_set *exceptfds, struct timeval *timeout);
137
138 #define Select(n,r,w,e,t) _XSelect(n,(fd_set*)r,(fd_set*)w,(fd_set*)e,(struct timeval*)t)
139
140 #else /* #ifdef NX_TRANS_SOCKET */
141
142 #define Select(n,r,w,e,t) select(n,(fd_set*)r,(fd_set*)w,(fd_set*)e,(struct timeval*)t)
143
144 #endif /* #ifdef NX_TRANS_SOCKET */
145
146 #define __X_FDS_BITS @USE_FDS_BITS@
147
148 #ifndef __FDS_BITS
149 # define __FDS_BITS(p) ((p)->__X_FDS_BITS)
150 #endif
151
152 #define __XFDS_BITS(p, n) (__FDS_BITS(p))[n]
153
154 #ifndef FD_SET
155 #define FD_SET(n, p) (__XFDS_BITS(p, ((n)/NFDBITS)) |= ((fd_mask)1 << ((n) % NFDBITS)))
156 #endif
157 #ifndef FD_CLR
158 #define FD_CLR(n, p) (__XFDS_BITS((p), ((n)/NFDBITS)) &= ~((fd_mask)1 << ((n) % NFDBITS)))
159 #endif
160 #ifndef FD_ISSET
161 #define FD_ISSET(n, p) ((__XFDS_BITS((p), ((n)/NFDBITS))) & ((fd_mask)1 << ((n) % NFDBITS)))
162 #endif
163 #ifndef FD_ZERO
164 #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p)))
165 #endif
166
167 /*
168 * The howmany(FD_SETSIZE, NFDBITS) computes the number of elements in the
169 * array. before accessing an element in the array we check it exists.
170 * If it does not exist then the compiler discards the code to access it.
171 */
172 #define XFD_ANYSET(p) \
173 ((howmany(FD_SETSIZE, NFDBITS) > 0 && (__XFDS_BITS(p, 0))) || \
174 (howmany(FD_SETSIZE, NFDBITS) > 1 && (__XFDS_BITS(p, 1))) || \
175 (howmany(FD_SETSIZE, NFDBITS) > 2 && (__XFDS_BITS(p, 2))) || \
176 (howmany(FD_SETSIZE, NFDBITS) > 3 && (__XFDS_BITS(p, 3))) || \
177 (howmany(FD_SETSIZE, NFDBITS) > 4 && (__XFDS_BITS(p, 4))) || \
178 (howmany(FD_SETSIZE, NFDBITS) > 5 && (__XFDS_BITS(p, 5))) || \
179 (howmany(FD_SETSIZE, NFDBITS) > 6 && (__XFDS_BITS(p, 6))) || \
180 (howmany(FD_SETSIZE, NFDBITS) > 7 && (__XFDS_BITS(p, 7))))
181
182 #define XFD_COPYSET(src,dst) { \
183 int __i__; \
184 for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
185 __XFDS_BITS((dst), __i__) = __XFDS_BITS((src), __i__); \
186 }
187 #define XFD_ANDSET(dst,b1,b2) { \
188 int __i__; \
189 for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
190 __XFDS_BITS((dst), __i__) = ((__XFDS_BITS((b1), __i__)) & (__XFDS_BITS((b2), __i__))); \
191 }
192 #define XFD_ORSET(dst,b1,b2) { \
193 int __i__; \
194 for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
195 __XFDS_BITS((dst), __i__) = ((__XFDS_BITS((b1), __i__)) | (__XFDS_BITS((b2), __i__))); \
196 }
197 #define XFD_UNSET(dst,b1) { \
198 int __i__; \
199 for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
200 __XFDS_BITS((dst), __i__) &= ~(__XFDS_BITS((b1), __i__)); \
201 }
202
203 #else /* USE_POLL */
204 #include <sys/poll.h>
205 #endif /* USE_POLL */
206
207 #else /* WIN32 */
208
209 #define XFD_SETSIZE 256
210 #ifndef FD_SETSIZE
211 #define FD_SETSIZE XFD_SETSIZE
212 #endif
213 #include <nx-X11/Xwinsock.h>
214
215 #define Select(n,r,w,e,t) select(0,(fd_set*)r,(fd_set*)w,(fd_set*)e,(struct timeval*)t)
216
217 #define XFD_SETCOUNT(p) (((fd_set FAR *)(p))->fd_count)
218 #define XFD_FD(p,i) (((fd_set FAR *)(p))->fd_array[i])
219 #define XFD_ANYSET(p) XFD_SETCOUNT(p)
220
221 #define XFD_COPYSET(src,dst) { \
222 u_int __i; \
223 FD_ZERO(dst); \
224 for (__i = 0; __i < XFD_SETCOUNT(src) ; __i++) { \
225 XFD_FD(dst,__i) = XFD_FD(src,__i); \
226 } \
227 XFD_SETCOUNT(dst) = XFD_SETCOUNT(src); \
228 }
229
230 #define XFD_ANDSET(dst,b1,b2) { \
231 u_int __i; \
232 FD_ZERO(dst); \
233 for (__i = 0; __i < XFD_SETCOUNT(b1) ; __i++) { \
234 if (FD_ISSET(XFD_FD(b1,__i), b2)) \
235 FD_SET(XFD_FD(b1,__i), dst); \
236 } \
237 }
238
239 #define XFD_ORSET(dst,b1,b2) { \
240 u_int __i; \
241 if (dst != b1) XFD_COPYSET(b1,dst); \
242 for (__i = 0; __i < XFD_SETCOUNT(b2) ; __i++) { \
243 if (!FD_ISSET(XFD_FD(b2,__i), dst)) \
244 FD_SET(XFD_FD(b2,__i), dst); \
245 } \
246 }
247
248 /* this one is really sub-optimal */
249 #define XFD_UNSET(dst,b1) { \
250 u_int __i; \
251 for (__i = 0; __i < XFD_SETCOUNT(b1) ; __i++) { \
252 FD_CLR(XFD_FD(b1,__i), dst); \
253 } \
254 }
255
256 /* we have to pay the price of having an array here, unlike with bitmasks
257 calling twice FD_SET with the same fd is not transparent, so be careful */
258 #undef FD_SET
259 #define FD_SET(fd,set) do { \
260 if (XFD_SETCOUNT(set) < FD_SETSIZE && !FD_ISSET(fd,set)) \
261 XFD_FD(set,XFD_SETCOUNT(set)++)=(fd); \
262 } while(0)
263
264 #define getdtablesize() FD_SETSIZE
265
266 #endif /* WIN32 */
267
268 #endif /* _XPOLL_H_ */