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
#![allow(dead_code)]
use {
    std::cell::Cell,
    crate::{
        cx::Cx,
        cursor::MouseCursor,
        makepad_micro_serde::*,
        makepad_math::dvec2,
        window::CxWindowPool,
        area::Area,
        event::{
            KeyEvent,
            ScrollEvent,
            MouseDownEvent,
            MouseUpEvent,
            MouseMoveEvent,
        }
    }
};

// HACK(eddyb) more or less `<[T; N]>::each_ref`, which is still unstable.
fn ref_array_to_array_of_refs<T, const N: usize>(ref_array: &[T; N]) -> [&T; N] {
    let mut out_refs = std::mem::MaybeUninit::<[&T; N]>::uninit();
    for (i, ref_elem) in ref_array.iter().enumerate() {
        unsafe { *out_refs.as_mut_ptr().cast::<&T>().add(i) = ref_elem; }
    }
    unsafe { out_refs.assume_init() }
}

pub const SWAPCHAIN_IMAGE_COUNT: usize = if cfg!(any(target_os = "macos",target_os = "windows")) { 1 } else { 2 };

/// "Swapchains" group together some number (i.e. `SWAPCHAIN_IMAGE_COUNT` here)
/// of "presentable images", to form a queue of render targets which can be
/// "presented" (to a surface, like a display, window, etc.) independently of
/// rendering being done onto *other* "presentable images" in the "swapchain".
///
/// Certain configurations of swapchains often have older/more specific names,
/// e.g. "double buffering" for `SWAPCHAIN_IMAGE_COUNT == 2` (or "triple" etc.).
#[derive(Copy, Clone, Debug, PartialEq, SerBin, DeBin, SerJson, DeJson)]
pub struct Swapchain<I>
    // HACK(eddyb) hint `{Ser,De}{Bin,Json}` derivers to add their own bounds.
    where I: Sized
{
    pub width: u32,
    pub height: u32,
    pub presentable_images: [PresentableImage<I>; SWAPCHAIN_IMAGE_COUNT],
}

impl Swapchain<()> {
    pub fn new(width: u32, height: u32) -> Self {
        let presentable_images = [(); SWAPCHAIN_IMAGE_COUNT].map(|()| PresentableImage {
            id: PresentableImageId::alloc(),
            image: (),
        });
        Self { width, height, presentable_images }
    }
}

impl<I> Swapchain<I> {
    pub fn images_as_ref(&self) -> Swapchain<&I> {
        let Swapchain { width, height, ref presentable_images } = *self;
        let presentable_images = ref_array_to_array_of_refs(presentable_images)
            .map(|&PresentableImage { id, ref image }| PresentableImage { id, image });
        Swapchain { width, height, presentable_images }
    }
    pub fn images_map<I2>(self, mut f: impl FnMut(PresentableImageId, I) -> I2) -> Swapchain<I2> {
        let Swapchain { width, height, presentable_images } = self;
        let presentable_images = presentable_images
            .map(|PresentableImage { id, image }| PresentableImage { id, image: f(id, image) });
        Swapchain { width, height, presentable_images }
    }
}

/// One of the "presentable images" of a [`SharedSwapchain`].
#[derive(Copy, Clone, Debug, PartialEq, SerBin, DeBin, SerJson, DeJson)]
pub struct PresentableImage<I>
    // HACK(eddyb) hint `{Ser,De}{Bin,Json}` derivers to add their own bounds.
    where I: Sized
{
    pub id: PresentableImageId,
    pub image: I,
}

/// Cross-process-unique (on best-effort) ID of a [`SharedPresentableImage`],
/// such that multiple processes on the same system should be able to share
/// swapchains with each-other and (effectively) never observe collisions.
#[derive(Copy, Clone, Debug, PartialEq, SerBin, DeBin, SerJson, DeJson)]
pub struct PresentableImageId {
    /// PID of the originating process (which allocated this ID).
    origin_pid: u32,

    /// The atomically-acquired value of a (private) counter, during allocation,
    /// in the originating process, which will guarantee that the same process
    /// continuously generating new swapchains will not overlap with itself,
    /// unless it generates billions of swapchains, mixing old and new ones.
    per_origin_counter: u32,
}

impl PresentableImageId {
    pub fn alloc() -> Self {
        use std::sync::atomic::{AtomicU32, Ordering};

        static COUNTER: AtomicU32 = AtomicU32::new(0);

        Self {
            origin_pid: std::process::id(),
            per_origin_counter: COUNTER.fetch_add(1, Ordering::Relaxed),
        }
    }

    pub fn as_u64(self) -> u64 {
        let Self { origin_pid, per_origin_counter } = self;
        (u64::from(origin_pid) << 32) | u64::from(per_origin_counter)
    }
}

pub type SharedSwapchain = Swapchain<SharedPresentableImageOsHandle>;

// FIXME(eddyb) move these type aliases into `os::{linux,apple,windows}`.

/// [DMA-BUF](crate::os::linux::dma_buf)-backed image from `eglExportDMABUFImageMESA`.
#[cfg(target_os = "linux")]
pub type SharedPresentableImageOsHandle =
    crate::os::linux::dma_buf::Image<crate::os::linux::dma_buf::RemoteFd>;

// HACK(eddyb) the macOS helper XPC service (in `os/apple/metal_xpc.{m,rs}`)
// doesn't need/want any form of "handle passing", as the `id` field contains
// all the disambiguating information it may need (however, long-term it'd
// probably be better to use something like `IOSurface` + mach ports).
#[cfg(target_os = "macos")]
#[derive(Copy, Clone, Debug, PartialEq, SerBin, DeBin, SerJson, DeJson)]
pub struct SharedPresentableImageOsHandle {
    // HACK(eddyb) working around deriving limitations.
    pub _dummy_for_macos: Option<u32>,
}


/// DirectX 11 `HANDLE` from `IDXGIResource::GetSharedHandle`.
#[cfg(target_os = "windows")]
// FIXME(eddyb) actually use a newtype of `HANDLE` with manual trait impls.
pub type SharedPresentableImageOsHandle = u64;

// FIXME(eddyb) use `enum Foo {}` here ideally, when the derives are fixed.
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
#[derive(Copy, Clone, Debug, PartialEq, SerBin, DeBin, SerJson, DeJson)]
pub struct SharedPresentableImageOsHandle {
    // HACK(eddyb) working around deriving limitations.
    pub _dummy_for_unsupported: Option<u32>,
}

#[derive(Copy, Clone, Debug, PartialEq, SerBin, DeBin, SerJson, DeJson)]
pub struct StdinWindowSize {
    pub dpi_factor: f64,
    pub swapchain: SharedSwapchain,
}

#[derive(Clone, Copy, Debug, Default, SerBin, DeBin, SerJson, DeJson, PartialEq)]
pub struct StdinMouseDown{
   pub button: usize,
   pub x: f64,
   pub y: f64,
   pub time: f64,
}

impl From<StdinMouseDown> for MouseDownEvent {
    fn from(v: StdinMouseDown) -> Self {
        Self{
            abs: dvec2(v.x, v.y),
            button: v.button,
            window_id: CxWindowPool::id_zero(),
            modifiers: Default::default(),
            time: v.time,
            handled: Cell::new(Area::Empty),
        }
    }
}

#[derive(Clone, Copy, Debug, Default, SerBin, DeBin, SerJson, DeJson, PartialEq)]
pub struct StdinMouseMove{
   pub time: f64,
   pub x: f64,
   pub y: f64
}

impl From<StdinMouseMove> for MouseMoveEvent {
    fn from(v: StdinMouseMove) -> Self {
        Self{
            abs: dvec2(v.x, v.y),
            window_id: CxWindowPool::id_zero(),
            modifiers: Default::default(),
            time: v.time,
            handled: Cell::new(Area::Empty),
        }
    }
}

#[derive(Clone, Copy, Debug, Default, SerBin, DeBin, SerJson, DeJson, PartialEq)]
pub struct StdinMouseUp{
   pub time: f64,
   pub button: usize,
   pub x: f64,
   pub y: f64
}

impl From<StdinMouseUp> for MouseUpEvent {
    fn from(v: StdinMouseUp) -> Self {
        Self{
            abs: dvec2(v.x, v.y),
            button: v.button,
            window_id: CxWindowPool::id_zero(),
            modifiers: Default::default(),
            time: v.time,
        }
    }
}


#[derive(Clone, Copy, Debug, Default, SerBin, DeBin, SerJson, DeJson, PartialEq)]
pub struct StdinScroll{
   pub time: f64,
   pub sx: f64,
   pub sy: f64,
   pub x: f64,
   pub y: f64,
   pub is_mouse: bool,
}

impl From<StdinScroll> for ScrollEvent {
    fn from(v: StdinScroll) -> Self {
        Self{
            abs: dvec2(v.x, v.y),
            scroll: dvec2(v.sx, v.sy),
            window_id: CxWindowPool::id_zero(),
            modifiers: Default::default(),
            handled_x: Cell::new(false),
            handled_y: Cell::new(false),
            is_mouse: v.is_mouse,
            time: v.time,
        }
    }
}

#[derive(Clone, Debug, SerBin, DeBin, SerJson, DeJson)]
pub enum HostToStdin{
    WindowSize(StdinWindowSize),
    Tick{
        buffer_id: u64,
        frame: u64,
        time: f64,
    },
    MouseDown(StdinMouseDown),
    MouseUp(StdinMouseUp),
    MouseMove(StdinMouseMove),
    KeyDown(KeyEvent),
    KeyUp(KeyEvent),
    Scroll(StdinScroll),
    ReloadFile{
        file:String,
        contents:String
    },
}

#[derive(Clone, Debug, SerBin, DeBin, SerJson, DeJson)]
pub enum StdinToHost {
    ReadyToStart,
    SetCursor(MouseCursor),
    // the client is done drawing, and the texture is completely updated
    DrawCompleteAndFlip(PresentableImageId),
}

impl StdinToHost{
    pub fn to_json(&self)->String{
        let mut json = self.serialize_json();
        json.push('\n');
        json
    }
}

impl HostToStdin{
    pub fn to_json(&self)->String{
        let mut json = self.serialize_json();
        json.push('\n');
        json
    }
}

impl Cx {
    
}