/* Copyright (c) 2014 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "inputcontroller/InputRecorder.h" #include "inputcontroller/InputSession.h" #include "utilities/IOUtilities.h" #include #include #define stemobject_implementation InputRecorder stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_end(); InputRecorder * InputRecorder_createWithFileOutput(InputController * inputController, const void * replayStartupData, uint32_t replayStartupDataSize, const char * filePath) { stemobject_create_implementation(initWithFileOutput, inputController, replayStartupData, replayStartupDataSize, filePath) } InputRecorder * InputRecorder_createWithMemwriteOutput(InputController * inputController, const void * replayStartupData, uint32_t replayStartupDataSize) { stemobject_create_implementation(initWithMemwriteOutput, inputController, replayStartupData, replayStartupDataSize) } static void writeData(InputRecorder * self, unsigned int size, const void * data) { if (self->outputFile != NULL) { fwrite(data, 1, size, self->outputFile); } else if (self->memwriteContext.data != NULL || self->memwriteContext.realloc) { memwrite(&self->memwriteContext, size, data); } } static void writeUInt8(InputRecorder * self, uint8_t uint8) { writeData(self, 1, &uint8); } static void writeUInt16(InputRecorder * self, uint16_t uint16) { #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ uint16 = swapEndian16(uint16); #endif writeData(self, 2, &uint16); } static void writeUInt32(InputRecorder * self, uint32_t uint32) { #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ uint32 = swapEndian32(uint32); #endif writeData(self, 4, &uint32); } static bool writeAction(InputRecorder * self, Atom actionID, enum InputSessionEventType eventType) { for (unsigned int actionIndex = 0; actionIndex < self->inputController->actionCount; actionIndex++) { if (self->inputController->actions[actionIndex].actionID == actionID) { writeUInt32(self, self->frameIndex - self->lastFrameIndex); writeUInt16(self, actionIndex); writeUInt8(self, eventType); self->lastFrameIndex = self->frameIndex; return true; } } #ifdef DEBUG fprintf(stderr, "Warning: Unrecognized actionID \"%s\" %s in InputRecorder\n", actionID, eventType == INPUT_SESSION_EVENT_ACTION_DOWN ? "down" : eventType == INPUT_SESSION_EVENT_ACTION_REPEAT ? "repeat" : "up"); #endif return false; } static bool actionDownCallback(Atom eventID, void * eventData, void * context) { InputRecorder * self = context; struct InputController_event * event = eventData; return writeAction(self, event->actionID, INPUT_SESSION_EVENT_ACTION_DOWN); } static bool actionUpCallback(Atom eventID, void * eventData, void * context) { InputRecorder * self = context; struct InputController_event * event = eventData; return writeAction(self, event->actionID, INPUT_SESSION_EVENT_ACTION_UP); } static bool actionRepeatCallback(Atom eventID, void * eventData, void * context) { InputRecorder * self = context; struct InputController_event * event = eventData; return writeAction(self, event->actionID, INPUT_SESSION_EVENT_ACTION_REPEAT); } static bool motionCallback(Atom eventID, void * eventData, void * context) { InputRecorder * self = context; struct InputController_motionEvent * event = eventData; for (unsigned int actionIndex = 0; actionIndex < self->inputController->actionCount; actionIndex++) { if (self->inputController->actions[actionIndex].actionID == event->actionID) { writeUInt32(self, self->frameIndex - self->lastFrameIndex); writeUInt16(self, actionIndex); writeUInt8(self, INPUT_SESSION_EVENT_MOTION); union {uint32_t uint32; fixed16_16 fixed;} fixedUnion; fixedUnion.fixed = event->valueX; writeUInt32(self, fixedUnion.uint32); fixedUnion.fixed = event->valueY; writeUInt32(self, fixedUnion.uint32); fixedUnion.fixed = event->rawValueX; writeUInt32(self, fixedUnion.uint32); fixedUnion.fixed = event->rawValueY; writeUInt32(self, fixedUnion.uint32); self->lastFrameIndex = self->frameIndex; return true; } } #ifdef DEBUG fprintf(stderr, "Warning: Unrecognized actionID \"%s\" moved in InputRecorder\n", event->actionID); #endif return false; } static void sharedInit(InputRecorder * self, InputController * inputController) { call_super(init, self); self->inputController = inputController; self->frameIndex = 0; self->lastFrameIndex = 0; if (self->inputController != NULL) { EventDispatcher_registerForEvent(self->inputController->eventDispatcher, ATOM(INPUT_CONTROLLER_EVENT_ACTION_DOWN), actionDownCallback, self); EventDispatcher_registerForEvent(self->inputController->eventDispatcher, ATOM(INPUT_CONTROLLER_EVENT_ACTION_UP), actionUpCallback, self); EventDispatcher_registerForEvent(self->inputController->eventDispatcher, ATOM(INPUT_CONTROLLER_EVENT_ACTION_REPEAT), actionRepeatCallback, self); EventDispatcher_registerForEvent(self->inputController->eventDispatcher, ATOM(INPUT_CONTROLLER_EVENT_MOTION), motionCallback, self); } } static void writeHeader(InputRecorder * self, const void * replayStartupData, uint32_t replayStartupDataSize) { writeUInt16(self, INPUT_SESSION_FORMAT_VERSION); writeUInt32(self, replayStartupDataSize); writeData(self, replayStartupDataSize, replayStartupData); if (self->inputController == NULL) { writeUInt16(self, 0); } else { writeUInt16(self, self->inputController->actionCount); for (unsigned int actionIndex = 0; actionIndex < self->inputController->actionCount; actionIndex++) { writeData(self, strlen(self->inputController->actions[actionIndex].actionID) + 1, self->inputController->actions[actionIndex].actionID); } } } bool InputRecorder_initWithFileOutput(InputRecorder * self, InputController * inputController, const void * replayStartupData, uint32_t replayStartupDataSize, const char * filePath) { sharedInit(self, inputController); self->memwriteContext.data = NULL; self->memwriteContext.realloc = false; self->outputFile = fopen(filePath, "wb"); if (self->outputFile == NULL) { InputRecorder_dispose(self); return false; } setvbuf(self->outputFile, NULL, _IONBF, 0); writeHeader(self, replayStartupData, replayStartupDataSize); return true; } bool InputRecorder_initWithMemwriteOutput(InputRecorder * self, InputController * inputController, const void * replayStartupData, uint32_t replayStartupDataSize) { sharedInit(self, inputController); self->outputFile = NULL; self->memwriteContext = memwriteContextInit(NULL, 0, 0, true); writeHeader(self, replayStartupData, replayStartupDataSize); return true; } void InputRecorder_dispose(InputRecorder * self) { call_super(dispose, self); if (self->inputController != NULL) { EventDispatcher_unregisterForEvent(self->inputController->eventDispatcher, ATOM(INPUT_CONTROLLER_EVENT_ACTION_DOWN), actionDownCallback, self); EventDispatcher_unregisterForEvent(self->inputController->eventDispatcher, ATOM(INPUT_CONTROLLER_EVENT_ACTION_UP), actionUpCallback, self); EventDispatcher_unregisterForEvent(self->inputController->eventDispatcher, ATOM(INPUT_CONTROLLER_EVENT_ACTION_REPEAT), actionRepeatCallback, self); EventDispatcher_unregisterForEvent(self->inputController->eventDispatcher, ATOM(INPUT_CONTROLLER_EVENT_MOTION), motionCallback, self); } if (self->outputFile == NULL) { free(self->memwriteContext.data); } else { fclose(self->outputFile); } } void InputRecorder_nextFrame(InputRecorder * self) { self->frameIndex++; } void InputRecorder_rewriteReplayStartupData(InputRecorder * self, const void * replayStartupData, uint32_t replayStartupDataSize) { if (self->outputFile != NULL) { long position = ftell(self->outputFile); fseek(self->outputFile, 6, SEEK_SET); writeData(self, replayStartupDataSize, replayStartupData); fseek(self->outputFile, position, SEEK_SET); } else { size_t position = self->memwriteContext.position; self->memwriteContext.position = 6; writeData(self, replayStartupDataSize, replayStartupData); self->memwriteContext.position = position; } }