1 | /***********************************************************
|
---|
2 |
|
---|
3 | Copyright 1987, 1998 The Open Group
|
---|
4 |
|
---|
5 | Permission to use, copy, modify, distribute, and sell this software and its
|
---|
6 | documentation for any purpose is hereby granted without fee, provided that
|
---|
7 | the above copyright notice appear in all copies and that both that
|
---|
8 | copyright notice and this permission notice appear in supporting
|
---|
9 | documentation.
|
---|
10 |
|
---|
11 | The above copyright notice and this permission notice shall be included in
|
---|
12 | all copies or substantial portions of the Software.
|
---|
13 |
|
---|
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
---|
17 | OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
---|
18 | AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
---|
19 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
---|
20 |
|
---|
21 | Except as contained in this notice, the name of The Open Group shall not be
|
---|
22 | used in advertising or otherwise to promote the sale, use or other dealings
|
---|
23 | in this Software without prior written authorization from The Open Group.
|
---|
24 |
|
---|
25 | Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
|
---|
26 |
|
---|
27 | All Rights Reserved
|
---|
28 |
|
---|
29 | Permission to use, copy, modify, and distribute this software and its
|
---|
30 | documentation for any purpose and without fee is hereby granted,
|
---|
31 | provided that the above copyright notice appear in all copies and that
|
---|
32 | both that copyright notice and this permission notice appear in
|
---|
33 | supporting documentation, and that the name of Digital not be
|
---|
34 | used in advertising or publicity pertaining to distribution of the
|
---|
35 | software without specific, written prior permission.
|
---|
36 |
|
---|
37 | DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
|
---|
38 | ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
|
---|
39 | DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
|
---|
40 | ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
---|
41 | WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
---|
42 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
---|
43 | SOFTWARE.
|
---|
44 |
|
---|
45 | Copyright 1992, 1993 Data General Corporation;
|
---|
46 | Copyright 1992, 1993 OMRON Corporation
|
---|
47 |
|
---|
48 | Permission to use, copy, modify, distribute, and sell this software and its
|
---|
49 | documentation for any purpose is hereby granted without fee, provided that the
|
---|
50 | above copyright notice appear in all copies and that both that copyright
|
---|
51 | notice and this permission notice appear in supporting documentation, and that
|
---|
52 | neither the name OMRON or DATA GENERAL be used in advertising or publicity
|
---|
53 | pertaining to distribution of the software without specific, written prior
|
---|
54 | permission of the party whose name is to be used. Neither OMRON or
|
---|
55 | DATA GENERAL make any representation about the suitability of this software
|
---|
56 | for any purpose. It is provided "as is" without express or implied warranty.
|
---|
57 |
|
---|
58 | OMRON AND DATA GENERAL EACH DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
|
---|
59 | SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
|
---|
60 | IN NO EVENT SHALL OMRON OR DATA GENERAL BE LIABLE FOR ANY SPECIAL, INDIRECT
|
---|
61 | OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
|
---|
62 | DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
---|
63 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
---|
64 | OF THIS SOFTWARE.
|
---|
65 |
|
---|
66 | ******************************************************************/
|
---|
67 | #ifndef MISC_H
|
---|
68 | #define MISC_H 1
|
---|
69 | /*
|
---|
70 | * X internal definitions
|
---|
71 | *
|
---|
72 | */
|
---|
73 |
|
---|
74 | #include <X11/Xosdefs.h>
|
---|
75 | #include <X11/Xfuncproto.h>
|
---|
76 | #include <X11/Xmd.h>
|
---|
77 | #include <X11/X.h>
|
---|
78 | #include <X11/Xdefs.h>
|
---|
79 |
|
---|
80 | #include <stddef.h>
|
---|
81 | #include <stdint.h>
|
---|
82 |
|
---|
83 | #ifndef MAXSCREENS
|
---|
84 | #define MAXSCREENS 16
|
---|
85 | #endif
|
---|
86 | #ifndef MAXGPUSCREENS
|
---|
87 | #define MAXGPUSCREENS 16
|
---|
88 | #endif
|
---|
89 | #define MAXCLIENTS 256
|
---|
90 | #define MAXEXTENSIONS 128
|
---|
91 | #define MAXFORMATS 8
|
---|
92 | #define MAXDEVICES 40 /* input devices */
|
---|
93 | #define GPU_SCREEN_OFFSET 256
|
---|
94 |
|
---|
95 | /* 128 event opcodes for core + extension events, excluding GE */
|
---|
96 | #define MAXEVENTS 128
|
---|
97 | #define EXTENSION_EVENT_BASE 64
|
---|
98 | #define EXTENSION_BASE 128
|
---|
99 |
|
---|
100 | typedef uint32_t ATOM;
|
---|
101 |
|
---|
102 | #ifndef TRUE
|
---|
103 | #define TRUE 1
|
---|
104 | #define FALSE 0
|
---|
105 | #endif
|
---|
106 |
|
---|
107 | #ifndef _XTYPEDEF_CALLBACKLISTPTR
|
---|
108 | typedef struct _CallbackList *CallbackListPtr; /* also in dix.h */
|
---|
109 |
|
---|
110 | #define _XTYPEDEF_CALLBACKLISTPTR
|
---|
111 | #endif
|
---|
112 |
|
---|
113 | typedef struct _xReq *xReqPtr;
|
---|
114 |
|
---|
115 | #include "os.h" /* for ALLOCATE_LOCAL and DEALLOCATE_LOCAL */
|
---|
116 | #include <X11/Xfuncs.h> /* for bcopy, bzero, and bcmp */
|
---|
117 |
|
---|
118 | #define NullBox ((BoxPtr)0)
|
---|
119 | #define MILLI_PER_MIN (1000 * 60)
|
---|
120 | #define MILLI_PER_SECOND (1000)
|
---|
121 |
|
---|
122 | /* this next is used with None and ParentRelative to tell
|
---|
123 | PaintWin() what to use to paint the background. Also used
|
---|
124 | in the macro IS_VALID_PIXMAP */
|
---|
125 |
|
---|
126 | #define USE_BACKGROUND_PIXEL 3
|
---|
127 | #define USE_BORDER_PIXEL 3
|
---|
128 |
|
---|
129 | /* byte swap a 32-bit literal */
|
---|
130 | static inline uint32_t
|
---|
131 | lswapl(uint32_t x)
|
---|
132 | {
|
---|
133 | return ((x & 0xff) << 24) |
|
---|
134 | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | ((x >> 24) & 0xff);
|
---|
135 | }
|
---|
136 |
|
---|
137 | /* byte swap a 16-bit literal */
|
---|
138 | static inline uint16_t
|
---|
139 | lswaps(uint16_t x)
|
---|
140 | {
|
---|
141 | return ((x & 0xff) << 8) | ((x >> 8) & 0xff);
|
---|
142 | }
|
---|
143 |
|
---|
144 | #undef min
|
---|
145 | #undef max
|
---|
146 |
|
---|
147 | #define min(a, b) (((a) < (b)) ? (a) : (b))
|
---|
148 | #define max(a, b) (((a) > (b)) ? (a) : (b))
|
---|
149 | /* abs() is a function, not a macro; include the file declaring
|
---|
150 | * it in case we haven't done that yet.
|
---|
151 | */
|
---|
152 | #include <stdlib.h>
|
---|
153 | #define sign(x) ((x) < 0 ? -1 : ((x) > 0 ? 1 : 0))
|
---|
154 | /* this assumes b > 0 */
|
---|
155 | #define modulus(a, b, d) if (((d) = (a) % (b)) < 0) (d) += (b)
|
---|
156 | /*
|
---|
157 | * return the least significant bit in x which is set
|
---|
158 | *
|
---|
159 | * This works on 1's complement and 2's complement machines.
|
---|
160 | * If you care about the extra instruction on 2's complement
|
---|
161 | * machines, change to ((x) & (-(x)))
|
---|
162 | */
|
---|
163 | #define lowbit(x) ((x) & (~(x) + 1))
|
---|
164 |
|
---|
165 | /* XXX Not for modules */
|
---|
166 | #include <limits.h>
|
---|
167 | #if !defined(MAXSHORT) || !defined(MINSHORT) || \
|
---|
168 | !defined(MAXINT) || !defined(MININT)
|
---|
169 | /*
|
---|
170 | * Some implementations #define these through <math.h>, so preclude
|
---|
171 | * #include'ing it later.
|
---|
172 | */
|
---|
173 |
|
---|
174 | #include <math.h>
|
---|
175 | #undef MAXSHORT
|
---|
176 | #define MAXSHORT SHRT_MAX
|
---|
177 | #undef MINSHORT
|
---|
178 | #define MINSHORT SHRT_MIN
|
---|
179 | #undef MAXINT
|
---|
180 | #define MAXINT INT_MAX
|
---|
181 | #undef MININT
|
---|
182 | #define MININT INT_MIN
|
---|
183 |
|
---|
184 | #include <assert.h>
|
---|
185 | #include <ctype.h>
|
---|
186 | #include <stdio.h> /* for fopen, etc... */
|
---|
187 |
|
---|
188 | #endif
|
---|
189 |
|
---|
190 | #ifndef PATH_MAX
|
---|
191 | #include <sys/param.h>
|
---|
192 | #ifndef PATH_MAX
|
---|
193 | #ifdef MAXPATHLEN
|
---|
194 | #define PATH_MAX MAXPATHLEN
|
---|
195 | #else
|
---|
196 | #define PATH_MAX 1024
|
---|
197 | #endif
|
---|
198 | #endif
|
---|
199 | #endif
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Calculate the number of bytes needed to hold bits.
|
---|
203 | * @param bits The minimum number of bits needed.
|
---|
204 | * @return The number of bytes needed to hold bits.
|
---|
205 | */
|
---|
206 | static inline int
|
---|
207 | bits_to_bytes(const int bits)
|
---|
208 | {
|
---|
209 | return ((bits + 7) >> 3);
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Calculate the number of 4-byte units needed to hold the given number of
|
---|
214 | * bytes.
|
---|
215 | * @param bytes The minimum number of bytes needed.
|
---|
216 | * @return The number of 4-byte units needed to hold bytes.
|
---|
217 | */
|
---|
218 | static inline int
|
---|
219 | bytes_to_int32(const int bytes)
|
---|
220 | {
|
---|
221 | return (((bytes) + 3) >> 2);
|
---|
222 | }
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Calculate the number of bytes (in multiples of 4) needed to hold bytes.
|
---|
226 | * @param bytes The minimum number of bytes needed.
|
---|
227 | * @return The closest multiple of 4 that is equal or higher than bytes.
|
---|
228 | */
|
---|
229 | static inline int
|
---|
230 | pad_to_int32(const int bytes)
|
---|
231 | {
|
---|
232 | return (((bytes) + 3) & ~3);
|
---|
233 | }
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Calculate padding needed to bring the number of bytes to an even
|
---|
237 | * multiple of 4.
|
---|
238 | * @param bytes The minimum number of bytes needed.
|
---|
239 | * @return The bytes of padding needed to arrive at the closest multiple of 4
|
---|
240 | * that is equal or higher than bytes.
|
---|
241 | */
|
---|
242 | static inline int
|
---|
243 | padding_for_int32(const int bytes)
|
---|
244 | {
|
---|
245 | return ((-bytes) & 3);
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 | extern char **xstrtokenize(const char *str, const char *separators);
|
---|
250 | extern void FormatInt64(int64_t num, char *string);
|
---|
251 | extern void FormatUInt64(uint64_t num, char *string);
|
---|
252 | extern void FormatUInt64Hex(uint64_t num, char *string);
|
---|
253 | extern void FormatDouble(double dbl, char *string);
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Compare the two version numbers comprising of major.minor.
|
---|
257 | *
|
---|
258 | * @return A value less than 0 if a is less than b, 0 if a is equal to b,
|
---|
259 | * or a value greater than 0
|
---|
260 | */
|
---|
261 | static inline int
|
---|
262 | version_compare(uint32_t a_major, uint32_t a_minor,
|
---|
263 | uint32_t b_major, uint32_t b_minor)
|
---|
264 | {
|
---|
265 | if (a_major > b_major)
|
---|
266 | return 1;
|
---|
267 | if (a_major < b_major)
|
---|
268 | return -1;
|
---|
269 | if (a_minor > b_minor)
|
---|
270 | return 1;
|
---|
271 | if (a_minor < b_minor)
|
---|
272 | return -1;
|
---|
273 |
|
---|
274 | return 0;
|
---|
275 | }
|
---|
276 |
|
---|
277 | /* some macros to help swap requests, replies, and events */
|
---|
278 |
|
---|
279 | #define LengthRestB(stuff) \
|
---|
280 | ((client->req_len << 2) - sizeof(*stuff))
|
---|
281 |
|
---|
282 | #define LengthRestS(stuff) \
|
---|
283 | ((client->req_len << 1) - (sizeof(*stuff) >> 1))
|
---|
284 |
|
---|
285 | #define LengthRestL(stuff) \
|
---|
286 | (client->req_len - (sizeof(*stuff) >> 2))
|
---|
287 |
|
---|
288 | #define SwapRestS(stuff) \
|
---|
289 | SwapShorts((short *)(stuff + 1), LengthRestS(stuff))
|
---|
290 |
|
---|
291 | #define SwapRestL(stuff) \
|
---|
292 | SwapLongs((CARD32 *)(stuff + 1), LengthRestL(stuff))
|
---|
293 |
|
---|
294 | #if defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
|
---|
295 | void __attribute__ ((error("wrong sized variable passed to swap")))
|
---|
296 | wrong_size(void);
|
---|
297 | #else
|
---|
298 | static inline void
|
---|
299 | wrong_size(void)
|
---|
300 | {
|
---|
301 | }
|
---|
302 | #endif
|
---|
303 |
|
---|
304 | #if !(defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590)))
|
---|
305 | static inline int
|
---|
306 | __builtin_constant_p(int x)
|
---|
307 | {
|
---|
308 | return 0;
|
---|
309 | }
|
---|
310 | #endif
|
---|
311 |
|
---|
312 | /* byte swap a 64-bit value */
|
---|
313 | static inline void
|
---|
314 | swap_uint64(uint64_t *x)
|
---|
315 | {
|
---|
316 | char n;
|
---|
317 |
|
---|
318 | n = ((char *) x)[0];
|
---|
319 | ((char *) x)[0] = ((char *) x)[7];
|
---|
320 | ((char *) x)[7] = n;
|
---|
321 |
|
---|
322 | n = ((char *) x)[1];
|
---|
323 | ((char *) x)[1] = ((char *) x)[6];
|
---|
324 | ((char *) x)[6] = n;
|
---|
325 |
|
---|
326 | n = ((char *) x)[2];
|
---|
327 | ((char *) x)[2] = ((char *) x)[5];
|
---|
328 | ((char *) x)[5] = n;
|
---|
329 |
|
---|
330 | n = ((char *) x)[3];
|
---|
331 | ((char *) x)[3] = ((char *) x)[4];
|
---|
332 | ((char *) x)[4] = n;
|
---|
333 | }
|
---|
334 |
|
---|
335 | #define swapll(x) do { \
|
---|
336 | if (sizeof(*(x)) != 8) \
|
---|
337 | wrong_size(); \
|
---|
338 | swap_uint64((uint64_t *)(x)); \
|
---|
339 | } while (0)
|
---|
340 |
|
---|
341 | /* byte swap a 32-bit value */
|
---|
342 | static inline void
|
---|
343 | swap_uint32(uint32_t * x)
|
---|
344 | {
|
---|
345 | char n = ((char *) x)[0];
|
---|
346 |
|
---|
347 | ((char *) x)[0] = ((char *) x)[3];
|
---|
348 | ((char *) x)[3] = n;
|
---|
349 | n = ((char *) x)[1];
|
---|
350 | ((char *) x)[1] = ((char *) x)[2];
|
---|
351 | ((char *) x)[2] = n;
|
---|
352 | }
|
---|
353 |
|
---|
354 | #define swapl(x) do { \
|
---|
355 | if (sizeof(*(x)) != 4) \
|
---|
356 | wrong_size(); \
|
---|
357 | if (__builtin_constant_p((uintptr_t)(x) & 3) && ((uintptr_t)(x) & 3) == 0) \
|
---|
358 | *(x) = lswapl(*(x)); \
|
---|
359 | else \
|
---|
360 | swap_uint32((uint32_t *)(x)); \
|
---|
361 | } while (0)
|
---|
362 |
|
---|
363 | /* byte swap a 16-bit value */
|
---|
364 | static inline void
|
---|
365 | swap_uint16(uint16_t * x)
|
---|
366 | {
|
---|
367 | char n = ((char *) x)[0];
|
---|
368 |
|
---|
369 | ((char *) x)[0] = ((char *) x)[1];
|
---|
370 | ((char *) x)[1] = n;
|
---|
371 | }
|
---|
372 |
|
---|
373 | #define swaps(x) do { \
|
---|
374 | if (sizeof(*(x)) != 2) \
|
---|
375 | wrong_size(); \
|
---|
376 | if (__builtin_constant_p((uintptr_t)(x) & 1) && ((uintptr_t)(x) & 1) == 0) \
|
---|
377 | *(x) = lswaps(*(x)); \
|
---|
378 | else \
|
---|
379 | swap_uint16((uint16_t *)(x)); \
|
---|
380 | } while (0)
|
---|
381 |
|
---|
382 | /* copy 32-bit value from src to dst byteswapping on the way */
|
---|
383 | #define cpswapl(src, dst) do { \
|
---|
384 | if (sizeof((src)) != 4 || sizeof((dst)) != 4) \
|
---|
385 | wrong_size(); \
|
---|
386 | (dst) = lswapl((src)); \
|
---|
387 | } while (0)
|
---|
388 |
|
---|
389 | /* copy short from src to dst byteswapping on the way */
|
---|
390 | #define cpswaps(src, dst) do { \
|
---|
391 | if (sizeof((src)) != 2 || sizeof((dst)) != 2) \
|
---|
392 | wrong_size(); \
|
---|
393 | (dst) = lswaps((src)); \
|
---|
394 | } while (0)
|
---|
395 |
|
---|
396 | extern _X_EXPORT void SwapLongs(CARD32 *list, unsigned long count);
|
---|
397 |
|
---|
398 | extern _X_EXPORT void SwapShorts(short *list, unsigned long count);
|
---|
399 |
|
---|
400 | extern _X_EXPORT void MakePredeclaredAtoms(void);
|
---|
401 |
|
---|
402 | extern _X_EXPORT int Ones(unsigned long /*mask */ );
|
---|
403 |
|
---|
404 | typedef struct _xPoint *DDXPointPtr;
|
---|
405 | typedef struct pixman_box16 *BoxPtr;
|
---|
406 | typedef struct _xEvent *xEventPtr;
|
---|
407 | typedef struct _xRectangle *xRectanglePtr;
|
---|
408 | typedef struct _GrabRec *GrabPtr;
|
---|
409 |
|
---|
410 | /* typedefs from other places - duplicated here to minimize the amount
|
---|
411 | * of unnecessary junk that one would normally have to include to get
|
---|
412 | * these symbols defined
|
---|
413 | */
|
---|
414 |
|
---|
415 | #ifndef _XTYPEDEF_CHARINFOPTR
|
---|
416 | typedef struct _CharInfo *CharInfoPtr; /* also in fonts/include/font.h */
|
---|
417 |
|
---|
418 | #define _XTYPEDEF_CHARINFOPTR
|
---|
419 | #endif
|
---|
420 |
|
---|
421 | extern _X_EXPORT unsigned long globalSerialNumber;
|
---|
422 | extern _X_EXPORT unsigned long serverGeneration;
|
---|
423 |
|
---|
424 | /* Don't use this directly, use BUG_WARN or BUG_WARN_MSG instead */
|
---|
425 | #define __BUG_WARN_MSG(cond, with_msg, ...) \
|
---|
426 | do { if (cond) { \
|
---|
427 | ErrorFSigSafe("BUG: triggered 'if (" #cond ")'\n"); \
|
---|
428 | ErrorFSigSafe("BUG: %s:%u in %s()\n", \
|
---|
429 | __FILE__, __LINE__, __func__); \
|
---|
430 | if (with_msg) ErrorFSigSafe(__VA_ARGS__); \
|
---|
431 | xorg_backtrace(); \
|
---|
432 | } } while(0)
|
---|
433 |
|
---|
434 | #define BUG_WARN_MSG(cond, ...) \
|
---|
435 | __BUG_WARN_MSG(cond, 1, __VA_ARGS__)
|
---|
436 |
|
---|
437 | #define BUG_WARN(cond) __BUG_WARN_MSG(cond, 0, NULL)
|
---|
438 |
|
---|
439 | #define BUG_RETURN(cond) \
|
---|
440 | do { if (cond) { __BUG_WARN_MSG(cond, 0, NULL); return; } } while(0)
|
---|
441 |
|
---|
442 | #define BUG_RETURN_MSG(cond, ...) \
|
---|
443 | do { if (cond) { __BUG_WARN_MSG(cond, 1, __VA_ARGS__); return; } } while(0)
|
---|
444 |
|
---|
445 | #define BUG_RETURN_VAL(cond, val) \
|
---|
446 | do { if (cond) { __BUG_WARN_MSG(cond, 0, NULL); return (val); } } while(0)
|
---|
447 |
|
---|
448 | #define BUG_RETURN_VAL_MSG(cond, val, ...) \
|
---|
449 | do { if (cond) { __BUG_WARN_MSG(cond, 1, __VA_ARGS__); return (val); } } while(0)
|
---|
450 |
|
---|
451 | #endif /* MISC_H */
|
---|