-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQRetroEnvironment.cpp
793 lines (630 loc) · 18.1 KB
/
QRetroEnvironment.cpp
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
#include "QRetroCommon.h"
#include "QRetroEnvironment.h"
#include <libretro.h>
#include <QThread>
void core_audio_sample(int16_t left, int16_t right)
{
auto _this = _qrthis();
if (_this && _this->audio())
{
sample_t samples[2] = {left, right};
_this->audio()->pushSamples(samples, 1);
}
}
size_t core_audio_sample_batch(const int16_t *data, size_t frames)
{
auto _this = _qrthis();
if (_this && _this->audio())
_this->audio()->pushSamples(data, frames);
return frames;
}
static bool core_camera_start(void)
{
auto _this = _qrthis();
if (_this && _this->camera())
return _this->camera()->start();
else
return false;
}
static void core_camera_stop(void)
{
auto _this = _qrthis();
if (_this && _this->camera())
_this->camera()->stop();
}
static long long unsigned core_hw_get_current_framebuffer()
{
auto _this = _qrthis();
if (_this && _this->core()->hw_render.context_type)
return _this->getCurrentFramebuffer();
return 0;
}
static void* core_hw_get_proc_address(const char *sym)
{
auto _this = _qrthis();
if (_this)
return _this->getProcAddress(QThread::currentThread(), sym);
return nullptr;
}
/* TODO: Support distance interval */
static void core_location_set_interval(unsigned ms, unsigned dist)
{
Q_UNUSED(dist)
auto _this = _qrthis();
if (_this && _this->location())
_this->location()->setUpdateInterval(static_cast<int>(ms));
}
static bool core_location_start(void)
{
auto _this = _qrthis();
if (_this && _this->location())
return _this->location()->startUpdates();
return false;
}
static void core_location_stop(void)
{
auto _this = _qrthis();
if (_this && _this->location())
_this->location()->stopUpdates();
}
static bool core_location_get_position(double *lat, double *lon,
double *horiz_accuracy, double *vert_accuracy)
{
auto _this = _qrthis();
if (_this && _this->location())
return _this->location()->getPosition(lat, lon, horiz_accuracy, vert_accuracy);
return false;
}
/* TODO: Stubbed. Do we need these? */
static void core_location_initialized(void)
{
}
static void core_location_deinitialized(void)
{
}
static void core_log(enum retro_log_level level, const char *fmt, ...)
{
auto _this = _qrthis();
char msg[1024];
va_list args;
QString final_string;
if (!_this || level < _this->getLogLevel())
return;
va_start(args, fmt);
vsnprintf(msg, sizeof(msg), fmt, args);
va_end(args);
final_string = QString(msg);
emit _this->onCoreLog(level, final_string);
}
/* TODO */
static bool core_rumble(unsigned int port, retro_rumble_effect effect,
unsigned short strength)
{
Q_UNUSED(port) Q_UNUSED(effect) Q_UNUSED(strength)
return true;
}
static bool core_sensor_set_state(unsigned port, retro_sensor_action action,
unsigned rate)
{
auto _this = _qrthis();
if (!_this)
return false;
else
return _this->sensors()->setState(port, action, rate);
}
static float core_sensor_get_input(unsigned port, unsigned id)
{
auto _this = _qrthis();
if (!_this)
return false;
else
return _this->sensors()->getInput(port, id);
}
static void core_led_set_state(int led, int state)
{
auto _this = _qrthis();
if (_this)
_this->led()->setState(led, state);
}
static void core_microphone_close_mic(retro_microphone_t *microphone)
{
auto _this = _qrthis();
if (_this)
return _this->microphone()->close(microphone);
}
static bool core_microphone_get_mic_state(const retro_microphone_t *microphone)
{
auto _this = _qrthis();
if (!_this)
return false;
else
return _this->microphone()->getState(microphone);
}
static bool core_microphone_set_mic_state(retro_microphone_t *microphone, bool state)
{
auto _this = _qrthis();
if (!_this)
return false;
else
return _this->microphone()->setState(microphone, state);
}
static bool core_microphone_get_params(const retro_microphone_t *microphone,
retro_microphone_params_t *params)
{
auto _this = _qrthis();
if (!_this)
return false;
else
return _this->microphone()->getParams(microphone, params);
}
static retro_microphone_t* core_microphone_open_mic(const retro_microphone_params_t *params)
{
auto _this = _qrthis();
if (!_this)
return nullptr;
else
return _this->microphone()->open(params);
}
static int core_microphone_read_mic(retro_microphone_t *microphone,
int16_t *samples, size_t num_samples)
{
auto _this = _qrthis();
if (!_this)
return 0;
else
return _this->microphone()->read(microphone, samples, num_samples);
}
static bool core_midi_input_enabled(void)
{
auto _this = _qrthis();
return _this && _this->midi()->inputEnabled();
}
static bool core_midi_output_enabled(void)
{
auto _this = _qrthis();
return _this && _this->midi()->outputEnabled();
}
static bool core_midi_read(uint8_t *byte)
{
auto _this = _qrthis();
return _this && _this->midi()->read(byte);
}
static bool core_midi_write(uint8_t byte, uint32_t delta_time)
{
auto _this = _qrthis();
return _this && _this->midi()->write(byte, delta_time);
}
static bool core_midi_flush()
{
auto _this = _qrthis();
return _this && _this->midi()->flush();
}
void core_video_refresh(const void *data, unsigned width,
unsigned height, size_t pitch)
{
auto _this = _qrthis();
if (!_this)
return;
emit _this->onVideoRefresh(data, width, height, static_cast<unsigned>(pitch));
}
bool core_environment(unsigned cmd, void *data)
{
auto _this = _qrthis();
bool experimental = cmd & RETRO_ENVIRONMENT_EXPERIMENTAL;
unsigned cmd_noflags = cmd & 0xFF;
if (!_this)
{
qWarning("Environment callback %u called on a NULL QRetro instance.", cmd);
return false;
}
/* The user can manually ignore certain environment callback IDs */
if (!_this->environmentCallbackSupported(cmd_noflags))
return false;
switch (cmd)
{
/* 01 */
case RETRO_ENVIRONMENT_SET_ROTATION:
if (data)
_this->setRotation(*(reinterpret_cast<const unsigned*>(data)) * 90);
else
return false;
break;
/* 02 / Deprecated */
case RETRO_ENVIRONMENT_GET_OVERSCAN:
core_log(RETRO_LOG_WARN, "RETRO_ENVIRONMENT_GET_OVERSCAN is deprecated!");
if (data)
*(reinterpret_cast<bool*>(data)) = _this->getOverscan();
else
return false;
break;
/* 03 */
case RETRO_ENVIRONMENT_GET_CAN_DUPE:
if (data)
*(reinterpret_cast<bool*>(data)) = _this->supportsDuping();
break;
/* Callbacks 04 - 05 are deprecated, and their IDs are reserved. */
case 4:
core_log(RETRO_LOG_WARN, "Old-style GET_VARIABLE is deprecated!");
return false;
case 5:
core_log(RETRO_LOG_WARN, "Old-style SET_VARIABLES is deprecated!");
return false;
/* 06 */
case RETRO_ENVIRONMENT_SET_MESSAGE:
emit _this->onCoreMessage(reinterpret_cast<const char*>(data));
break;
/* 07 / TODO: Test */
case RETRO_ENVIRONMENT_SHUTDOWN:
_this->close();
break;
/* 08 */
case RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL:
_this->setPerformanceLevel(*reinterpret_cast<const unsigned*>(data));
break;
/* 09 */
case RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY:
*reinterpret_cast<const char**>(data) =
_this->directories()->get(QRetroDirectories::System);
break;
/* 10 */
case RETRO_ENVIRONMENT_SET_PIXEL_FORMAT:
_this->setPixelFormat(
*reinterpret_cast<enum retro_pixel_format*>(data));
break;
/* 11 / TODO */
case RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS:
{
auto desc = reinterpret_cast<const retro_input_descriptor*>(data);
while (desc->description)
{
printf("%s: %u %u %u %u\n", desc->description, desc->port, desc->device, desc->index, desc->id);
desc++;
}
break;
}
/* 12 */
case RETRO_ENVIRONMENT_SET_KEYBOARD_CALLBACK:
_this->core()->keyboard =
*reinterpret_cast<retro_keyboard_callback*>(data);
break;
/* 13
case RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE: */
/* 14 / TODO: Vulkan, GLES testing */
case RETRO_ENVIRONMENT_SET_HW_RENDER:
{
auto hw = reinterpret_cast<retro_hw_render_callback*>(data);
switch (hw->context_type)
{
case RETRO_HW_CONTEXT_NONE:
_this->initVideo(hw->context_type);
break;
case RETRO_HW_CONTEXT_OPENGL:
case RETRO_HW_CONTEXT_OPENGL_CORE:
case RETRO_HW_CONTEXT_OPENGLES2:
case RETRO_HW_CONTEXT_OPENGLES3:
case RETRO_HW_CONTEXT_OPENGLES_VERSION:
_this->initVideo(hw->context_type);
hw->get_proc_address =
reinterpret_cast<retro_hw_get_proc_address_t>(
core_hw_get_proc_address);
hw->get_current_framebuffer =
reinterpret_cast<retro_hw_get_current_framebuffer_t>(
core_hw_get_current_framebuffer);
_this->core()->hw_render = *hw;
break;
default:
core_log(RETRO_LOG_ERROR, "Requested unsupported renderer %u",
hw->context_type);
return false;
}
break;
}
/* 15 */
case RETRO_ENVIRONMENT_GET_VARIABLE:
{
auto var = reinterpret_cast<retro_variable*>(data);
if (!var || !var->key)
return false;
auto val = _this->options()->getOptionValue(var->key);
if (!val)
return false;
var->value = val;
break;
}
/* 16 */
case RETRO_ENVIRONMENT_SET_VARIABLES:
if (data)
_this->options()->setOptions(reinterpret_cast<retro_variable*>(data));
break;
/* 17 / TODO: Test */
case RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE:
if (data)
*reinterpret_cast<bool*>(data) = _this->options()->variablesUpdated();
break;
/* 18 */
case RETRO_ENVIRONMENT_SET_SUPPORT_NO_GAME:
if (data)
_this->setSupportsNoGame(*(reinterpret_cast<bool*>(data)));
break;
/* 19 */
case RETRO_ENVIRONMENT_GET_LIBRETRO_PATH:
if (data)
*reinterpret_cast<const char**>(data) = _this->corePath();
break;
/* Callback 20 is deprecated/unused. */
case 20:
core_log(RETRO_LOG_WARN, "Old-style SET_AUDIO_CALLBACK is deprecated!");
return false;
/* 21 */
case RETRO_ENVIRONMENT_SET_FRAME_TIME_CALLBACK:
{
auto cb = reinterpret_cast<retro_frame_time_callback*>(data);
if (cb && cb->callback)
_this->core()->frame_time_callback = *cb;
break;
}
/* 22 */
case RETRO_ENVIRONMENT_SET_AUDIO_CALLBACK:
{
auto cb = reinterpret_cast<retro_audio_callback*>(data);
if (cb && cb->callback && cb->set_state)
_this->core()->audio_callback = *cb;
break;
}
/* 23 */
case RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE:
{
auto cb = reinterpret_cast<retro_rumble_interface*>(data);
cb->set_rumble_state = core_rumble;
break;
}
/* 25 */
case RETRO_ENVIRONMENT_GET_SENSOR_INTERFACE:
{
auto cb = reinterpret_cast<retro_sensor_interface*>(data);
cb->set_sensor_state = core_sensor_set_state;
cb->get_sensor_input = core_sensor_get_input;
break;
}
/* 26 */
case RETRO_ENVIRONMENT_GET_CAMERA_INTERFACE:
{
auto cb = reinterpret_cast<retro_camera_callback*>(data);
if (cb)
{
cb->start = core_camera_start;
cb->stop = core_camera_stop;
_this->camera()->init(cb);
}
break;
}
/* 27 */
case RETRO_ENVIRONMENT_GET_LOG_INTERFACE:
{
auto cb = reinterpret_cast<retro_log_callback*>(data);
cb->log = core_log;
break;
}
/* 28 / TODO
case RETRO_ENVIRONMENT_GET_PERF_INTERFACE:
{
auto cb = reinterpret_cast<retro_perf_callback*>(data);
}
*/
/* 29 */
case RETRO_ENVIRONMENT_GET_LOCATION_INTERFACE:
{
auto location = reinterpret_cast<retro_location_callback*>(data);
location->set_interval = core_location_set_interval;
location->start = core_location_start;
location->stop = core_location_stop;
location->get_position = core_location_get_position;
location->initialized = core_location_initialized;
location->deinitialized = core_location_deinitialized;
break;
}
/* 30 */
case RETRO_ENVIRONMENT_GET_CORE_ASSETS_DIRECTORY:
*reinterpret_cast<const char**>(data) =
_this->directories()->get(QRetroDirectories::CoreAssets);
break;
/* 31 */
case RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY:
*reinterpret_cast<const char**>(data) =
_this->directories()->get(QRetroDirectories::Save);
break;
/* 32 / TODO
case RETRO_ENVIRONMENT_SET_SYSTEM_AV_INFO:
break;
*/
/* 33 / TODO: Test */
case RETRO_ENVIRONMENT_SET_PROC_ADDRESS_CALLBACK:
_this->core()->get_proc_address_interface =
*reinterpret_cast<retro_get_proc_address_interface*>(data);
break;
/* 34 / TODO
case RETRO_ENVIRONMENT_SET_SUBSYSTEM_INFO:
break;
*/
/* 35 / TODO */
case RETRO_ENVIRONMENT_SET_CONTROLLER_INFO:
{
auto info = reinterpret_cast<const retro_controller_info*>(data);
for (unsigned i = 0; i < info->num_types; i++)
printf("%0X4: %s\n", info->types[i].id, info->types[i].desc);
break;
}
/* 36 */
case RETRO_ENVIRONMENT_SET_MEMORY_MAPS:
_this->setMemoryMaps(reinterpret_cast<const retro_memory_map*>(data));
break;
/* 37 */
case RETRO_ENVIRONMENT_SET_GEOMETRY:
{
auto geo = reinterpret_cast<const retro_game_geometry*>(data);
if (geo)
_this->setGeometry(geo->base_width, geo->base_height);
break;
}
/* 38 */
case RETRO_ENVIRONMENT_GET_USERNAME:
*reinterpret_cast<const char**>(data) = _this->username()->get();
break;
/* 39 */
case RETRO_ENVIRONMENT_GET_LANGUAGE:
*reinterpret_cast<unsigned*>(data) = _this->getLanguage();
break;
/* 40 */
case RETRO_ENVIRONMENT_GET_CURRENT_SOFTWARE_FRAMEBUFFER:
return _this->getCurrentSoftwareFramebuffer(
reinterpret_cast<retro_framebuffer*>(data));
/* 41 / TODO */
// case RETRO_ENVIRONMENT_GET_HW_RENDER_INTERFACE:
/* 42 */
case RETRO_ENVIRONMENT_SET_SUPPORT_ACHIEVEMENTS:
_this->setSupportsAchievements(*reinterpret_cast<bool*>(data));
break;
/* 43 / TODO */
//case RETRO_ENVIRONMENT_SET_HW_RENDER_CONTEXT_NEGOTIATION_INTERFACE:
//{
// auto hw = reinterpret_cast<const struct retro_hw_render_context_negotiation_interface_vulkan*>(data);
// Q_UNUSED(hw)
//if (hw->interface_type != RETRO_HW_RENDER_CONTEXT_NEGOTIATION_INTERFACE_VULKAN)
// return false;
//}
/* 44 / TODO / There is an API clash here. Special casing is needed until
* it's fixed. */
case RETRO_ENVIRONMENT_SET_SERIALIZATION_QUIRKS:
case RETRO_ENVIRONMENT_SET_HW_SHARED_CONTEXT:
if (!experimental)
_this->setSerializationQuirks(reinterpret_cast<uint64_t*>(data));
else
return false;
break;
/* 46 */
case RETRO_ENVIRONMENT_GET_LED_INTERFACE:
{
auto led = reinterpret_cast<retro_led_interface*>(data);
if (led)
led->set_led_state = core_led_set_state;
break;
}
/* 47 */
case RETRO_ENVIRONMENT_GET_AUDIO_VIDEO_ENABLE:
*(reinterpret_cast<int*>(data)) = _this->audioVideoEnable()->getFlags();
break;
/* 48 */
case RETRO_ENVIRONMENT_GET_MIDI_INTERFACE:
{
auto midi = reinterpret_cast<retro_midi_interface*>(data);
if (midi)
{
midi->input_enabled = core_midi_input_enabled;
midi->output_enabled = core_midi_output_enabled;
midi->read = core_midi_read;
midi->write = core_midi_write;
midi->flush = core_midi_flush;
}
break;
}
/* 49 */
case RETRO_ENVIRONMENT_GET_FASTFORWARDING:
*(reinterpret_cast<bool*>(data)) = _this->fastForwarding();
break;
/* 50 */
case RETRO_ENVIRONMENT_GET_TARGET_REFRESH_RATE:
*(reinterpret_cast<float*>(data)) =
static_cast<float>(_this->targetRefreshRate());
break;
/* 51 */
case RETRO_ENVIRONMENT_GET_INPUT_BITMASKS:
if (data)
*(reinterpret_cast<bool*>(data)) = _this->input()->supportsBitmasks();
return _this->input()->supportsBitmasks();
/* 52 */
case RETRO_ENVIRONMENT_GET_CORE_OPTIONS_VERSION:
*(reinterpret_cast<unsigned*>(data)) = _this->options()->maxVersion();
break;
/* 53 */
case RETRO_ENVIRONMENT_SET_CORE_OPTIONS:
_this->options()->setOptions(
reinterpret_cast<retro_core_option_definition**>(&data));
break;
/* 54 */
case RETRO_ENVIRONMENT_SET_CORE_OPTIONS_INTL:
_this->options()->setOptions(
reinterpret_cast<retro_core_options_intl*>(data));
break;
/* 55 */
case RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY:
{
auto var = reinterpret_cast<retro_core_option_display*>(data);
_this->options()->setVisibility(var->key, var->visible);
break;
}
/* 56 */
case RETRO_ENVIRONMENT_GET_PREFERRED_HW_RENDER:
*reinterpret_cast<unsigned*>(data) = _this->getPreferredRenderer();
break;
/* 61 */
case RETRO_ENVIRONMENT_GET_INPUT_MAX_USERS:
if (data)
*reinterpret_cast<unsigned*>(data) = _this->input()->maxUsers();
break;
/* 64 */
case RETRO_ENVIRONMENT_SET_FASTFORWARDING_OVERRIDE:
if (data)
_this->setFastForwardingOverride(
reinterpret_cast<retro_fastforwarding_override*>(data));
break;
/* 70 */
case RETRO_ENVIRONMENT_SET_VARIABLE:
{
if (data)
{
auto var = reinterpret_cast<retro_variable*>(data);
_this->options()->setOptionValue(var->key, var->value);
}
break;
}
/* 74 */
case RETRO_ENVIRONMENT_GET_JIT_CAPABLE:
if (!data)
return false;
else
*reinterpret_cast<bool*>(data) = _this->jitCapable();
break;
/* 75 */
case RETRO_ENVIRONMENT_GET_MICROPHONE_INTERFACE:
{
if (!data)
return false;
else
{
auto cb = reinterpret_cast<retro_microphone_interface*>(data);
cb->interface_version = _this->microphone()->interfaceVersion();
cb->close_mic = core_microphone_close_mic;
cb->get_mic_state = core_microphone_get_mic_state;
cb->set_mic_state = core_microphone_set_mic_state;
cb->get_params = core_microphone_get_params;
cb->open_mic = core_microphone_open_mic;
cb->read_mic = core_microphone_read_mic;
}
break;
}
/* Callback 76 is deprecated */
case 76:
core_log(RETRO_LOG_WARN, "Old-style SET_NETPACKET_INTERFACE is deprecated!");
return false;
/* 77 */
case RETRO_ENVIRONMENT_GET_DEVICE_POWER:
return _this->devicePower()->get(reinterpret_cast<retro_device_power*>(data));
default:
qWarning("Unimplemented environment callback %u%s%s.",
cmd_noflags,
cmd_noflags >= RETRO_ENVIRONMENT_SIZE ? " (above maximum)" : "",
experimental ? " (experimental)" : "");
return false;
}
return true;
}