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 #include <pthread.h>
83 
84 #ifndef MAXSCREENS
85 #define MAXSCREENS	16
86 #endif
87 #ifndef MAXGPUSCREENS
88 #define MAXGPUSCREENS	16
89 #endif
90 #define MAXCLIENTS	2048
91 #define LIMITCLIENTS	256     /* Must be a power of 2 and <= MAXCLIENTS */
92 #define MAXEXTENSIONS   128
93 #define MAXFORMATS	8
94 #define MAXDEVICES	256      /* input devices */
95 #define GPU_SCREEN_OFFSET 256
96 
97 /* 128 event opcodes for core + extension events, excluding GE */
98 #define MAXEVENTS       128
99 #define EXTENSION_EVENT_BASE 64
100 #define EXTENSION_BASE 128
101 
102 typedef uint32_t ATOM;
103 
104 #ifndef TRUE
105 #define TRUE 1
106 #define FALSE 0
107 #endif
108 
109 #ifndef _XTYPEDEF_CALLBACKLISTPTR
110 typedef struct _CallbackList *CallbackListPtr;  /* also in dix.h */
111 
112 #define _XTYPEDEF_CALLBACKLISTPTR
113 #endif
114 
115 typedef struct _xReq *xReqPtr;
116 
117 #include "os.h"                 /* for ALLOCATE_LOCAL and DEALLOCATE_LOCAL */
118 #include <X11/Xfuncs.h>         /* for bcopy, bzero, and bcmp */
119 
120 #define NullBox ((BoxPtr)0)
121 #define MILLI_PER_MIN (1000 * 60)
122 #define MILLI_PER_SECOND (1000)
123 
124     /* this next is used with None and ParentRelative to tell
125        PaintWin() what to use to paint the background. Also used
126        in the macro IS_VALID_PIXMAP */
127 
128 #define USE_BACKGROUND_PIXEL 3
129 #define USE_BORDER_PIXEL 3
130 
131 #undef min
132 #undef max
133 
134 #define min(a, b) (((a) < (b)) ? (a) : (b))
135 #define max(a, b) (((a) > (b)) ? (a) : (b))
136 /* abs() is a function, not a macro; include the file declaring
137  * it in case we haven't done that yet.
138  */
139 #include <stdlib.h>
140 #define sign(x) ((x) < 0 ? -1 : ((x) > 0 ? 1 : 0))
141 /* this assumes b > 0 */
142 #define modulus(a, b, d)    if (((d) = (a) % (b)) < 0) (d) += (b)
143 /*
144  * return the least significant bit in x which is set
145  *
146  * This works on 1's complement and 2's complement machines.
147  * If you care about the extra instruction on 2's complement
148  * machines, change to ((x) & (-(x)))
149  */
150 #define lowbit(x) ((x) & (~(x) + 1))
151 
152 /* XXX Not for modules */
153 #include <limits.h>
154 #if !defined(MAXSHORT) || !defined(MINSHORT) || \
155     !defined(MAXINT) || !defined(MININT)
156 /*
157  * Some implementations #define these through <math.h>, so preclude
158  * #include'ing it later.
159  */
160 
161 #include <math.h>
162 #undef MAXSHORT
163 #define MAXSHORT SHRT_MAX
164 #undef MINSHORT
165 #define MINSHORT SHRT_MIN
166 #undef MAXINT
167 #define MAXINT INT_MAX
168 #undef MININT
169 #define MININT INT_MIN
170 
171 #include <assert.h>
172 #include <ctype.h>
173 #include <stdio.h>              /* for fopen, etc... */
174 
175 #endif
176 
177 #ifndef PATH_MAX
178 #include <sys/param.h>
179 #ifndef PATH_MAX
180 #ifdef MAXPATHLEN
181 #define PATH_MAX MAXPATHLEN
182 #else
183 #define PATH_MAX 1024
184 #endif
185 #endif
186 #endif
187 
188 /**
189  * Calculate the number of bytes needed to hold bits.
190  * @param bits The minimum number of bits needed.
191  * @return The number of bytes needed to hold bits.
192  */
193 static inline int
bits_to_bytes(const int bits)194 bits_to_bytes(const int bits)
195 {
196     return ((bits + 7) >> 3);
197 }
198 
199 /**
200  * Calculate the number of 4-byte units needed to hold the given number of
201  * bytes.
202  * @param bytes The minimum number of bytes needed.
203  * @return The number of 4-byte units needed to hold bytes.
204  */
205 static inline int
bytes_to_int32(const int bytes)206 bytes_to_int32(const int bytes)
207 {
208     return (((bytes) + 3) >> 2);
209 }
210 
211 /**
212  * Calculate the number of bytes (in multiples of 4) needed to hold bytes.
213  * @param bytes The minimum number of bytes needed.
214  * @return The closest multiple of 4 that is equal or higher than bytes.
215  */
216 static inline int
pad_to_int32(const int bytes)217 pad_to_int32(const int bytes)
218 {
219     return (((bytes) + 3) & ~3);
220 }
221 
222 /**
223  * Calculate padding needed to bring the number of bytes to an even
224  * multiple of 4.
225  * @param bytes The minimum number of bytes needed.
226  * @return The bytes of padding needed to arrive at the closest multiple of 4
227  * that is equal or higher than bytes.
228  */
229 static inline int
padding_for_int32(const int bytes)230 padding_for_int32(const int bytes)
231 {
232     return ((-bytes) & 3);
233 }
234 
235 
236 extern _X_EXPORT char **xstrtokenize(const char *str, const char *separators);
237 extern void FormatInt64(int64_t num, char *string);
238 extern void FormatUInt64(uint64_t num, char *string);
239 extern void FormatUInt64Hex(uint64_t num, char *string);
240 extern void FormatDouble(double dbl, char *string);
241 
242 /**
243  * Compare the two version numbers comprising of major.minor.
244  *
245  * @return A value less than 0 if a is less than b, 0 if a is equal to b,
246  * or a value greater than 0
247  */
248 static inline int
version_compare(uint32_t a_major,uint32_t a_minor,uint32_t b_major,uint32_t b_minor)249 version_compare(uint32_t a_major, uint32_t a_minor,
250                 uint32_t b_major, uint32_t b_minor)
251 {
252     if (a_major > b_major)
253         return 1;
254     if (a_major < b_major)
255         return -1;
256     if (a_minor > b_minor)
257         return 1;
258     if (a_minor < b_minor)
259         return -1;
260 
261     return 0;
262 }
263 
264 /* some macros to help swap requests, replies, and events */
265 
266 #define LengthRestB(stuff) \
267     ((client->req_len << 2) - sizeof(*stuff))
268 
269 #define LengthRestS(stuff) \
270     ((client->req_len << 1) - (sizeof(*stuff) >> 1))
271 
272 #define LengthRestL(stuff) \
273     (client->req_len - (sizeof(*stuff) >> 2))
274 
275 #define SwapRestS(stuff) \
276     SwapShorts((short *)(stuff + 1), LengthRestS(stuff))
277 
278 #define SwapRestL(stuff) \
279     SwapLongs((CARD32 *)(stuff + 1), LengthRestL(stuff))
280 
281 #if defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
282 void __attribute__ ((error("wrong sized variable passed to swap")))
283 wrong_size(void);
284 #else
285 static inline void
wrong_size(void)286 wrong_size(void)
287 {
288 }
289 #endif
290 
291 #if !(defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590)))
292 static inline int
__builtin_constant_p(int x)293 __builtin_constant_p(int x)
294 {
295     return 0;
296 }
297 #endif
298 
299 static inline uint64_t
bswap_64(uint64_t x)300 bswap_64(uint64_t x)
301 {
302     return (((x & 0xFF00000000000000ull) >> 56) |
303             ((x & 0x00FF000000000000ull) >> 40) |
304             ((x & 0x0000FF0000000000ull) >> 24) |
305             ((x & 0x000000FF00000000ull) >>  8) |
306             ((x & 0x00000000FF000000ull) <<  8) |
307             ((x & 0x0000000000FF0000ull) << 24) |
308             ((x & 0x000000000000FF00ull) << 40) |
309             ((x & 0x00000000000000FFull) << 56));
310 }
311 
312 #define swapll(x) do { \
313 		if (sizeof(*(x)) != 8) \
314 			wrong_size(); \
315 		*(x) = bswap_64(*(x));          \
316 	} while (0)
317 
318 static inline uint32_t
bswap_32(uint32_t x)319 bswap_32(uint32_t x)
320 {
321     return (((x & 0xFF000000) >> 24) |
322             ((x & 0x00FF0000) >> 8) |
323             ((x & 0x0000FF00) << 8) |
324             ((x & 0x000000FF) << 24));
325 }
326 
327 static inline Bool
checked_int64_add(int64_t * out,int64_t a,int64_t b)328 checked_int64_add(int64_t *out, int64_t a, int64_t b)
329 {
330     /* Do the potentially overflowing math as uint64_t, as signed
331      * integers in C are undefined on overflow (and the compiler may
332      * optimize out our overflow check below, otherwise)
333      */
334     int64_t result = (uint64_t)a + (uint64_t)b;
335     /* signed addition overflows if operands have the same sign, and
336      * the sign of the result doesn't match the sign of the inputs.
337      */
338     Bool overflow = (a < 0) == (b < 0) && (a < 0) != (result < 0);
339 
340     *out = result;
341 
342     return overflow;
343 }
344 
345 static inline Bool
checked_int64_subtract(int64_t * out,int64_t a,int64_t b)346 checked_int64_subtract(int64_t *out, int64_t a, int64_t b)
347 {
348     int64_t result = (uint64_t)a - (uint64_t)b;
349     Bool overflow = (a < 0) != (b < 0) && (a < 0) != (result < 0);
350 
351     *out = result;
352 
353     return overflow;
354 }
355 
356 #define swapl(x) do { \
357 		if (sizeof(*(x)) != 4) \
358 			wrong_size(); \
359 		*(x) = bswap_32(*(x)); \
360 	} while (0)
361 
362 static inline uint16_t
bswap_16(uint16_t x)363 bswap_16(uint16_t x)
364 {
365     return (((x & 0xFF00) >> 8) |
366             ((x & 0x00FF) << 8));
367 }
368 
369 #define swaps(x) do { \
370 		if (sizeof(*(x)) != 2) \
371 			wrong_size(); \
372 		*(x) = bswap_16(*(x)); \
373 	} while (0)
374 
375 /* copy 32-bit value from src to dst byteswapping on the way */
376 #define cpswapl(src, dst) do { \
377 		if (sizeof((src)) != 4 || sizeof((dst)) != 4) \
378 			wrong_size(); \
379 		(dst) = bswap_32((src)); \
380 	} while (0)
381 
382 /* copy short from src to dst byteswapping on the way */
383 #define cpswaps(src, dst) do { \
384 		if (sizeof((src)) != 2 || sizeof((dst)) != 2) \
385 			wrong_size(); \
386 		(dst) = bswap_16((src)); \
387 	} while (0)
388 
389 extern _X_EXPORT void SwapLongs(CARD32 *list, unsigned long count);
390 
391 extern _X_EXPORT void SwapShorts(short *list, unsigned long count);
392 
393 extern _X_EXPORT void MakePredeclaredAtoms(void);
394 
395 extern _X_EXPORT int Ones(unsigned long /*mask */ );
396 
397 typedef struct _xPoint *DDXPointPtr;
398 typedef struct pixman_box16 *BoxPtr;
399 typedef struct _xEvent *xEventPtr;
400 typedef struct _xRectangle *xRectanglePtr;
401 typedef struct _GrabRec *GrabPtr;
402 
403 /*  typedefs from other places - duplicated here to minimize the amount
404  *  of unnecessary junk that one would normally have to include to get
405  *  these symbols defined
406  */
407 
408 #ifndef _XTYPEDEF_CHARINFOPTR
409 typedef struct _CharInfo *CharInfoPtr;  /* also in fonts/include/font.h */
410 
411 #define _XTYPEDEF_CHARINFOPTR
412 #endif
413 
414 extern _X_EXPORT unsigned long globalSerialNumber;
415 extern _X_EXPORT unsigned long serverGeneration;
416 
417 /* Don't use this directly, use BUG_WARN or BUG_WARN_MSG instead */
418 #define __BUG_WARN_MSG(cond, with_msg, ...)                                \
419           do { if (cond) {                                                \
420               ErrorFSigSafe("BUG: triggered 'if (" #cond ")'\n");          \
421               ErrorFSigSafe("BUG: %s:%u in %s()\n",                        \
422                            __FILE__, __LINE__, __func__);                 \
423               if (with_msg) ErrorFSigSafe(__VA_ARGS__);                    \
424               xorg_backtrace();                                           \
425           } } while(0)
426 
427 #define BUG_WARN_MSG(cond, ...)                                           \
428           __BUG_WARN_MSG(cond, 1, __VA_ARGS__)
429 
430 #define BUG_WARN(cond)  __BUG_WARN_MSG(cond, 0, NULL)
431 
432 #define BUG_RETURN(cond) \
433         do { if (cond) { __BUG_WARN_MSG(cond, 0, NULL); return; } } while(0)
434 
435 #define BUG_RETURN_MSG(cond, ...) \
436         do { if (cond) { __BUG_WARN_MSG(cond, 1, __VA_ARGS__); return; } } while(0)
437 
438 #define BUG_RETURN_VAL(cond, val) \
439         do { if (cond) { __BUG_WARN_MSG(cond, 0, NULL); return (val); } } while(0)
440 
441 #define BUG_RETURN_VAL_MSG(cond, val, ...) \
442         do { if (cond) { __BUG_WARN_MSG(cond, 1, __VA_ARGS__); return (val); } } while(0)
443 
444 #endif                          /* MISC_H */
445