Skip to content

Commit

Permalink
altrace: initial add, work-in-progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Jul 4, 2019
1 parent f5b36ae commit c9665eb
Show file tree
Hide file tree
Showing 5 changed files with 3,306 additions and 0 deletions.
180 changes: 180 additions & 0 deletions altrace/altrace_common.h
@@ -0,0 +1,180 @@
/**
* MojoAL; a simple drop-in OpenAL implementation.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/

// this is not a generic header that list declarations, it's shared code that
// two source files use. We could clean this up if necessary later.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <dlfcn.h>
#include <errno.h>
#include <time.h>
#include <stdint.h>

#include "../AL/al.h"
#include "../AL/alc.h"

#define ALTRACE_LOG_FILE_MAGIC 0x0104E5A1
#define ALTRACE_LOG_FILE_FORMAT 1

/* AL_EXT_FLOAT32 support... */
#ifndef AL_FORMAT_MONO_FLOAT32
#define AL_FORMAT_MONO_FLOAT32 0x10010
#endif

#ifndef AL_FORMAT_STEREO_FLOAT32
#define AL_FORMAT_STEREO_FLOAT32 0x10011
#endif

/* ALC_EXT_DISCONNECTED support... */
#ifndef ALC_CONNECTED
#define ALC_CONNECTED 0x313
#endif

typedef uint8_t uint8;
typedef int32_t int32;
typedef uint32_t uint32;
typedef uint64_t uint64;
typedef unsigned int uint;

#if defined(__clang__) || defined(__GNUC__)
#define NORETURN __attribute__((noreturn))
#else
#define NORETURN
#endif

#ifdef __linux__
# include <endian.h>
# if __BYTE_ORDER == 4321
# define BIGENDIAN 1
# endif
#elif defined(__hppa__) || defined(__m68k__) || defined(mc68000) || \
defined(_M_M68K) || (defined(__MIPS__) && defined(__MISPEB__)) || \
defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
defined(__sparc__)
# define BIGENDIAN 1
#endif

#ifdef BIGENDIAN
static uint32 swap32(const uint32 x)
{
return (uint32) ( (x << 24) | ((x << 8) & 0x00FF0000) |
((x >> 8) & 0x0000FF00) | (x >> 24) );
}

static uint64 swap64(uint64 x)
{
uint32 hi, lo;
lo = (Uint32) (x & 0xFFFFFFFF);
x >>= 32;
hi = (Uint32) (x & 0xFFFFFFFF);
x = swap32(lo);
x <<= 32;
x |= swap32(hi);
return x;
}
#else
#define swap32(x) (x)
#define swap64(x) (x)
#endif

typedef enum
{
ALEE_EOS,
ALEE_ALERROR_EVENT,
ALEE_ALCERROR_EVENT,
#define ENTRYPOINT(ret,name,params,args) ALEE_##name,
#include "altrace_entrypoints.h"
ALEE_MAX
} EntryEnum;


#define ENTRYPOINT(ret,name,params,args) static ret (*REAL_##name) params = NULL;
#include "altrace_entrypoints.h"

static struct timespec starttime;
static uint32 NOW(void)
{
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) == -1) {
fprintf(stderr, APPNAME ": Failed to get current clock time: %s\n", strerror(errno));
return 0;
}

return (uint32)
( ( (((uint64) ts.tv_sec) * 1000) + (((uint64) ts.tv_nsec) / 1000000) ) -
( (((uint64) starttime.tv_sec) * 1000) + (((uint64) starttime.tv_nsec) / 1000000) ) );
}

static int init_clock(void)
{
if (clock_gettime(CLOCK_MONOTONIC_RAW, &starttime) == -1) {
fprintf(stderr, APPNAME ": Failed to get current clock time: %s\n", strerror(errno));
return 0;
}
usleep(1000); // just so NOW() is (hopefully) never 0
return 1;
}

static int logfd = -1;
static void *realdll = NULL;

static void *loadEntryPoint(void *dll, const char *fnname, int *okay)
{
void *fn = dlsym(dll, fnname);
if (!fn) {
fprintf(stderr, APPNAME ": Real OpenAL library doesn't have entry point '%s'!\n", fnname);
*okay = 0;
}
return fn;
}

static int load_real_openal(void)
{
int okay = 1;
#ifdef __APPLE__
const char *dllname = "libopenal.1.dylib";
#elif defined(_WIN32)
const char *dllname = "openal32.dll";
#else
const char *dllname = "libopenal.so.1";
#endif

realdll = dlopen(dllname, RTLD_NOW | RTLD_LOCAL);
if (!realdll) {
fprintf(stderr, APPNAME ": Failed to load real OpenAL: %s\n", dlerror());
fflush(stderr);
return 0;
}

#define ENTRYPOINT(ret,name,params,args) REAL_##name = (ret (*)params) loadEntryPoint(realdll, #name, &okay);
#include "altrace_entrypoints.h"
return okay;
}

static void close_real_openal(void)
{
void *dll = realdll;

realdll = NULL;

#define ENTRYPOINT(ret,name,params,args) REAL_##name = NULL;
#include "altrace_entrypoints.h"

if (dll) {
dlclose(dll);
}
}

// end of altrace_common.h ...
100 changes: 100 additions & 0 deletions altrace/altrace_entrypoints.h
@@ -0,0 +1,100 @@
#ifndef ENTRYPOINTVOID
#define ENTRYPOINTVOID(name,params,args) ENTRYPOINT(void,name,params,args)
#endif

ENTRYPOINT(ALCcontext *,alcGetCurrentContext,(void),())
ENTRYPOINT(ALCdevice *,alcGetContextsDevice,(ALCcontext *context),(context))
ENTRYPOINT(ALCboolean,alcIsExtensionPresent,(ALCdevice *device, const ALCchar *extname),(device,extname))
ENTRYPOINT(void *,alcGetProcAddress,(ALCdevice *device, const ALCchar *funcname),(device,funcname))
ENTRYPOINT(ALCenum,alcGetEnumValue,(ALCdevice *device, const ALCchar *enumname),(device,enumname))
ENTRYPOINT(const ALCchar *,alcGetString,(ALCdevice *device, ALCenum param),(device,param))
ENTRYPOINT(ALCdevice *,alcCaptureOpenDevice,(const ALCchar *devicename, ALCuint frequency, ALCenum format, ALCsizei buffersize),(devicename,frequency,format,buffersize))
ENTRYPOINT(ALCboolean,alcCaptureCloseDevice,(ALCdevice *device),(device))
ENTRYPOINT(ALCdevice *,alcOpenDevice,(const ALCchar *devicename),(devicename))
ENTRYPOINT(ALCboolean,alcCloseDevice,(ALCdevice *device),(device))
ENTRYPOINT(ALCcontext *,alcCreateContext,(ALCdevice *device, const ALCint* attrlist),(device,attrlist))
ENTRYPOINT(ALCboolean,alcMakeContextCurrent,(ALCcontext *ctx),(ctx))
ENTRYPOINTVOID(alcProcessContext,(ALCcontext *ctx),(ctx))
ENTRYPOINTVOID(alcSuspendContext,(ALCcontext *ctx),(ctx))
ENTRYPOINTVOID(alcDestroyContext,(ALCcontext *ctx),(ctx))
ENTRYPOINT(ALCenum,alcGetError,(ALCdevice *device),(device))
ENTRYPOINTVOID(alcGetIntegerv,(ALCdevice *device, ALCenum param, ALCsizei size, ALCint *values),(device,param,size,values))
ENTRYPOINTVOID(alcCaptureStart,(ALCdevice *device),(device))
ENTRYPOINTVOID(alcCaptureStop,(ALCdevice *device),(device))
ENTRYPOINTVOID(alcCaptureSamples,(ALCdevice *device, ALCvoid *buffer, ALCsizei samples),(device,buffer,samples))
ENTRYPOINTVOID(alDopplerFactor,(ALfloat value),(value))
ENTRYPOINTVOID(alDopplerVelocity,(ALfloat value),(value))
ENTRYPOINTVOID(alSpeedOfSound,(ALfloat value),(value))
ENTRYPOINTVOID(alDistanceModel,(ALenum model),(model))
ENTRYPOINTVOID(alEnable,(ALenum capability),(capability))
ENTRYPOINTVOID(alDisable,(ALenum capability),(capability))
ENTRYPOINT(ALboolean,alIsEnabled,(ALenum capability),(capability))
ENTRYPOINT(const ALchar *,alGetString,(const ALenum param),(param))
ENTRYPOINTVOID(alGetBooleanv,(ALenum param, ALboolean *values),(param,values))
ENTRYPOINTVOID(alGetIntegerv,(ALenum param, ALint *values),(param,values))
ENTRYPOINTVOID(alGetFloatv,(ALenum param, ALfloat *values),(param,values))
ENTRYPOINTVOID(alGetDoublev,(ALenum param, ALdouble *values),(param,values))
ENTRYPOINT(ALboolean,alGetBoolean,(ALenum param),(param))
ENTRYPOINT(ALint,alGetInteger,(ALenum param),(param))
ENTRYPOINT(ALfloat,alGetFloat,(ALenum param),(param))
ENTRYPOINT(ALdouble,alGetDouble,(ALenum param),(param))
ENTRYPOINT(ALboolean,alIsExtensionPresent,(const ALchar *extname),(extname))
ENTRYPOINT(ALenum,alGetError,(void),())
ENTRYPOINT(void *,alGetProcAddress,(const ALchar *funcname),(funcname))
ENTRYPOINT(ALenum,alGetEnumValue,(const ALchar *enumname),(enumname))
ENTRYPOINTVOID(alListenerfv,(ALenum param, const ALfloat *values),(param,values))
ENTRYPOINTVOID(alListenerf,(ALenum param, ALfloat value),(param,value))
ENTRYPOINTVOID(alListener3f,(ALenum param, ALfloat value1, ALfloat value2, ALfloat value3),(param,value1,value2,value3))
ENTRYPOINTVOID(alListeneriv,(ALenum param, const ALint *values),(param,values))
ENTRYPOINTVOID(alListeneri,(ALenum param, ALint value),(param,value))
ENTRYPOINTVOID(alListener3i,(ALenum param, ALint value1, ALint value2, ALint value3),(param,value1,value2,value3))
ENTRYPOINTVOID(alGetListenerfv,(ALenum param, ALfloat *values),(param,values))
ENTRYPOINTVOID(alGetListenerf,(ALenum param, ALfloat *value),(param,value))
ENTRYPOINTVOID(alGetListener3f,(ALenum param, ALfloat *value1, ALfloat *value2, ALfloat *value3),(param,value1,value2,value3))
ENTRYPOINTVOID(alGetListeneri,(ALenum param, ALint *value),(param,value))
ENTRYPOINTVOID(alGetListeneriv,(ALenum param, ALint *values),(param,values))
ENTRYPOINTVOID(alGetListener3i,(ALenum param, ALint *value1, ALint *value2, ALint *value3),(param,value1,value2,value3))
ENTRYPOINTVOID(alGenSources,(ALsizei n, ALuint *names),(n,names))
ENTRYPOINTVOID(alDeleteSources,(ALsizei n, const ALuint *names),(n,names))
ENTRYPOINT(ALboolean,alIsSource,(ALuint name),(name))
ENTRYPOINTVOID(alSourcefv,(ALuint name, ALenum param, const ALfloat *values),(name,param,values))
ENTRYPOINTVOID(alSourcef,(ALuint name, ALenum param, ALfloat value),(name,param,value))
ENTRYPOINTVOID(alSource3f,(ALuint name, ALenum param, ALfloat value1, ALfloat value2, ALfloat value3),(name,param,value1,value2,value3))
ENTRYPOINTVOID(alSourceiv,(ALuint name, ALenum param, const ALint *values),(name,param,values))
ENTRYPOINTVOID(alSourcei,(ALuint name, ALenum param, ALint value),(name,param,value))
ENTRYPOINTVOID(alSource3i,(ALuint name, ALenum param, ALint value1, ALint value2, ALint value3),(name,param,value1,value2,value3))
ENTRYPOINTVOID(alGetSourcefv,(ALuint name, ALenum param, ALfloat *values),(name,param,values))
ENTRYPOINTVOID(alGetSourcef,(ALuint name, ALenum param, ALfloat *value),(name,param,value))
ENTRYPOINTVOID(alGetSource3f,(ALuint name, ALenum param, ALfloat *value1, ALfloat *value2, ALfloat *value3),(name,param,value1,value2,value3))
ENTRYPOINTVOID(alGetSourceiv,(ALuint name, ALenum param, ALint *values),(name,param,values))
ENTRYPOINTVOID(alGetSourcei,(ALuint name, ALenum param, ALint *value),(name,param,value))
ENTRYPOINTVOID(alGetSource3i,(ALuint name, ALenum param, ALint *value1, ALint *value2, ALint *value3),(name,param,value1,value2,value3))
ENTRYPOINTVOID(alSourcePlay,(ALuint name),(name))
ENTRYPOINTVOID(alSourcePlayv,(ALsizei n, const ALuint *names),(n, names))
ENTRYPOINTVOID(alSourcePause,(ALuint name),(name))
ENTRYPOINTVOID(alSourcePausev,(ALsizei n, const ALuint *names),(n, names))
ENTRYPOINTVOID(alSourceRewind,(ALuint name),(name))
ENTRYPOINTVOID(alSourceRewindv,(ALsizei n, const ALuint *names),(n, names))
ENTRYPOINTVOID(alSourceStop,(ALuint name),(name))
ENTRYPOINTVOID(alSourceStopv,(ALsizei n, const ALuint *names),(n, names))
ENTRYPOINTVOID(alSourceQueueBuffers,(ALuint name, ALsizei nb, const ALuint *bufnames),(name,nb,bufnames))
ENTRYPOINTVOID(alSourceUnqueueBuffers,(ALuint name, ALsizei nb, ALuint *bufnames),(name,nb,bufnames))
ENTRYPOINTVOID(alGenBuffers,(ALsizei n, ALuint *names),(n,names))
ENTRYPOINTVOID(alDeleteBuffers,(ALsizei n, const ALuint *names),(n,names))
ENTRYPOINT(ALboolean,alIsBuffer,(ALuint name),(name))
ENTRYPOINTVOID(alBufferData,(ALuint name, ALenum alfmt, const ALvoid *data, ALsizei size, ALsizei freq),(name,alfmt,data,size,freq))
ENTRYPOINTVOID(alBufferfv,(ALuint name, ALenum param, const ALfloat *values),(name,param,values))
ENTRYPOINTVOID(alBufferf,(ALuint name, ALenum param, ALfloat value),(name,param,value))
ENTRYPOINTVOID(alBuffer3f,(ALuint name, ALenum param, ALfloat value1, ALfloat value2, ALfloat value3),(name,param,value1,value2,value3))
ENTRYPOINTVOID(alBufferiv,(ALuint name, ALenum param, const ALint *values),(name,param,values))
ENTRYPOINTVOID(alBufferi,(ALuint name, ALenum param, ALint value),(name,param,value))
ENTRYPOINTVOID(alBuffer3i,(ALuint name, ALenum param, ALint value1, ALint value2, ALint value3),(name,param,value1,value2,value3))
ENTRYPOINTVOID(alGetBufferfv,(ALuint name, ALenum param, ALfloat *values),(name,param,values))
ENTRYPOINTVOID(alGetBufferf,(ALuint name, ALenum param, ALfloat *value),(name,param,value))
ENTRYPOINTVOID(alGetBuffer3f,(ALuint name, ALenum param, ALfloat *value1, ALfloat *value2, ALfloat *value3),(name,param,value1,value2,value3))
ENTRYPOINTVOID(alGetBufferi,(ALuint name, ALenum param, ALint *value),(name,param,value))
ENTRYPOINTVOID(alGetBuffer3i,(ALuint name, ALenum param, ALint *value1, ALint *value2, ALint *value3),(name,param,value1,value2,value3))
ENTRYPOINTVOID(alGetBufferiv,(ALuint name, ALenum param, ALint *values),(name,param,values))
#undef ENTRYPOINT
#undef ENTRYPOINTVOID

0 comments on commit c9665eb

Please sign in to comment.