1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#![no_std]
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(clippy::unreadable_literal)]
#![allow(clippy::redundant_static_lifetimes)]
#![allow(clippy::cognitive_complexity)]

//! The `fermium` crate is raw bindings to the SDL2 C API.
//!
//! Compared to the common alternative,
//! [sdl2-sys](https://crates.io/crates/sdl2-sys), this is has consts instead of
//! enums, it is slightly more complete, and it works _much_ better on windows
//! MSVC (no special setup at all).
//!
//! Depending on how you configure the crate, you can target a minimum SDL2
//! version of 2.0.8 or later. You can compile against a later version than your
//! minimum bind version, but not an earlier one, so for best compatability you
//! should target the earliest SDL2 API version you can. The current version of
//! SDL2 in Ubuntu 18 is 2.0.8, and Debian Stable has 2.0.9, so we don't at this
//! time bother to support SDL2 versions before 2.0.8.
//!
//! `bindgen` is used to generate the bindings from the official SDL2 include
//! files. At the moment we include the following SDL2 headers:
//!
//! * `SDL2.h`
//! * `SDL_syswm.h`
//! * `SDL_vulkan.h`
//!
//! However, `SDL_syswm.h` in particular pulls in a bunch of extra code and it
//! overwhelms the generated bindings. To avoid this, we only keep the following
//! whitelist of items:
//!
//! * `SDL_` (functions, types, and vars)
//! * `SDLK_` (vars)
//! * `AUDIO_` (vars)
//! * Any other items that the above depend on.
//!
//! It is thought that this will expose all needed functionality, but if you
//! think something should be added to the whitelist please [submit an
//! issue](https://github.com/Lokathor/fermium/issues).
//!
//! ## Docs
//!
//! Bindgen doesn't understand how to convert doxygen style docs into rustdoc
//! style docs. What it does generate makes rustdoc think there a bunch of
//! random code block all over that it should run as test cases. Sadly, rustdoc
//! has no way to turn this off, so I have to tell bindgen to just emit no docs
//! at all.
//!
//! Instead you should check out the [SDL2 Wiki](https://wiki.libsdl.org/)

/// Does all our conditional compilation selection.
macro_rules! magic {
  (
    $(if #[cfg($($test:meta),*)] {
      $($if_tokens:tt)*
    })else* else {
      $($else_tokens:tt)*
    }
  ) => {
    magic!{
      @__forests [ ] ;
      $( [ {$($test),*} {$($if_tokens)*} ], )*
      [ { } {$($else_tokens)*} ],
    }
  };

  (
    if #[cfg($($if_meta:meta),*)] {
      $($if_tokens:tt)*
    } $(else if #[cfg($($else_meta:meta),*)] {
      $($else_tokens:tt)*
    })*
  ) => {
    magic!{
      @__forests [ ] ;
      [ {$($if_meta),*} {$($if_tokens)*} ],
      $( [ {$($else_meta),*} {$($else_tokens)*} ], )*
    }
  };

  (
    @__forests [ $($not:meta,)* ] ;
  ) => {
    /* halt expansion */
  };

  (
    @__forests [ $($not:meta,)* ] ;
    [ { $($m:meta),* } { $($tokens:tt)* } ],
    $($rest:tt)*
  ) => {
    #[cfg(all( $($m,)* not(any($($not),*)) ))]
    magic!{ @__identity $($tokens)* }

    magic!{
      @__forests [ $($not,)* $($m,)* ] ;
      $($rest)*
    }
  };

  (
    @__identity $($tokens:tt)*
  ) => {
    $($tokens)*
  };
}

// re-export the variable-length C types from their source crate to ease the end
// user experience.
pub use core::ffi::c_void;
magic! {
  if #[cfg(windows)] {
    pub type c_char = i8;
    pub type c_schar = i8;
    pub type c_uchar = u8;
    pub type c_short = i16;
    pub type c_ushort = u16;
    pub type c_int = i32;
    pub type c_uint = u32;
    pub type c_long = i32;
    pub type c_ulong = u32;
    pub type c_longlong = i64;
    pub type c_ulonglong = u64;
  } else {
    pub use libc::{
      c_char, c_int, c_long, c_longlong, c_short, c_uint, c_ulong, c_ulonglong, c_ushort, c_schar, c_uchar
    };
  }
}

// bring in the correct bindings file. The bindgen binary will place them into
// OUT_DIR, but for the pre-generated bindings they'll be within the source
// directory along side the lib.rs file.
magic! {
  if #[cfg(feature = "use_bindgen_bin")] {
    include!(concat!(
      env!("OUT_DIR"),
      "/SDL2-v2.0.",
      env!("BIND_PATCH_LEVEL"),
      "-",
      env!("TARGET"),
      ".rs"
    ));
  } else {
    include!(concat!(
      "SDL2-v2.0.",
      env!("BIND_PATCH_LEVEL"),
      "-",
      env!("TARGET"),
      ".rs"
    ));
  }
}

// Note(Lokathor): Bindgen doesn't parse all things properly on its own, and it
// doesn't parse CPP macros at all, so we must define some more stuff here.

/// See remarks of [`SDL_PixelFormatEnum`](https://wiki.libsdl.org/SDL_PixelFormatEnum)
pub type PixelType = _bindgen_ty_1;

/// See remarks of [`SDL_PixelFormatEnum`](https://wiki.libsdl.org/SDL_PixelFormatEnum#Remarks)
pub type BitmapOrder = _bindgen_ty_2;

/// See remarks of [`SDL_PixelFormatEnum`](https://wiki.libsdl.org/SDL_PixelFormatEnum#Remarks)
pub type PackedOrder = _bindgen_ty_3;

/// See remarks of [`SDL_PixelFormatEnum`](https://wiki.libsdl.org/SDL_PixelFormatEnum#Remarks)
pub type ArrayOrder = _bindgen_ty_4;

/// See remarks of [`SDL_PixelFormatEnum`](https://wiki.libsdl.org/SDL_PixelFormatEnum#Remarks)
pub type PackedLayout = _bindgen_ty_5;

// For whatever reason, bindgen processes this type properly in the 2.0.10
// headers and later, so after ty_6 there's a discepency between what the
// unknown types mean.
magic! {
  if #[cfg(not(feature = "bind_SDL2_2_0_10"))] {
    /// See [`SDL_PixelFormatEnum`](https://wiki.libsdl.org/SDL_PixelFormatEnum)
    pub type SDL_PixelFormatEnum = _bindgen_ty_6;
  }
}

magic! {
  if #[cfg(not(feature = "bind_SDL2_2_0_10"))] {
    /// See [`SDL_Keycode`](https://wiki.libsdl.org/SDL_Keycode)
    pub type SDLK = _bindgen_ty_7;
  } else {
    /// See [`SDL_Keycode`](https://wiki.libsdl.org/SDL_Keycode)
    pub type SDLK = _bindgen_ty_6;
  }
}

magic! {
  if #[cfg(not(feature = "bind_SDL2_2_0_10"))] {
    /// See [`SDL_LOG_CATEGORY`](https://wiki.libsdl.org/SDL_LOG_CATEGORY)
    pub type LogCategory = _bindgen_ty_8;
  } else {
    /// See [`SDL_LOG_CATEGORY`](https://wiki.libsdl.org/SDL_LOG_CATEGORY)
    pub type LogCategory = _bindgen_ty_7;
  }
}

/// `SDL_touch.h`: Used as the device ID for mouse events simulated with touch
/// input
pub const SDL_TOUCH_MOUSEID: u32 = -1i32 as u32;

// only present in 2.0.10 and later
magic! {
  if #[cfg(feature = "bind_SDL2_2_0_10")] {
    /// `SDL_touch.h`: Used as the SDL_TouchID for touch events simulated with
    /// mouse input
    pub const SDL_MOUSE_TOUCHID: u64 = -1i64 as u64;
  }
}

/// `SDL_surface.h`: Evaluates to true if the surface needs to be locked before
/// access.
///
/// ## Safety
///
/// This must be a valid `SDL_Surface` pointer.
#[inline(always)]
pub unsafe fn SDL_MUSTLOCK(surface: *const SDL_Surface) -> bool {
  (*surface).flags & SDL_RLEACCEL != 0
}

/// `SDL_pixels.h`: "internal" macro to check if a value is a pixel format value.
#[inline(always)]
pub const fn SDL_PIXELFLAG(format: SDL_PixelFormatEnum) -> SDL_PixelFormatEnum {
  (format >> 28) & 0x0F
}

/// `SDL_pixels.h`: Pixel type of this format.
#[inline(always)]
pub const fn SDL_PIXELTYPE(format: SDL_PixelFormatEnum) -> SDL_PixelFormatEnum {
  (format >> 24) & 0x0F
}

/// `SDL_pixels.h`: Component ordering of this format.
#[inline(always)]
pub const fn SDL_PIXELORDER(
  format: SDL_PixelFormatEnum,
) -> SDL_PixelFormatEnum {
  (format >> 20) & 0x0F
}

/// `SDL_pixels.h`: Channel width layout of this format.
#[inline(always)]
pub const fn SDL_PIXELLAYOUT(
  format: SDL_PixelFormatEnum,
) -> SDL_PixelFormatEnum {
  (format >> 16) & 0x0F
}

/// `SDL_pixels.h`: Bits per pixel.
#[inline(always)]
pub const fn SDL_BITSPERPIXEL(
  format: SDL_PixelFormatEnum,
) -> SDL_PixelFormatEnum {
  (format >> 8) & 0xFF
}

/// `SDL_pixels.h`: Bytes per pixel.
#[inline(always)]
pub fn SDL_BYTESPERPIXEL(format: SDL_PixelFormatEnum) -> SDL_PixelFormatEnum {
  if SDL_ISPIXELFORMAT_FOURCC(format) {
    if format == SDL_PIXELFORMAT_YUY2
      || format == SDL_PIXELFORMAT_UYVY
      || format == SDL_PIXELFORMAT_YVYU
    {
      2
    } else {
      1
    }
  } else {
    format & 0xFF
  }
}

/// `SDL_pixels.h`: Is this an indexed format?
#[inline(always)]
pub fn SDL_ISPIXELFORMAT_INDEXED(format: SDL_PixelFormatEnum) -> bool {
  !SDL_ISPIXELFORMAT_FOURCC(format)
    && (SDL_PIXELTYPE(format) == SDL_PIXELTYPE_INDEX1
      || SDL_PIXELTYPE(format) == SDL_PIXELTYPE_INDEX4
      || SDL_PIXELTYPE(format) == SDL_PIXELTYPE_INDEX8)
}

/// `SDL_pixels.h`: Is this a packed format?
#[inline(always)]
pub fn SDL_ISPIXELFORMAT_PACKED(format: SDL_PixelFormatEnum) -> bool {
  !SDL_ISPIXELFORMAT_FOURCC(format)
    && (SDL_PIXELTYPE(format) == SDL_PIXELTYPE_PACKED8
      || SDL_PIXELTYPE(format) == SDL_PIXELTYPE_PACKED16
      || SDL_PIXELTYPE(format) == SDL_PIXELTYPE_PACKED32)
}

/// `SDL_pixels.h`: Is this an array format?
#[inline(always)]
pub fn SDL_ISPIXELFORMAT_ARRAY(format: SDL_PixelFormatEnum) -> bool {
  !SDL_ISPIXELFORMAT_FOURCC(format)
    && (SDL_PIXELTYPE(format) == SDL_PIXELTYPE_ARRAYU8
      || SDL_PIXELTYPE(format) == SDL_PIXELTYPE_ARRAYU16
      || SDL_PIXELTYPE(format) == SDL_PIXELTYPE_ARRAYU32
      || SDL_PIXELTYPE(format) == SDL_PIXELTYPE_ARRAYF16
      || SDL_PIXELTYPE(format) == SDL_PIXELTYPE_ARRAYF32)
}

/// `SDL_pixels.h`: Does this format have an alpha channel?
#[inline(always)]
pub fn SDL_ISPIXELFORMAT_ALPHA(format: SDL_PixelFormatEnum) -> bool {
  (SDL_ISPIXELFORMAT_PACKED(format)
    && (SDL_PIXELORDER(format) == SDL_PACKEDORDER_ARGB
      || SDL_PIXELORDER(format) == SDL_PACKEDORDER_RGBA
      || SDL_PIXELORDER(format) == SDL_PACKEDORDER_ABGR
      || SDL_PIXELORDER(format) == SDL_PACKEDORDER_BGRA))
    || (SDL_ISPIXELFORMAT_ARRAY(format)
      && (SDL_PIXELORDER(format) == SDL_ARRAYORDER_ARGB
        || SDL_PIXELORDER(format) == SDL_ARRAYORDER_RGBA
        || SDL_PIXELORDER(format) == SDL_ARRAYORDER_ABGR
        || SDL_PIXELORDER(format) == SDL_ARRAYORDER_BGRA))
}

/// `SDL_pixels.h`: Is this a FourCC format?
#[inline(always)]
pub fn SDL_ISPIXELFORMAT_FOURCC(format: SDL_PixelFormatEnum) -> bool {
  (format != 0) && (SDL_PIXELFLAG(format) != 1)
}