-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
altrace_record.c
2761 lines (2409 loc) · 70.7 KB
/
altrace_record.c
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
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* alTrace; a debugging tool for OpenAL.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#include <execinfo.h>
#include <float.h>
const char *GAppName = "altrace_record";
#ifdef _MSC_VER
#define AL_API __declspec(dllexport)
#define ALC_API __declspec(dllexport)
#elif defined(__GNUC__) || defined(__clang__)
#define AL_API __attribute__((visibility("default")))
#define ALC_API __attribute__((visibility("default")))
#elif defined(__GNUC__) || defined(__clang__)
#define AL_API extern
#define ALC_API extern
#endif
#include "altrace_common.h"
// not in the headers, natch.
AL_API void AL_APIENTRY alTracePushScope(const ALchar *str);
AL_API void AL_APIENTRY alTracePopScope(void);
AL_API void AL_APIENTRY alTraceMessage(const ALchar *str);
AL_API void AL_APIENTRY alTraceBufferLabel(ALuint name, const ALchar *str);
AL_API void AL_APIENTRY alTraceSourceLabel(ALuint name, const ALchar *str);
AL_API void AL_APIENTRY alcTraceDeviceLabel(ALCdevice *device, const ALCchar *str);
AL_API void AL_APIENTRY alcTraceContextLabel(ALCcontext *ctx, const ALCchar *str);
static int logfd = -1;
static pthread_mutex_t _apilock;
static pthread_mutex_t *apilock;
typedef struct BufferWrapper
{
ALuint name;
ALint channels;
ALint bits;
ALint frequency;
ALint size; /* length of data in bytes. */
struct BufferWrapper *hash_prev; /* previous item in same hash bucket. */
struct BufferWrapper *hash_next; /* next item in same hash bucket. */
} BufferWrapper;
typedef struct SourceWrapper
{
ALuint name;
ALenum state;
ALenum type;
ALuint buffer;
ALint buffers_queued;
ALint buffers_processed;
ALboolean source_relative;
ALboolean looping;
ALint sec_offset;
ALint sample_offset;
ALint byte_offset;
ALfloat gain;
ALfloat min_gain;
ALfloat max_gain;
ALfloat reference_distance;
ALfloat rolloff_factor;
ALfloat max_distance;
ALfloat pitch;
ALfloat cone_inner_angle;
ALfloat cone_outer_angle;
ALfloat cone_outer_gain;
ALfloat position[3];
ALfloat velocity[3];
ALfloat direction[3];
struct SourceWrapper *playlist_next;
struct SourceWrapper *playlist_prev;
struct SourceWrapper *hash_prev; /* previous item in same hash bucket. */
struct SourceWrapper *hash_next; /* next item in same hash bucket. */
} SourceWrapper;
struct ContextWrapper;
typedef struct DeviceWrapper
{
ALCdevice *device;
ALCenum errorlatch;
ALCboolean iscapture;
ALCboolean connected;
ALCboolean supports_disconnect_ext;
ALCint capture_samples;
int samplesize; /* size of a capture device sample in bytes */
char *extension_string;
BufferWrapper *wrapped_buffer_hash[256];
struct ContextWrapper *contexts;
struct DeviceWrapper *prev;
struct DeviceWrapper *next;
} DeviceWrapper;
typedef struct ContextWrapper
{
ALCcontext *ctx;
DeviceWrapper *device;
char *extension_string;
ALenum errorlatch;
ALboolean checked_static_state;
SourceWrapper *wrapped_source_hash[256];
ALenum distance_model;
ALfloat doppler_factor;
ALfloat doppler_velocity;
ALfloat speed_of_sound;
ALfloat listener_position[3];
ALfloat listener_velocity[3];
ALfloat listener_orientation[6];
ALfloat listener_gain;
SourceWrapper *playlist;
struct ContextWrapper *next;
struct ContextWrapper *prev;
} ContextWrapper;
static DeviceWrapper null_device;
static ALenum null_context_errorlatch = AL_NO_ERROR;
static ContextWrapper *current_context;
static void quit_altrace_record(void) __attribute__((destructor));
void out_of_memory(void)
{
fputs(GAppName, stderr);
fputs(": Out of memory!\n", stderr);
fflush(stderr);
quit_altrace_record();
_exit(42);
}
// override _exit(), which terminates the process without running library
// destructors, so we can close our log file, etc.
void _exit(int status)
{
quit_altrace_record();
_Exit(status); // just use _Exit(), which does the same thing but no one really uses. :P
}
NORETURN static void IO_WRITE_FAIL(void)
{
fprintf(stderr, "%s: failed to write to log: %s\n", GAppName, strerror(errno));
quit_altrace_record();
_exit(42);
}
static void writele32(const uint32 x)
{
const uint32 y = swap32(x);
if (write(logfd, &y, sizeof (y)) != sizeof (y)) {
IO_WRITE_FAIL();
}
}
static void writele64(const uint64 x)
{
const uint64 y = swap64(x);
if (write(logfd, &y, sizeof (y)) != sizeof (y)) {
IO_WRITE_FAIL();
}
}
static void IO_INT32(const int32 x)
{
union { int32 si32; uint32 ui32; } cvt;
cvt.si32 = x;
writele32(cvt.ui32);
}
static void IO_UINT32(const uint32 x)
{
writele32(x);
}
static void IO_UINT64(const uint64 x)
{
writele64(x);
}
static void IO_ALCSIZEI(const ALCsizei x)
{
IO_UINT64((uint64) x);
}
static void IO_ALSIZEI(const ALsizei x)
{
IO_UINT64((uint64) x);
}
static void IO_FLOAT(const float x)
{
union { float f; uint32 ui32; } cvt;
cvt.f = x;
IO_UINT32(cvt.ui32);
}
static void IO_DOUBLE(const double x)
{
union { double d; uint64 ui64; } cvt;
cvt.d = x;
IO_UINT64(cvt.ui64);
}
static void IO_STRING(const char *str)
{
if (!str) {
IO_UINT64(0xFFFFFFFFFFFFFFFFull);
} else {
const size_t len = strlen(str);
IO_UINT64((uint64) len);
if (len > 0) {
if (write(logfd, str, len) != len) IO_WRITE_FAIL();
}
}
}
static void IO_BLOB(const uint8 *data, const uint64 len)
{
if (!data) {
IO_UINT64(0xFFFFFFFFFFFFFFFFull);
} else {
const size_t slen = (size_t) len;
IO_UINT64(len);
if (len > 0) {
if (write(logfd, data, slen) != slen) IO_WRITE_FAIL();
}
}
}
static void IO_EVENTENUM(const EventEnum x)
{
IO_UINT32((uint32) x);
}
static void IO_PTR(const void *ptr)
{
IO_UINT64((uint64) (size_t) ptr);
}
static void IO_ALCENUM(ALCenum e)
{
IO_UINT32((uint32) e);
}
static void IO_ENUM(ALenum e)
{
IO_UINT32((uint32) e);
}
static void IO_ALCBOOLEAN(ALCboolean b)
{
IO_UINT32((uint32) b);
}
static void IO_BOOLEAN(ALboolean b)
{
IO_UINT32((uint32) b);
}
static void free_hash_item_stackframe(void *from, char *to) { free(to); }
static uint8 hash_stackframe(void *from) {
// everything is going to end in a multiple of pointer size, so flatten down.
const size_t val = ((size_t) from) / (sizeof (void *));
return (uint8) (val & 0xFF); // good enough, I guess.
}
HASH_MAP(stackframe, void *, char *)
// backtrace_symbols() is pretty expensive, so we don't want to run it
// dozens of times per-frame. So we call it on individual frames when
// we haven't seen them before, assuming most of our calls come from a
// handful of places, and even there we can reuse most of the callstack
// frames anyhow.
static char *get_callstack_sym(void *frame, int *_seen_before)
{
char *retval = get_mapped_stackframe(frame);
if (retval) {
*_seen_before = 1;
} else {
char **syms = backtrace_symbols(&frame, 1);
char *sym = syms[0];
if (sym && (*sym == '0')) { // skip '0\s+' on macOS; it's counting frames assuming there's more than one.
while (*(++sym) == ' ') { /* spin */ }
}
retval = sym ? strdup(sym) : NULL;
free(syms);
*_seen_before = 0;
if (retval) {
add_stackframe_to_map(frame, retval);
}
}
return retval;
}
__attribute__((noinline)) static void IO_ENTRYINFO(const EventEnum entryid)
{
const uint32 currentms = now();
void* callstack[MAX_CALLSTACKS + 2];
char *new_strings[MAX_CALLSTACKS];
void *new_strings_ptrs[MAX_CALLSTACKS];
int frames = backtrace(callstack, MAX_CALLSTACKS);
int num_new_strings = 0;
int i;
frames -= 2; // skip IO_ENTRYINFO and entry point.
if (frames < 0) {
frames = 0;
}
for (i = 0; i < frames; i++) {
int seen_before = 0;
void *ptr = callstack[i + 2];
char *str = get_callstack_sym(ptr, &seen_before);
if ((str == NULL) && !seen_before) {
break;
}
if (!seen_before) {
new_strings[num_new_strings] = str;
new_strings_ptrs[num_new_strings] = ptr;
num_new_strings++;
}
}
frames = i; /* in case we stopped early. */
if (num_new_strings > 0) {
IO_EVENTENUM(ALEE_NEW_CALLSTACK_SYMS);
IO_UINT32((uint32) num_new_strings);
for (i = 0; i < num_new_strings; i++) {
IO_PTR(new_strings_ptrs[i]);
IO_STRING(new_strings[i]);
}
}
IO_EVENTENUM(entryid);
IO_UINT32(currentms);
IO_UINT64((uint64) pthread_self());
IO_UINT32((uint32) frames);
for (i = 0; i < frames; i++) {
IO_PTR(callstack[i + 2]);
}
}
static void APILOCK(void)
{
const int rc = pthread_mutex_lock(apilock);
if (rc != 0) {
fprintf(stderr, "%s: Failed to grab API lock: %s\n", GAppName, strerror(rc));
quit_altrace_record();
_exit(42);
}
}
static void APIUNLOCK(void)
{
const int rc = pthread_mutex_unlock(apilock);
if (rc != 0) {
fprintf(stderr, "%s: Failed to release API lock: %s\n", GAppName, strerror(rc));
quit_altrace_record();
_exit(42);
}
}
static ALenum check_al_error_events(void)
{
if (!current_context) return AL_NO_ERROR; // !!! FIXME: OpenAL-Soft returns AL_INVALID_OPERATION if no context is current.
const ALenum alerr = REAL_alGetError();
if (alerr != AL_NO_ERROR) {
ALenum *errorlatch = current_context ? ¤t_context->errorlatch : &null_context_errorlatch;
IO_EVENTENUM(ALEE_ALERROR_TRIGGERED);
IO_ENUM(alerr);
if (*errorlatch == AL_NO_ERROR) {
*errorlatch = alerr;
}
}
return alerr;
}
static ALCenum check_alc_error_events(DeviceWrapper *device)
{
ALCenum alcerr = ALC_NO_ERROR;
if (device) {
alcerr = REAL_alcGetError(device->device);
if (alcerr != ALC_NO_ERROR) {
IO_EVENTENUM(ALEE_ALCERROR_TRIGGERED);
IO_PTR(device);
IO_ALCENUM(alcerr);
if (device->errorlatch == ALC_NO_ERROR) {
device->errorlatch = alcerr;
}
}
}
return alcerr;
}
static void check_al_async_states(void);
#define IO_START(e) \
{ \
APILOCK(); \
IO_ENTRYINFO(ALEE_##e)
#define IO_END() \
check_al_error_events(); \
check_al_async_states(); \
APIUNLOCK(); \
}
#define IO_END_ALC(dev) \
check_alc_error_events(dev); \
check_al_async_states(); \
APIUNLOCK(); \
}
static const char *get_procname(const int argc, char **argv)
{
const char *procname = "MyOpenALProgram";
if (argv && argv[0]) {
const char *ptr = strrchr(argv[0], '/');
procname = ptr ? (ptr + 1) : argv[0];
}
return procname;
}
static char *choose_tracefile_name(const int argc, char **argv)
{
const char *procname = get_procname(argc, argv);
char *retval = sprintf_alloc("%s.altrace", procname);
int i = 1;
while (retval != NULL) {
FILE *f = fopen(retval, "rb");
if (!f) {
break;
}
fclose(f);
free(retval);
retval = sprintf_alloc("%s.%d.altrace", procname, i);
i++;
}
return retval;
}
static void init_altrace_record(int argc, char **argv) __attribute__((constructor));
static void init_altrace_record(int argc, char **argv)
{
int okay = 1;
fprintf(stderr, "\n\n\n%s: starting up...\n", GAppName);
fflush(stderr);
if (!init_clock()) {
fflush(stderr);
_exit(42);
}
if (!load_real_openal()) {
_exit(42);
}
if (okay) {
const int rc = pthread_mutex_init(&_apilock, NULL);
if (rc != 0) {
fprintf(stderr, "%s: Failed to create mutex: %s\n", GAppName, strerror(rc));
okay = 0;
}
apilock = &_apilock;
}
if (okay) {
char *filename = choose_tracefile_name(argc, argv);
logfd = filename ? open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644) : -1;
if (logfd == -1) {
fprintf(stderr, "%s: Failed to open OpenAL log file '%s': %s\n", GAppName, filename, filename ? strerror(errno) : "Out of memory");
okay = 0;
} else {
fprintf(stderr, "%s: Recording OpenAL session to log file '%s'\n\n\n", GAppName, filename);
}
free(filename);
}
fflush(stderr);
if (!okay) {
quit_altrace_record();
_exit(42);
}
IO_UINT32(ALTRACE_LOG_FILE_MAGIC);
IO_UINT32(ALTRACE_LOG_FILE_FORMAT);
}
static void quit_altrace_record(void)
{
const int io = logfd;
pthread_mutex_t *mutex = apilock;
logfd = -1;
apilock = NULL;
fprintf(stderr, "%s: Shutting down...\n", GAppName);
fflush(stderr);
if (io != -1) {
const uint32 eos = swap32((uint32) ALEE_EOS);
const uint32 ticks = swap32(now());
if ((write(io, &eos, 4) != 4) || (write(io, &ticks, 4) != 4)) {
fprintf(stderr, "%s: Failed to write EOS to OpenAL log file: %s\n", GAppName, strerror(errno));
}
if (close(io) < 0) {
fprintf(stderr, "%s: Failed to close OpenAL log file: %s\n", GAppName, strerror(errno));
}
}
if (mutex) {
pthread_mutex_destroy(mutex);
}
#define ENTRYPOINT(ret,name,params,args,numargs,visitparams,visitargs) REAL_##name = NULL;
#include "altrace_entrypoints.h"
close_real_openal();
free_stackframe_map();
fflush(stderr);
}
ALCcontext *alcGetCurrentContext(void)
{
ALCcontext *retval;
IO_START(alcGetCurrentContext);
retval = REAL_alcGetCurrentContext();
(void) retval; // !!! FIXME: assert this hasn't gone out of sync with current_context...
IO_PTR(current_context);
IO_END_ALC(NULL);
return (ALCcontext *) current_context;
}
ALCdevice *alcGetContextsDevice(ALCcontext *_ctx)
{
ContextWrapper *ctx = (ContextWrapper *) _ctx;
ALCdevice *retval;
IO_START(alcGetContextsDevice);
IO_PTR(ctx);
retval = REAL_alcGetContextsDevice(ctx->ctx);
(void) retval; // !!! FIXME: assert this hasn't gone out of sync with current_context...
IO_PTR(ctx->device);
IO_END_ALC(ctx->device);
return (ALCdevice *) ctx->device;
}
ALCboolean alcIsExtensionPresent(ALCdevice *_device, const ALCchar *extname)
{
DeviceWrapper *device = _device ? (DeviceWrapper *) _device : &null_device;
ALCboolean retval;
IO_START(alcIsExtensionPresent);
IO_PTR(_device);
IO_STRING(extname);
if (strcasecmp(extname, "ALC_EXT_trace_info") == 0) {
retval = ALC_TRUE;
} else if (strcasecmp(extname, "ALC_EXT_EFX") == 0) { retval = ALC_FALSE; // !!! FIXME
} else {
retval = REAL_alcIsExtensionPresent(device->device, extname);
}
IO_ALCBOOLEAN(retval);
IO_END_ALC(device);
return retval;
}
void *alcGetProcAddress(ALCdevice *_device, const ALCchar *funcname)
{
DeviceWrapper *device = _device ? (DeviceWrapper *) _device : &null_device;
void *retval = NULL;
IO_START(alcGetProcAddress);
IO_PTR(_device);
IO_STRING(funcname);
// always return our entry points, so the app always calls through here.
if (!funcname || ((funcname[0] != 'a') || (funcname[1] != 'l') || (funcname[2] != 'c'))) {
// !!! FIXME: should set an error state.
retval = NULL;
}
#define ENTRYPOINT(ret,fn,params,args,numargs,visitparams,visitargs) else if (strcmp(funcname, #fn) == 0) { retval = (void *) fn; }
#include "altrace_entrypoints.h"
IO_PTR(retval);
IO_END_ALC(device);
return retval;
}
ALCenum alcGetEnumValue(ALCdevice *_device, const ALCchar *enumname)
{
DeviceWrapper *device = _device ? (DeviceWrapper *) _device : &null_device;
ALCenum retval;
IO_START(alcGetEnumValue);
IO_PTR(_device);
IO_STRING(enumname);
retval = REAL_alcGetEnumValue(device->device, enumname);
IO_ALCENUM(retval);
IO_END_ALC(device);
return retval;
}
const ALCchar *alcGetString(ALCdevice *_device, ALCenum param)
{
DeviceWrapper *device = _device ? (DeviceWrapper *) _device : &null_device;
const ALCchar *retval;
IO_START(alcGetString);
IO_PTR(_device);
IO_ALCENUM(param);
retval = REAL_alcGetString(device->device, param);
if ((param == ALC_EXTENSIONS) && retval) {
const char *addstr = "ALC_EXT_trace_info";
const size_t slen = strlen(retval) + strlen(addstr) + 2;
char *ptr = (char *) realloc(device->extension_string, slen);
if (ptr) {
device->extension_string = ptr;
snprintf(ptr, slen, "%s%s%s", retval, *retval ? " " : "", addstr);
retval = (const ALCchar *) ptr;
}
}
IO_STRING(retval);
IO_END_ALC(device);
return retval;
}
ALCdevice *alcCaptureOpenDevice(const ALCchar *devicename, ALCuint frequency, ALCenum format, ALCsizei buffersize)
{
DeviceWrapper *device = (DeviceWrapper *) calloc(1, sizeof (DeviceWrapper));
ALCdevice *retval;
if (!device) {
return NULL;
}
IO_START(alcCaptureOpenDevice);
IO_STRING(devicename);
IO_UINT32(frequency);
IO_ALCENUM(format);
IO_ALSIZEI(buffersize);
retval = REAL_alcCaptureOpenDevice(devicename, frequency, format, buffersize);
IO_PTR(retval ? device : NULL);
if (!retval) {
free(device);
} else {
ALCint alci = 0;
const ALCchar *alcstr;
device->device = retval;
device->iscapture = ALC_TRUE;
device->connected = ALC_TRUE;
device->supports_disconnect_ext = REAL_alcIsExtensionPresent(device->device, "ALC_EXT_disconnect");
if (format == AL_FORMAT_MONO8) {
device->samplesize = 1;
} else if (format == AL_FORMAT_MONO16) {
device->samplesize = 2;
} else if (format == AL_FORMAT_STEREO8) {
device->samplesize = 2;
} else if (format == AL_FORMAT_STEREO16) {
device->samplesize = 4;
// !!! FIXME: float32
}
device->next = null_device.next;
device->prev = &null_device;
null_device.next = device;
if (device->next) {
device->next->prev = device;
}
REAL_alcGetIntegerv(device->device, ALC_MAJOR_VERSION, 1, &alci);
IO_INT32(alci);
REAL_alcGetIntegerv(device->device, ALC_MINOR_VERSION, 1, &alci);
IO_INT32(alci);
alcstr = REAL_alcGetString(device->device, ALC_CAPTURE_DEVICE_SPECIFIER);
IO_STRING(alcstr);
alcstr = REAL_alcGetString(device->device, ALC_EXTENSIONS);
IO_STRING(alcstr);
}
IO_END_ALC(device);
return (ALCdevice *) device;
}
ALCboolean alcCaptureCloseDevice(ALCdevice *_device)
{
DeviceWrapper *device = _device ? (DeviceWrapper *) _device : &null_device;
ALCboolean retval;
IO_START(alcCaptureCloseDevice);
IO_PTR(_device);
retval = REAL_alcCaptureCloseDevice(device->device);
IO_ALCBOOLEAN(retval);
if (retval == ALC_TRUE) {
if (device != &null_device) {
if (device->next) {
device->next->prev = device->prev;
}
if (device->prev) {
device->prev->next = device->next;
}
}
free(device->extension_string);
free(device);
}
IO_END_ALC(retval == ALC_TRUE ? NULL : device)
return retval;
}
ALCdevice *alcOpenDevice(const ALCchar *devicename)
{
DeviceWrapper *device = (DeviceWrapper *) calloc(1, sizeof (DeviceWrapper));
ALCdevice *retval;
if (!device) {
return NULL;
}
IO_START(alcOpenDevice);
IO_STRING(devicename);
retval = REAL_alcOpenDevice(devicename);
IO_PTR(retval ? device : NULL);
if (!retval) {
free(device);
} else {
ALCint alci = 0;
const ALCchar *alcstr;
device->device = retval;
device->iscapture = ALC_FALSE;
device->connected = ALC_TRUE;
device->supports_disconnect_ext = REAL_alcIsExtensionPresent(device->device, "ALC_EXT_disconnect");
device->next = null_device.next;
device->prev = &null_device;
null_device.next = device;
if (device->next) {
device->next->prev = device;
}
REAL_alcGetIntegerv(device->device, ALC_MAJOR_VERSION, 1, &alci);
IO_INT32(alci);
REAL_alcGetIntegerv(device->device, ALC_MINOR_VERSION, 1, &alci);
IO_INT32(alci);
alcstr = REAL_alcGetString(device->device, ALC_DEVICE_SPECIFIER);
IO_STRING(alcstr);
alcstr = REAL_alcGetString(device->device, ALC_EXTENSIONS);
IO_STRING(alcstr);
}
IO_END_ALC(device);
return (ALCdevice *) device;
}
ALCboolean alcCloseDevice(ALCdevice *_device)
{
DeviceWrapper *device = _device ? (DeviceWrapper *) _device : &null_device;
ALCboolean retval;
IO_START(alcCloseDevice);
IO_PTR(_device);
retval = REAL_alcCloseDevice(device->device);
IO_ALCBOOLEAN(retval);
if (retval == ALC_TRUE) {
if (device != &null_device) {
if (device->next) {
device->next->prev = device->prev;
}
if (device->prev) {
device->prev->next = device->next;
}
}
free(device->extension_string);
free(device);
}
IO_END_ALC(retval == ALC_TRUE ? NULL : device)
return retval;
}
static void check_listener_state_floatv(const ALenum param, const int numfloats, ALfloat *current)
{
if (current_context) {
ALfloat fval[6] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
const size_t size = sizeof (ALfloat) * numfloats;
int i;
REAL_alGetListenerfv(param, fval);
if (memcmp(fval, current, size) != 0) {
IO_EVENTENUM(ALEE_LISTENER_STATE_CHANGED_FLOATV);
IO_PTR(current_context);
IO_ENUM(param);
IO_UINT32((uint32) numfloats);
for (i = 0; i < numfloats; i++) {
IO_FLOAT(fval[i]);
}
memcpy(current, fval, size);
}
}
}
static void check_listener_state(void)
{
ContextWrapper *ctx = current_context;
if (ctx) {
check_listener_state_floatv(AL_POSITION, 3, ctx->listener_position);
check_listener_state_floatv(AL_VELOCITY, 3, ctx->listener_velocity);
check_listener_state_floatv(AL_ORIENTATION, 6, ctx->listener_orientation);
check_listener_state_floatv(AL_GAIN, 1, &ctx->listener_gain);
}
}
static void check_context_state_enum(const ALenum param, ALenum *current)
{
if (current_context) {
ALint ival = 0;
ALenum newval;
REAL_alGetIntegerv(param, &ival);
newval = (ALenum) ival;
if (newval != *current) {
IO_EVENTENUM(ALEE_CONTEXT_STATE_CHANGED_ENUM);
IO_PTR(current_context);
IO_ENUM(param);
IO_ENUM(newval);
*current = newval;
}
}
}
static void check_context_state_float(const ALenum param, ALfloat *current)
{
if (current_context) {
ALfloat fval = 0.0f;
REAL_alGetFloatv(param, &fval);
if (fval != *current) {
IO_EVENTENUM(ALEE_CONTEXT_STATE_CHANGED_FLOAT);
IO_PTR(current_context);
IO_ENUM(param);
IO_FLOAT(fval);
*current = fval;
}
}
}
static void check_context_state(void)
{
ContextWrapper *ctx = current_context;
if (ctx) {
check_context_state_enum(AL_DISTANCE_MODEL, &ctx->distance_model);
check_context_state_float(AL_DOPPLER_FACTOR, &ctx->doppler_factor);
check_context_state_float(AL_DOPPLER_VELOCITY, &ctx->doppler_velocity);
check_context_state_float(AL_SPEED_OF_SOUND, &ctx->speed_of_sound);
check_listener_state();
}
}
ALCcontext *alcCreateContext(ALCdevice *_device, const ALCint* attrlist)
{
DeviceWrapper *device = _device ? (DeviceWrapper *) _device : &null_device;
ContextWrapper *ctx = (ContextWrapper *) calloc(1, sizeof (ContextWrapper));
ALCcontext *retval;
uint32 attrcount = 0;
uint32 i;
if (!ctx) {
out_of_memory();
}
IO_START(alcCreateContext);
IO_PTR(_device);
IO_PTR(attrlist);
if (attrlist) {
while (attrlist[attrcount] != 0) { attrcount += 2; }
attrcount++;
}
IO_UINT32(attrcount);
if (attrlist) {
for (i = 0; i < attrcount; i++) {
IO_INT32(attrlist[i]);
}
}
retval = REAL_alcCreateContext(device->device, attrlist);
IO_PTR(retval ? ctx : NULL);
if (retval == NULL) {
free(ctx);
} else {
ctx->ctx = retval;
ctx->device = device;
ctx->distance_model = AL_INVERSE_DISTANCE_CLAMPED;
ctx->doppler_factor = 1.0f;
ctx->doppler_velocity = 1.0f;
ctx->speed_of_sound = 343.3f;
ctx->listener_gain = 1.0f;
ctx->listener_orientation[2] = -1.0f;
ctx->listener_orientation[4] = 1.0f;
ctx->prev = NULL;
ctx->next = device->contexts;
device->contexts = ctx;
if (ctx->next) {
ctx->next->prev = ctx;
}
}
IO_END_ALC(device);
return (ALCcontext *) ctx;
}
static void query_context_string(ContextWrapper *ctx, const ALenum param)
{
// we currently assume none of these strings change, so we send them
// unconditionally here, having gated this behind a single check elsewhere.
const ALchar *str = REAL_alGetString(param);
IO_EVENTENUM(ALEE_CONTEXT_STATE_CHANGED_STRING);
IO_PTR(ctx);
IO_ENUM(param);
IO_STRING(str);
}
static void query_context_attribs(ContextWrapper *ctx)
{
/* !!! FIXME
if (!ctx->device->iscapture) {
ALint numattr = 0;
REAL_a
case ALC_ATTRIBUTES_SIZE:
case ALC_ALL_ATTRIBUTES:
*/
}
static void check_context_static_state(ContextWrapper *ctx)
{
if (ctx && !ctx->checked_static_state) {
ctx->checked_static_state = AL_TRUE;
query_context_string(ctx, AL_VERSION);
query_context_string(ctx, AL_RENDERER);
query_context_string(ctx, AL_VENDOR);
query_context_string(ctx, AL_EXTENSIONS);
query_context_attribs(ctx);
}
}
ALCboolean alcMakeContextCurrent(ALCcontext *_ctx)
{
ContextWrapper *ctx = (ContextWrapper *) _ctx;
ALCboolean retval;
IO_START(alcMakeContextCurrent);
IO_PTR(ctx);
retval = REAL_alcMakeContextCurrent(ctx ? ctx->ctx : NULL);
IO_ALCBOOLEAN(retval);
if (retval) {
current_context = ctx;
if (ctx) {
check_context_static_state(ctx);
check_context_state();
}
}
IO_END_ALC(current_context ? current_context->device : NULL);
return retval;
}
void alcProcessContext(ALCcontext *_ctx)
{
ContextWrapper *ctx = (ContextWrapper *) _ctx;
IO_START(alcProcessContext);
IO_PTR(ctx);
REAL_alcProcessContext(ctx ? ctx->ctx : NULL);
IO_END_ALC(ctx ? ctx->device : NULL);
}
void alcSuspendContext(ALCcontext *_ctx)
{
ContextWrapper *ctx = (ContextWrapper *) _ctx;
IO_START(alcSuspendContext);
IO_PTR(ctx);
REAL_alcSuspendContext(ctx ? ctx->ctx : NULL);
IO_END_ALC(ctx ? ctx->device : NULL);
}