1 module derelict.x11.Xmd;
2 
3 version(linux):
4 //~ import std.string;
5 import std.conv;
6 import core.stdc.config;
7 
8 import derelict.x11.Xtos;
9 
10 extern (C) nothrow @nogc:
11 /*
12  *  Xmd.d: MACHINE DEPENDENT DECLARATIONS.
13  */
14 
15 /*
16  * Special per-machine configuration flags.
17  */
18 version( X86_64 ){
19     enum bool LONG64 = true; /* 32/64-bit architecture */
20     /*
21      * Stuff to handle large architecture machines; the constants were generated
22      * on a 32-bit machine and must correspond to the protocol.
23      */
24     enum bool MUSTCOPY = true;
25 }
26 else{
27     enum bool LONG64 = false;
28     enum bool MUSTCOPY = false;
29 }
30 
31 
32 
33 /*
34  * Definition of macro used to set constants for size of network structures;
35  * machines with preprocessors that can't handle all of the sz_ symbols
36  * can define this macro to be sizeof(x) if and only if their compiler doesn't
37  * pad out structures (esp. the xTextElt structure which contains only two
38  * one-byte fields).  Network structures should always define sz_symbols.
39  *
40  * The sz_ prefix is used instead of something more descriptive so that the
41  * symbols are no more than 32 characters long (which causes problems for some
42  * compilers and preprocessors).
43  *
44  * The extra indirection is to get macro arguments to expand correctly before
45  * the concatenation, rather than afterward.
46  */
47 //~ template _SIZEOF(T){ size_t _SIZEOF = T.sizeof; }
48 size_t _SIZEOF(T)(){ return T.sizeof; }
49 alias _SIZEOF SIZEOF;
50 
51 /*
52  * Bitfield suffixes for the protocol structure elements, if you
53  * need them.  Note that bitfields are not guaranteed to be signed
54  * (or even unsigned) according to ANSI C.
55  */
56 version( X86_64 ){
57     alias long INT64;
58     //~ #  define B32 :32
59     //~ #  define B16 :16
60     alias uint INT32;
61     alias uint INT16;
62 }
63 else{
64     //~ #  define B32
65     //~ #  define B16
66     static if( LONG64 ){
67         alias c_long INT64;
68         alias int INT32;
69     }
70     else
71         alias c_long INT32;
72     alias short INT16;
73 }
74 
75 alias byte    INT8;
76 
77 static if( LONG64 ){
78     alias c_ulong CARD64;
79     alias uint CARD32;
80 }
81 else
82     alias c_ulong CARD32;
83 
84 static if( !WORD64 && !LONG64 )
85     alias ulong CARD64;
86 
87 alias ushort    CARD16;
88 alias byte      CARD8;
89 
90 alias CARD32    BITS32;
91 alias CARD16    BITS16;
92 
93 alias CARD8     BYTE;
94 alias CARD8     BOOL;
95 
96 /*
97  * definitions for sign-extending bitfields on 64-bit architectures
98  */
99 static if( WORD64 ){
100     template cvtINT8toInt(INT8 val)     {   const int       cvtINT8toInt    = cast(int)     (val & 0x00000080) ? (val | 0xffffffffffffff00) : val; }
101     template cvtINT16toInt(INT16 val)   {   const int       cvtINT16toInt   = cast(int)     (val & 0x00008000) ? (val | 0xffffffffffff0000) : val; }
102     template cvtINT32toInt(INT32 val)   {   const int       cvtINT32toInt   = cast(int)     (val & 0x80000000) ? (val | 0xffffffff00000000) : val; }
103     template cvtINT8toShort(INT8 val)   {   const short     cvtINT8toShort  = cast(short)   cvtINT8toInt(val); }
104     template cvtINT16toShort(INT16 val) {   const short     cvtINT16toShort = cast(short)   cvtINT16toInt(val); }
105     template cvtINT32toShort(INT32 val) {   const short     cvtINT32toShort = cast(short)   cvtINT32toInt(val); }
106     template cvtINT8toLong(INT8 val)    {   const c_long    cvtINT8toLong   = cast(c_long)  cvtINT8toInt(val); }
107     template cvtINT16toLong(INT16 val)  {   const c_long    cvtINT16toLong  = cast(c_long)  cvtINT16toInt(val); }
108     template cvtINT32toLong(INT32 val)  {   const c_long    cvtINT32toLong  = cast(c_long)  cvtINT32toInt(val); }
109 }
110 else{ /* WORD64 and UNSIGNEDBITFIELDS */
111     template cvtINT8toInt(INT8 val)     {   const int       cvtINT8toInt    = cast(int)     val; }
112     template cvtINT16toInt(INT16 val)   {   const int       cvtINT16toInt   = cast(int)     val; }
113     template cvtINT32toInt(INT32 val)   {   const int       cvtINT32toInt   = cast(int)     val; }
114     template cvtINT8toShort(INT8 val)   {   const short     cvtINT8toShort  = cast(short)   val; }
115     template cvtINT16toShort(INT16 val) {   const short     cvtINT16toShort = cast(short)   val; }
116     template cvtINT32toShort(INT32 val) {   const short     cvtINT32toShort = cast(short)   val; }
117     template cvtINT8toLong(INT8 val)    {   const c_long    cvtINT8toLong   = cast(c_long)  val; }
118     template cvtINT16toLong(INT16 val)  {   const c_long    cvtINT16toLong  = cast(c_long)  val; }
119     template cvtINT32toLong(INT32 val)  {   const c_long    cvtINT32toLong  = cast(c_long)  val; }
120 }
121 
122 
123 
124 static if( MUSTCOPY ){
125     /*
126      * This macro must not cast or else pointers will get aligned and be wrong
127      */
128     T NEXTPTR(T)(T p){ const T NEXTPTR = p + SIZEOF!(T); }
129 }
130 else{/* else not MUSTCOPY, this is used for 32-bit machines */
131     /*
132      * this version should leave result of type (t *), but that should only be
133      * used when not in MUSTCOPY
134      */
135     T NEXTPTR(T)(T p){ const T NEXTPTR = p + 1; }
136 }/* MUSTCOPY - used machines whose C structs don't line up with proto */