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
// SPDX-License-Identifier: MPL-2.0

//! Bindings to *[libui-ng]*.
//!
//! [libui-ng]: https://github.com/libui-ng/libui-ng

#![allow(
    non_camel_case_types,
    non_snake_case,
    non_upper_case_globals,
)]

macro_rules! reexport {
    ($krate:ident) => {
        pub mod $krate {
            #![doc = concat!(
                "Re-exported items from [`",
                stringify!($krate),
                "`](https://crates.io/crates/",
                stringify!($krate),
                ").",
            )]

            pub use ::$krate::*;
        }
    };
}

reexport!(libc);

// `uiDateTimePicker` functions reference the `tm` struct from `<time.h>`, which we re-export here.
use ::libc::tm;

macro_rules! include_bindings {
    ($name:literal) => {
        include!(concat!(env!("OUT_DIR"), "/", $name, ".rs"));
    };
}

// Include the core, cross-platform bindings.
include_bindings!("bindings");
include_bindings!("bindings-control-sigs");

/// Platform-specific functionality.
pub mod platform {
    macro_rules! def_platform {
        (
            mod: $mod:tt,
            platform: $platform:literal,
            header: $header:literal,
            os: $os:literal
            $(
                , prelude: {
                    $($prelude_item:item)*
                }
            )? $(,)?
        ) => {
            #[doc = concat!("Additional features available on ", $platform, " platforms.")]
            #[cfg(target_os = $os)]
            pub mod $mod {
                use crate::*;

                $(
                    $($prelude_item)*
                )?

                // Include the platform-specific bindings.
                include_bindings!($header);
            }
        };
    }

    def_platform! {
        mod: darwin,
        platform: "Darwin",
        header: "bindings-darwin",
        os: "macos",
        prelude: {
            // TODO: Is there an established crate of Cocoa bindings that can replace this?

            reexport!(objc);

            use ::objc::runtime::BOOL;

            macro_rules! def_classes {
                ($($name:ident)*) => {
                    $(
                        pub type $name = ::objc::runtime::Object;
                    )*
                };
            }

            def_classes! {
                NSControl
                NSString
                NSView
            }

            // See <https://developer.apple.com/documentation/objectivec/nsinteger>.
            pub type NSInteger = libc::c_long;
            // See <https://developer.apple.com/documentation/objectivec/nsuinteger>.
            pub type NSUInteger = libc::c_ulong;
            // See <https://developer.apple.com/documentation/appkit/nscontrolsize>.
            pub type NSControlSize = NSUInteger;
            // See <https://developer.apple.com/documentation/appkit/nslayoutconstraintorientation>.
            pub type NSLayoutConstraintOrientation = NSInteger;
            // See <https://developer.apple.com/documentation/appkit/nslayoutpriority>.
            pub type NSLayoutPriority = libc::c_float;

            // `CGFloat` doesn't appear to have proper Objective-C documentation anymore, so you can
            // see its definition here:
            // <https://github.com/phracker/MacOSX-SDKs/blob/041600eda65c6a668f66cb7d56b7d1da3e8bcc93/MacOSX11.3.sdk/System/Library/Frameworks/CoreGraphics.framework/Versions/A/Headers/CGBase.h#L334-L348>.
            #[cfg(target_pointer_width = "64")]
            pub type CGFloat = libc::c_double;
            #[cfg(not(target_pointer_width = "64"))]
            pub type CGFloat = libc::c_float;
        },
    }
    def_platform! {
        mod: unix,
        platform: "Unix",
        header: "bindings-unix",
        os: "linux",
        prelude: {
            reexport!(glib_sys);
            reexport!(gtk_sys);

            use ::glib_sys::gboolean;
            use ::gtk_sys::GtkContainer;
        },
    }
    def_platform! {
        mod: windows,
        platform: "Windows",
        header: "bindings-windows",
        os: "windows",
        prelude: {
            reexport!(windows_sys);

            use ::windows_sys::Win32::Foundation::{
                BOOL,
                HINSTANCE,
                HWND,
                RECT,
            };

            // `windows_sys` doesn't provide these for some reason, so we have to do this ourselves.
            // See <https://learn.microsoft.com/en-us/windows/win32/winprog/windows-data-types> for
            // reference.

            pub type DWORD = libc::c_ulong;
            pub type LONG = libc::c_long;
            // TODO: Is this semantically correct?
            pub type LONG_PTR = isize;
            pub type LPCWSTR = *const WCHAR;
            pub type LPVOID = *mut libc::c_void;
            pub type WCHAR = libc::wchar_t;
        },
    }
}