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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
use super::*;
macro_rules! into_generic {
($type:ty, $lifetime:tt) => {
into_generic!($type, GenericContext, $lifetime);
};
($type:ty, $other:ident, $lifetime:tt) => {
impl<$lifetime> From<&$type> for $other<$lifetime> {
fn from(other: &$type) -> $other<$lifetime> {
$other::new(other.environment_callback)
}
}
impl<$lifetime> From<&mut $type> for $other<$lifetime> {
fn from(other: &mut $type) -> $other<$lifetime> {
$other::new(other.environment_callback)
}
}
};
}
macro_rules! make_context {
($name:ident $(, #[doc = $doc:tt ])?) => {
$(#[doc = $doc])?
pub struct $name<'a> {
pub(crate) environment_callback: &'a retro_environment_t,
}
impl<'a> $name<'a> {
pub fn new(environment_callback: &'a retro_environment_t) -> Self {
Self {
environment_callback
}
}
}
into_generic!($name<'a>, 'a);
};
}
pub struct GenericContext<'a> {
pub(crate) environment_callback: &'a retro_environment_t,
}
impl<'a> GenericContext<'a> {
pub fn new(environment_callback: &'a retro_environment_t) -> Self {
Self {
environment_callback,
}
}
pub unsafe fn environment_callback(&self) -> &'a retro_environment_t {
self.environment_callback
}
pub fn enable_keyboard_callback(&self) -> bool {
self.set_keyboard_callback(retro_keyboard_callback {
callback: Some(retro_keyboard_callback_fn),
})
}
pub fn enable_audio_callback(&self) -> bool {
self.set_audio_callback(retro_audio_callback {
callback: Some(retro_audio_callback_fn),
set_state: Some(retro_audio_set_state_callback_fn),
})
}
pub fn enable_disk_control_interface(&self) -> bool {
self.set_disk_control_interface(retro_disk_control_callback {
set_eject_state: Some(retro_set_eject_state_callback),
get_eject_state: Some(retro_get_eject_state_callback),
get_image_index: Some(retro_get_image_index_callback),
set_image_index: Some(retro_set_image_index_callback),
get_num_images: Some(retro_get_num_images_callback),
replace_image_index: Some(retro_replace_image_index_callback),
add_image_index: Some(retro_add_image_index_callback),
})
}
pub fn enable_extended_disk_control_interface(&self) -> Result<(), Box<dyn std::error::Error>> {
if self.get_disk_control_interface_version() >= 1 {
let success = self.set_disk_control_ext_interface(retro_disk_control_ext_callback {
set_eject_state: Some(retro_set_eject_state_callback),
get_eject_state: Some(retro_get_eject_state_callback),
get_image_index: Some(retro_get_image_index_callback),
set_image_index: Some(retro_set_image_index_callback),
get_num_images: Some(retro_get_num_images_callback),
replace_image_index: Some(retro_replace_image_index_callback),
add_image_index: Some(retro_add_image_index_callback),
set_initial_image: Some(retro_set_initial_image_callback),
get_image_path: Some(retro_get_image_path_callback),
get_image_label: Some(retro_get_image_label_callback),
});
if !success {
return Err("Failed to enable the extended disk control interface.".into());
}
} else {
return Err("The extended disk control interface is unsupported.".into());
}
Ok(())
}
pub fn enable_audio_buffer_status_callback(&self) -> bool {
let data = retro_audio_buffer_status_callback {
callback: Some(retro_audio_buffer_status_callback_fn),
};
self.set_audio_buffer_status_callback(data)
}
}
pub type ResetContext<'a> = GenericContext<'a>;
pub type DeinitContext<'a> = GenericContext<'a>;
pub type GetSerializeSizeContext<'a> = GenericContext<'a>;
pub type SerializeContext<'a> = GenericContext<'a>;
pub type UnserializeContext<'a> = GenericContext<'a>;
pub type UnloadGameContext<'a> = GenericContext<'a>;
pub type CheatResetContext<'a> = GenericContext<'a>;
pub type CheatSetContext<'a> = GenericContext<'a>;
pub type GetRegionContext<'a> = GenericContext<'a>;
pub type GetMemoryDataContext<'a> = GenericContext<'a>;
pub type GetMemorySizeContext<'a> = GenericContext<'a>;
make_context!(GetAvInfoContext);
make_context!(InitContext);
make_context!(OptionsChangedContext);
make_context!(LoadGameSpecialContext);
make_context!(SetEnvironmentContext);
impl<'a> SetEnvironmentContext<'a> {
pub fn enable_proc_address_interface(&mut self) -> bool {
self.set_proc_address_callback(retro_get_proc_address_interface {
get_proc_address: Some(retro_get_proc_address_callback),
})
}
}
pub struct LoadGameContext<'a> {
pub(crate) environment_callback: &'a retro_environment_t,
pub(crate) camera_interface: &'a mut Option<retro_camera_callback>,
pub(crate) perf_interface: &'a mut Option<retro_perf_callback>,
pub(crate) location_interface: &'a mut Option<retro_location_callback>,
pub(crate) rumble_interface: &'a mut Option<retro_rumble_interface>,
#[cfg(feature = "unstable-env-commands")]
pub(crate) sensor_interface: &'a mut Option<retro_sensor_interface>,
}
impl<'a> LoadGameContext<'a> {
pub fn new(
environment_callback: &'a retro_environment_t,
camera_interface: &'a mut Option<retro_camera_callback>,
perf_interface: &'a mut Option<retro_perf_callback>,
location_interface: &'a mut Option<retro_location_callback>,
rumble_interface: &'a mut Option<retro_rumble_interface>,
#[cfg(feature = "unstable-env-commands")] sensor_interface: &'a mut Option<
retro_sensor_interface,
>,
) -> Self {
Self {
environment_callback,
camera_interface,
perf_interface,
location_interface,
rumble_interface,
#[cfg(feature = "unstable-env-commands")]
sensor_interface,
}
}
pub fn enable_frame_time_callback(&self, reference: i64) {
self.set_frame_time_callback(retro_frame_time_callback {
callback: Some(retro_frame_time_callback_fn),
reference,
});
}
#[proc::unstable(feature = "env-commands")]
pub fn enable_camera_interface(
&mut self,
caps: u64,
width: u32,
height: u32,
) -> Result<(), Box<dyn std::error::Error>> {
*self.camera_interface = self.get_camera_interface(retro_camera_callback {
caps,
width,
height,
start: None,
stop: None,
frame_raw_framebuffer: Some(retro_camera_frame_raw_framebuffer_callback),
frame_opengl_texture: Some(retro_camera_frame_opengl_texture_callback),
initialized: Some(retro_camera_initialized_callback),
deinitialized: Some(retro_camera_deinitialized_callback),
});
if self.camera_interface.is_some() {
Ok(())
} else {
Err("Failed to enable camera interface".into())
}
}
#[cfg(feature = "unstable-env-commands")]
#[proc::unstable(feature = "env-commands")]
pub fn enable_sensor_interface(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let ctx: GenericContext = self.into();
*self.sensor_interface = ctx.get_sensor_interface();
if self.sensor_interface.is_some() {
Ok(())
} else {
Err("Failed to enable sensor interface".into())
}
}
pub fn enable_perf_interface(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let ctx: GenericContext = self.into();
*self.perf_interface = ctx.get_perf_interface();
if self.perf_interface.is_some() {
Ok(())
} else {
Err("Failed to enable performance interface".into())
}
}
pub fn enable_location_interface(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let ctx: GenericContext = self.into();
*self.location_interface = ctx.get_location_callback();
if self.location_interface.is_some() {
Ok(())
} else {
Err("Failed to enable location interface".into())
}
}
pub fn enable_rumble_interface(&mut self) -> Result<(), Box<dyn std::error::Error>> {
*self.rumble_interface = self.get_rumble_interface();
if self.rumble_interface.is_some() {
Ok(())
} else {
Err("Failed to rumble location interface".into())
}
}
}
into_generic!(LoadGameContext<'a>, 'a);
pub struct AudioContext<'a> {
pub(crate) environment_callback: &'a retro_environment_t,
pub(crate) audio_sample_batch_callback: &'a retro_audio_sample_batch_t,
pub(crate) audio_sample_callback: &'a retro_audio_sample_t,
}
impl AudioContext<'_> {
pub fn batch_audio_samples(&self, samples: &[i16]) {
if let Some(callback) = self.audio_sample_batch_callback {
let len = samples.len() as u64;
unsafe {
(callback)(samples.as_ptr(), len / 2);
}
}
}
pub fn queue_audio_sample(&self, left: i16, right: i16) {
if let Some(callback) = self.audio_sample_callback {
unsafe {
(callback)(left, right);
}
}
}
}
into_generic!(AudioContext<'a>, 'a);
pub struct RunContext<'a> {
pub(crate) environment_callback: &'a retro_environment_t,
pub(crate) audio_sample_batch_callback: &'a retro_audio_sample_batch_t,
pub(crate) audio_sample_callback: &'a retro_audio_sample_t,
pub(crate) input_poll_callback: &'a retro_input_poll_t,
pub(crate) input_state_callback: &'a retro_input_state_t,
pub(crate) video_refresh_callback: &'a retro_video_refresh_t,
pub(crate) can_dupe: bool,
pub(crate) had_frame: &'a mut bool,
pub(crate) last_width: &'a mut u32,
pub(crate) last_height: &'a mut u32,
pub(crate) last_pitch: &'a mut u64,
pub(crate) supports_bitmasks: bool,
}
into_generic!(RunContext<'a>, 'a);
impl<'a> From<&mut RunContext<'a>> for AudioContext<'a> {
fn from(other: &mut RunContext<'a>) -> AudioContext<'a> {
AudioContext {
environment_callback: other.environment_callback,
audio_sample_batch_callback: other.audio_sample_batch_callback,
audio_sample_callback: other.audio_sample_callback,
}
}
}
impl<'a> RunContext<'_> {
#[inline(always)]
pub fn can_dupe(&self) -> bool {
self.can_dupe
}
pub fn poll_input(&self) {
if let Some(callback) = self.input_poll_callback {
unsafe {
(callback)();
}
}
}
pub fn get_input_state(&self, port: u32, device: u32, index: u32, id: u32) -> u16 {
if let Some(callback) = self.input_state_callback {
unsafe { (callback)(port, device, index, id) as u16 }
} else {
0
}
}
pub fn get_joypad_state(&self, port: u32, index: u32) -> JoypadState {
if let Some(callback) = self.input_state_callback {
let mut mask = JoypadState::empty();
unsafe {
if (callback)(port, RETRO_DEVICE_JOYPAD, index, RETRO_DEVICE_ID_JOYPAD_B) != 0 {
mask |= JoypadState::B
}
if (callback)(port, RETRO_DEVICE_JOYPAD, index, RETRO_DEVICE_ID_JOYPAD_Y) != 0 {
mask |= JoypadState::Y
}
if (callback)(
port,
RETRO_DEVICE_JOYPAD,
index,
RETRO_DEVICE_ID_JOYPAD_SELECT,
) != 0
{
mask |= JoypadState::SELECT
}
if (callback)(
port,
RETRO_DEVICE_JOYPAD,
index,
RETRO_DEVICE_ID_JOYPAD_START,
) != 0
{
mask |= JoypadState::START
}
if (callback)(port, RETRO_DEVICE_JOYPAD, index, RETRO_DEVICE_ID_JOYPAD_UP) != 0 {
mask |= JoypadState::UP
}
if (callback)(
port,
RETRO_DEVICE_JOYPAD,
index,
RETRO_DEVICE_ID_JOYPAD_DOWN,
) != 0
{
mask |= JoypadState::DOWN
}
if (callback)(
port,
RETRO_DEVICE_JOYPAD,
index,
RETRO_DEVICE_ID_JOYPAD_LEFT,
) != 0
{
mask |= JoypadState::LEFT
}
if (callback)(
port,
RETRO_DEVICE_JOYPAD,
index,
RETRO_DEVICE_ID_JOYPAD_RIGHT,
) != 0
{
mask |= JoypadState::RIGHT
}
if (callback)(port, RETRO_DEVICE_JOYPAD, index, RETRO_DEVICE_ID_JOYPAD_A) != 0 {
mask |= JoypadState::A
}
if (callback)(port, RETRO_DEVICE_JOYPAD, index, RETRO_DEVICE_ID_JOYPAD_X) != 0 {
mask |= JoypadState::X
}
if (callback)(port, RETRO_DEVICE_JOYPAD, index, RETRO_DEVICE_ID_JOYPAD_L) != 0 {
mask |= JoypadState::L
}
if (callback)(port, RETRO_DEVICE_JOYPAD, index, RETRO_DEVICE_ID_JOYPAD_R) != 0 {
mask |= JoypadState::R
}
if (callback)(port, RETRO_DEVICE_JOYPAD, index, RETRO_DEVICE_ID_JOYPAD_L2) != 0 {
mask |= JoypadState::L2
}
if (callback)(port, RETRO_DEVICE_JOYPAD, index, RETRO_DEVICE_ID_JOYPAD_R2) != 0 {
mask |= JoypadState::R2
}
if (callback)(port, RETRO_DEVICE_JOYPAD, index, RETRO_DEVICE_ID_JOYPAD_L3) != 0 {
mask |= JoypadState::L3
}
if (callback)(port, RETRO_DEVICE_JOYPAD, index, RETRO_DEVICE_ID_JOYPAD_R3) != 0 {
mask |= JoypadState::R3
}
}
return mask;
}
JoypadState::empty()
}
#[proc::unstable(feature = "env-commands")]
pub fn get_joypad_bitmask(&self, port: u32, index: u32) -> JoypadState {
if let Some(callback) = self.input_state_callback {
if self.supports_bitmasks {
return JoypadState::from_bits_truncate((callback)(
port,
RETRO_DEVICE_JOYPAD,
index,
RETRO_DEVICE_ID_JOYPAD_MASK,
) as u16);
}
return self.get_joypad_state(port, index);
}
JoypadState::empty()
}
pub fn draw_frame(&mut self, data: &[u8], width: u32, height: u32, pitch: u64) {
if let Some(callback) = self.video_refresh_callback {
*self.had_frame = true;
*self.last_width = width;
*self.last_height = height;
*self.last_pitch = pitch;
unsafe { (callback)(data.as_ptr() as *const c_void, width, height, pitch) }
}
}
pub fn dupe_frame(&self) {
if !self.can_dupe {
eprintln!("[ERROR] This frontend does not support frame duping!");
return;
} else if !*self.had_frame {
eprintln!("[ERROR] Cannot dupe frame, no previous frame has been drawn!");
return;
}
if let Some(callback) = self.video_refresh_callback {
unsafe {
(callback)(
std::ptr::null() as *const c_void,
*self.last_width,
*self.last_height,
*self.last_pitch,
)
}
}
}
pub fn draw_framebuffer(&mut self, framebuffer: retro_framebuffer, pitch: u64) {
if let Some(callback) = self.video_refresh_callback {
*self.had_frame = true;
*self.last_width = framebuffer.width;
*self.last_height = framebuffer.height;
*self.last_pitch = pitch;
unsafe {
(callback)(
framebuffer.data,
framebuffer.width,
framebuffer.height,
pitch,
)
}
}
}
pub fn draw_hardware_frame(&mut self, width: u32, height: u32, pitch: u64) {
if let Some(callback) = self.video_refresh_callback {
*self.had_frame = true;
*self.last_width = width;
*self.last_height = height;
*self.last_pitch = pitch;
unsafe {
(callback)(
RETRO_HW_FRAME_BUFFER_VALID as *const c_void,
width,
height,
pitch,
)
}
}
}
}