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
#![cfg_attr(not(test), no_std)]
extern crate sys;
extern crate alloc;

use core::ffi::c_float;
use core::ffi::c_int;
use core::ffi::c_uint;
use core::time::Duration;

pub mod time;
pub mod lang;
pub mod update;
pub mod event;

pub mod prelude {
	pub use crate::System;
	pub use crate::time::*;
	pub use crate::lang::*;
	pub use crate::update::*;
	pub use crate::event::*;
}

use time::*;
use lang::*;


#[derive(Debug, Clone, Copy)]
pub struct System<Api = api::Default>(Api);

impl System<api::Default> {
	/// Creates default [`System`] without type parameter requirement.
	///
	/// Uses ZST [`api::Default`].
	#[allow(non_snake_case)]
	pub fn Default() -> Self { Self(Default::default()) }
}

impl<Api: Default + api::Api> Default for System<Api> {
	fn default() -> Self { Self(Default::default()) }
}

impl<Api: Default + api::Api> System<Api> {
	pub fn new() -> Self { Self(Default::default()) }
}

impl<Api: api::Api> System<Api> {
	pub fn new_with(api: Api) -> Self { Self(api) }
}


impl<Api: api::Api> System<Api> {
	/// Equivalent to [`sys::ffi::playdate_sys::getLanguage`]
	#[doc(alias = "sys::ffi::playdate_sys::getLanguage")]
	#[inline(always)]
	pub fn language(&self) -> PDLanguage {
		let f = self.0.get_language();
		unsafe { f() }
	}

	/// Equivalent to [`sys::ffi::playdate_sys::getCurrentTimeMilliseconds`]
	#[doc(alias = "sys::ffi::playdate_sys::getCurrentTimeMilliseconds")]
	#[inline(always)]
	pub fn current_time_milliseconds(&self) -> Duration {
		let f = self.0.get_current_time_milliseconds();
		let t = unsafe { f() };
		Duration::from_millis(t.into())
	}

	/// Equivalent to [`sys::ffi::playdate_sys::getSecondsSinceEpoch`]
	#[doc(alias = "sys::ffi::playdate_sys::getSecondsSinceEpoch")]
	#[inline(always)]
	pub fn seconds_since_epoch(&self) -> Duration {
		let f = self.0.get_seconds_since_epoch();
		let mut millis: c_uint = 0;
		let secs = unsafe { f(&mut millis) };
		Duration::new(secs.into(), 0) + Duration::from_millis(millis.into())
	}

	/// Equivalent to [`sys::ffi::playdate_sys::drawFPS`]
	#[doc(alias = "sys::ffi::playdate_sys::drawFPS")]
	#[inline(always)]
	pub fn draw_fps(&self, x: c_int, y: c_int) {
		let f = self.0.draw_fps();
		unsafe { f(x, y) }
	}

	/// Equivalent to [`sys::ffi::playdate_sys::getFlipped`]
	#[doc(alias = "sys::ffi::playdate_sys::getFlipped")]
	#[inline(always)]
	pub fn flipped(&self) -> bool {
		let f = self.0.get_flipped();
		unsafe { f() == 1 }
	}

	/// Equivalent to [`sys::ffi::playdate_sys::setAutoLockDisabled`]
	#[doc(alias = "sys::ffi::playdate_sys::setAutoLockDisabled")]
	#[inline(always)]
	pub fn set_auto_lock_disabled(&self, disable: bool) {
		let f = self.0.set_auto_lock_disabled();
		unsafe { f(disable as _) }
	}

	/// Equivalent to [`sys::ffi::playdate_sys::getReduceFlashing`]
	#[doc(alias = "sys::ffi::playdate_sys::getReduceFlashing")]
	#[inline(always)]
	pub fn reduce_flashing(&self) -> bool {
		let f = self.0.get_reduce_flashing();
		unsafe { f() == 1 }
	}

	// TODO: invent analog of `std::time::Instant`

	/// Equivalent to [`sys::ffi::playdate_sys::getElapsedTime`]
	#[doc(alias = "sys::ffi::playdate_sys::getElapsedTime")]
	#[inline(always)]
	pub fn elapsed_time(&self) -> Duration {
		let f = self.0.get_elapsed_time();
		let secs = unsafe { f() };
		Duration::from_secs_f32(secs)
	}

	/// Equivalent to [`sys::ffi::playdate_sys::resetElapsedTime`]
	#[doc(alias = "sys::ffi::playdate_sys::resetElapsedTime")]
	#[inline(always)]
	pub fn reset_elapsed_time(&self) {
		let f = self.0.reset_elapsed_time();
		unsafe { f() }
	}

	/// Equivalent to [`sys::ffi::playdate_sys::getBatteryPercentage`]
	#[doc(alias = "sys::ffi::playdate_sys::getBatteryPercentage")]
	#[inline(always)]
	pub fn battery_percentage(&self) -> c_float {
		let f = self.0.get_battery_percentage();
		unsafe { f() }
	}


	/// Equivalent to [`sys::ffi::playdate_sys::getBatteryVoltage`]
	#[doc(alias = "sys::ffi::playdate_sys::getBatteryVoltage")]
	#[inline(always)]
	pub fn battery_voltage(&self) -> c_float {
		let f = self.0.get_battery_voltage();
		unsafe { f() }
	}

	/// Equivalent to [`sys::ffi::playdate_sys::getTimezoneOffset`]
	#[doc(alias = "sys::ffi::playdate_sys::getTimezoneOffset")]
	#[inline(always)]
	pub fn timezone_offset(&self) -> i32 {
		let f = self.0.get_timezone_offset();
		unsafe { f() }
	}

	/// Equivalent to [`sys::ffi::playdate_sys::shouldDisplay24HourTime`]
	#[doc(alias = "sys::ffi::playdate_sys::shouldDisplay24HourTime")]
	#[inline(always)]
	pub fn should_display_24_hour_time(&self) -> bool {
		let f = self.0.should_display_24_hour_time();
		unsafe { f() == 1 }
	}

	/// Equivalent to [`sys::ffi::playdate_sys::convertEpochToDateTime`]
	#[doc(alias = "sys::ffi::playdate_sys::convertEpochToDateTime")]
	#[inline(always)]
	pub fn convert_epoch_to_date_time(&self, epoch: u32) -> PDDateTime {
		let mut dt = PDDateTime { year: 0,
		                          month: 0,
		                          day: 0,
		                          weekday: 0,
		                          hour: 0,
		                          minute: 0,
		                          second: 0 };
		self.convert_epoch_to_date_time_to(epoch, &mut dt);
		dt
	}

	/// Equivalent to [`sys::ffi::playdate_sys::convertEpochToDateTime`]
	#[doc(alias = "sys::ffi::playdate_sys::convertEpochToDateTime")]
	#[inline(always)]
	pub fn convert_epoch_to_date_time_to(&self, epoch: u32, dt: &mut PDDateTime) {
		let f = self.0.convert_epoch_to_date_time();
		unsafe { f(epoch, dt) }
	}

	/// Equivalent to [`sys::ffi::playdate_sys::convertDateTimeToEpoch`]
	#[doc(alias = "sys::ffi::playdate_sys::convertDateTimeToEpoch")]
	pub fn convert_date_time_to_epoch(&self, dt: &PDDateTime) -> u32 {
		let f = self.0.convert_date_time_to_epoch();
		let epoch = unsafe { f(dt as *const _ as *mut _) };
		let _ = dt; // this to prevent earlier drop.
		epoch
	}
}


pub mod api {
	use core::ffi::c_float;
	use core::ffi::c_int;
	use core::ffi::c_uint;
	use core::ffi::c_void;

	use sys::ffi::PDCallbackFunction;
	use sys::ffi::PDDateTime;
	use sys::ffi::PDLanguage;


	#[derive(Debug, Clone, Copy, core::default::Default)]
	pub struct Default;

	impl Api for Default {}


	pub trait Api {
		/// Equivalent to [`sys::ffi::playdate_sys::getLanguage`]
		#[doc(alias = "sys::ffi::playdate_sys::getLanguage")]
		#[inline(always)]
		fn get_language(&self) -> unsafe extern "C" fn() -> PDLanguage { *sys::api!(system.getLanguage) }

		/// Equivalent to [`sys::ffi::playdate_sys::getCurrentTimeMilliseconds`]
		#[doc(alias = "sys::ffi::playdate_sys::getCurrentTimeMilliseconds")]
		#[inline(always)]
		fn get_current_time_milliseconds(&self) -> unsafe extern "C" fn() -> c_uint {
			*sys::api!(system.getCurrentTimeMilliseconds)
		}

		/// Equivalent to [`sys::ffi::playdate_sys::getSecondsSinceEpoch`]
		#[doc(alias = "sys::ffi::playdate_sys::getSecondsSinceEpoch")]
		#[inline(always)]
		fn get_seconds_since_epoch(&self) -> unsafe extern "C" fn(milliseconds: *mut c_uint) -> c_uint {
			*sys::api!(system.getSecondsSinceEpoch)
		}

		/// Equivalent to [`sys::ffi::playdate_sys::drawFPS`]
		#[doc(alias = "sys::ffi::playdate_sys::drawFPS")]
		#[inline(always)]
		fn draw_fps(&self) -> unsafe extern "C" fn(x: c_int, y: c_int) { *sys::api!(system.drawFPS) }

		/// Equivalent to [`sys::ffi::playdate_sys::setUpdateCallback`]
		#[doc(alias = "sys::ffi::playdate_sys::setUpdateCallback")]
		#[inline(always)]
		fn set_update_callback(&self) -> unsafe extern "C" fn(update: PDCallbackFunction, userdata: *mut c_void) {
			*sys::api!(system.setUpdateCallback)
		}

		/// Equivalent to [`sys::ffi::playdate_sys::getFlipped`]
		#[doc(alias = "sys::ffi::playdate_sys::getFlipped")]
		#[inline(always)]
		fn get_flipped(&self) -> unsafe extern "C" fn() -> c_int { *sys::api!(system.getFlipped) }

		/// Equivalent to [`sys::ffi::playdate_sys::setAutoLockDisabled`]
		#[doc(alias = "sys::ffi::playdate_sys::setAutoLockDisabled")]
		#[inline(always)]
		fn set_auto_lock_disabled(&self) -> unsafe extern "C" fn(disable: c_int) {
			*sys::api!(system.setAutoLockDisabled)
		}

		/// Equivalent to [`sys::ffi::playdate_sys::getReduceFlashing`]
		#[doc(alias = "sys::ffi::playdate_sys::getReduceFlashing")]
		#[inline(always)]
		fn get_reduce_flashing(&self) -> unsafe extern "C" fn() -> c_int { *sys::api!(system.getReduceFlashing) }

		/// Equivalent to [`sys::ffi::playdate_sys::getElapsedTime`]
		#[doc(alias = "sys::ffi::playdate_sys::getElapsedTime")]
		#[inline(always)]
		fn get_elapsed_time(&self) -> unsafe extern "C" fn() -> c_float { *sys::api!(system.getElapsedTime) }

		/// Equivalent to [`sys::ffi::playdate_sys::resetElapsedTime`]
		#[doc(alias = "sys::ffi::playdate_sys::resetElapsedTime")]
		#[inline(always)]
		fn reset_elapsed_time(&self) -> unsafe extern "C" fn() { *sys::api!(system.resetElapsedTime) }

		/// Equivalent to [`sys::ffi::playdate_sys::getBatteryPercentage`]
		#[doc(alias = "sys::ffi::playdate_sys::getBatteryPercentage")]
		#[inline(always)]
		fn get_battery_percentage(&self) -> unsafe extern "C" fn() -> c_float {
			*sys::api!(system.getBatteryPercentage)
		}


		/// Equivalent to [`sys::ffi::playdate_sys::getBatteryVoltage`]
		#[doc(alias = "sys::ffi::playdate_sys::getBatteryVoltage")]
		#[inline(always)]
		fn get_battery_voltage(&self) -> unsafe extern "C" fn() -> c_float { *sys::api!(system.getBatteryVoltage) }

		/// Equivalent to [`sys::ffi::playdate_sys::getTimezoneOffset`]
		#[doc(alias = "sys::ffi::playdate_sys::getTimezoneOffset")]
		#[inline(always)]
		fn get_timezone_offset(&self) -> unsafe extern "C" fn() -> i32 { *sys::api!(system.getTimezoneOffset) }

		/// Equivalent to [`sys::ffi::playdate_sys::shouldDisplay24HourTime`]
		#[doc(alias = "sys::ffi::playdate_sys::shouldDisplay24HourTime")]
		#[inline(always)]
		fn should_display_24_hour_time(&self) -> unsafe extern "C" fn() -> c_int {
			*sys::api!(system.shouldDisplay24HourTime)
		}

		/// Equivalent to [`sys::ffi::playdate_sys::convertEpochToDateTime`]
		#[doc(alias = "sys::ffi::playdate_sys::convertEpochToDateTime")]
		#[inline(always)]
		fn convert_epoch_to_date_time(&self) -> unsafe extern "C" fn(epoch: u32, datetime: *mut PDDateTime) {
			*sys::api!(system.convertEpochToDateTime)
		}

		/// Equivalent to [`sys::ffi::playdate_sys::convertDateTimeToEpoch`]
		#[doc(alias = "sys::ffi::playdate_sys::convertDateTimeToEpoch")]
		#[inline(always)]
		fn convert_date_time_to_epoch(&self) -> unsafe extern "C" fn(datetime: *mut PDDateTime) -> u32 {
			*sys::api!(system.convertDateTimeToEpoch)
		}
	}
}