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