aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrad Davis <[email protected]>2014-08-11 22:05:38 -0700
committerBrad Davis <[email protected]>2014-08-11 22:05:38 -0700
commit06dc0c027cb2ecb5419c893681455ac183301a24 (patch)
tree9145de0897494ad321e88640200ddbd5b1a8ddab
parent04a03e83f135fbfccbc960b13e05729490ccd375 (diff)
Adding mac 0.4.1 files0.4.1
-rw-r--r--LibOVR/Src/Displays/OVR_OSX_Display.cpp356
-rw-r--r--LibOVR/Src/Displays/OVR_OSX_Display.h139
-rw-r--r--LibOVR/Src/Kernel/OVR_ThreadsPthread.cpp842
-rw-r--r--LibOVR/Src/Net/OVR_Unix_Socket.cpp586
-rw-r--r--LibOVR/Src/Net/OVR_Unix_Socket.h152
-rw-r--r--Samples/CommonSrc/Platform/OSX_Gamepad.cpp430
-rw-r--r--Samples/CommonSrc/Platform/OSX_Gamepad.h66
-rw-r--r--Samples/CommonSrc/Platform/OSX_Platform.h113
-rw-r--r--Samples/CommonSrc/Platform/OSX_Platform.mm553
-rw-r--r--Samples/CommonSrc/Platform/OSX_PlatformObjc.h53
-rw-r--r--Samples/CommonSrc/Platform/OSX_WavPlayer.cpp250
-rw-r--r--Samples/CommonSrc/Platform/OSX_WavPlayer.h72
12 files changed, 3612 insertions, 0 deletions
diff --git a/LibOVR/Src/Displays/OVR_OSX_Display.cpp b/LibOVR/Src/Displays/OVR_OSX_Display.cpp
new file mode 100644
index 0000000..4cd9307
--- /dev/null
+++ b/LibOVR/Src/Displays/OVR_OSX_Display.cpp
@@ -0,0 +1,356 @@
+/************************************************************************************
+
+Filename : OVR_OSX_Display.cpp
+Content : OSX-specific Display declarations
+Created : July 2, 2014
+Authors : James Hughes
+
+Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved.
+
+Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License");
+you may not use the Oculus VR Rift SDK except in compliance with the License,
+which is provided at the time of installation or download, or which
+otherwise accompanies this software in either electronic or hard copy form.
+
+You may obtain a copy of the License at
+
+http://www.oculusvr.com/licenses/LICENSE-3.1
+
+Unless required by applicable law or agreed to in writing, the Oculus VR SDK
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+*************************************************************************************/
+
+#include "OVR_OSX_Display.h"
+#include "../Kernel/OVR_Log.h"
+
+#include <ApplicationServices/ApplicationServices.h>
+#include <CoreFoundation/CoreFoundation.h>
+#include <CoreFoundation/CFString.h>
+#include <IOKit/graphics/IOGraphicsLib.h>
+
+//-------------------------------------------------------------------------------------
+// ***** Display enumeration Helpers
+
+namespace OVR {
+
+// FIXME Code duplication with windows.
+#define EDID_LENGTH 0x80
+
+#define EDID_HEADER 0x00
+#define EDID_HEADER_END 0x07
+
+#define ID_MANUFACTURER_NAME 0x08
+#define ID_MANUFACTURER_NAME_END 0x09
+
+#define EDID_STRUCT_VERSION 0x12
+#define EDID_STRUCT_REVISION 0x13
+
+#define ESTABLISHED_TIMING_1 0x23
+#define ESTABLISHED_TIMING_2 0x24
+#define MANUFACTURERS_TIMINGS 0x25
+
+#define DETAILED_TIMING_DESCRIPTIONS_START 0x36
+#define DETAILED_TIMING_DESCRIPTION_SIZE 18
+#define NO_DETAILED_TIMING_DESCRIPTIONS 4
+
+#define DETAILED_TIMING_DESCRIPTION_1 0x36
+#define DETAILED_TIMING_DESCRIPTION_2 0x48
+#define DETAILED_TIMING_DESCRIPTION_3 0x5a
+#define DETAILED_TIMING_DESCRIPTION_4 0x6c
+
+#define MONITOR_NAME 0xfc
+#define MONITOR_LIMITS 0xfd
+#define MONITOR_SERIAL 0xff
+
+#define UNKNOWN_DESCRIPTOR -1
+#define DETAILED_TIMING_BLOCK -2
+
+#define DESCRIPTOR_DATA 5
+
+const UByte edid_v1_header[] = { 0x00, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0x00 };
+
+const UByte edid_v1_descriptor_flag[] = { 0x00, 0x00 };
+
+// FIXME Code duplication with windows. Refactor.
+static int blockType( UByte* block )
+{
+ if ( !strncmp( (const char*)edid_v1_descriptor_flag, (const char*)block, 2 ) )
+ {
+ // descriptor
+ if ( block[ 2 ] != 0 )
+ return UNKNOWN_DESCRIPTOR;
+ return block[ 3 ];
+ }
+ else
+ {
+ return DETAILED_TIMING_BLOCK;
+ }
+}
+
+static char* getMonitorName( UByte const* block )
+{
+ static char name[ 13 ];
+ unsigned i;
+ UByte const* ptr = block + DESCRIPTOR_DATA;
+
+ for( i = 0; i < 13; i++, ptr++ )
+ {
+ if ( *ptr == 0xa )
+ {
+ name[ i ] = 0;
+ return name;
+ }
+
+ name[ i ] = *ptr;
+ }
+
+ return name;
+}
+
+// FIXME Code duplication with windows. Refactor.
+static bool parseEdid( UByte* edid, OVR::OSX::DisplayEDID& edidResult )
+{
+ unsigned i;
+ UByte* block;
+ const char* monitor_name = "Unknown";
+ UByte checksum = 0;
+
+ for( i = 0; i < EDID_LENGTH; i++ )
+ checksum += edid[ i ];
+
+ // Bad checksum, fail EDID
+ if ( checksum != 0 )
+ return false;
+
+ if ( strncmp( (const char*)edid+EDID_HEADER, (const char*)edid_v1_header, EDID_HEADER_END+1 ) )
+ {
+ // First bytes don't match EDID version 1 header
+ return false;
+ }
+
+
+ // OVR_DEBUG_LOG_TEXT(( "\n# EDID version %d revision %d\n",
+ // (int)edid[EDID_STRUCT_VERSION],(int)edid[EDID_STRUCT_REVISION] ));
+
+ // Monitor name and timings
+
+ char serialNumber[14];
+ memset( serialNumber, 0, 14 );
+
+ block = edid + DETAILED_TIMING_DESCRIPTIONS_START;
+
+ for( i = 0; i < NO_DETAILED_TIMING_DESCRIPTIONS; i++,
+ block += DETAILED_TIMING_DESCRIPTION_SIZE )
+ {
+
+ if ( blockType( block ) == MONITOR_NAME )
+ {
+ monitor_name = getMonitorName( block );
+ }
+
+ if( blockType( block ) == MONITOR_SERIAL )
+ {
+ memcpy( serialNumber, block + 5, 13 );
+ break;
+ }
+ }
+
+ UByte vendorString[4] = {0};
+
+ vendorString[0] = (edid[8] >> 2 & 31) + 64;
+ vendorString[1] = ((edid[8] & 3) << 3) | (edid[9] >> 5) + 64;
+ vendorString[2] = (edid[9] & 31) + 64;
+
+ edidResult.ModelNumber = *(UInt16*)&edid[10];
+ edidResult.MonitorName = OVR::String(monitor_name);
+ edidResult.VendorName = OVR::String((const char*)vendorString);
+ edidResult.SerialNumber = OVR::String(serialNumber);
+
+ // printf( "\tIdentifier \"%s\"\n", monitor_name );
+ // printf( "\tVendorName \"%s\"\n", vendorString );
+ // printf( "\tModelName \"%s\"\n", monitor_name );
+ // printf( "\tModelNumber %d\n", edidResult.ModelNumber );
+ // printf( "\tSerialNumber \"%s\"\n", edidResult.SerialNumber.ToCStr() );
+
+ // FIXME: Get timings as well, though they aren't very useful here
+ // except for the vertical refresh rate, presumably
+
+ return true;
+}
+
+static int discoverExtendedRifts(OVR::OSX::DisplayDesc* descriptorArray, int inputArraySize, bool edidInfo)
+{
+ OVR_UNUSED(edidInfo);
+ int result = 0;
+
+ static bool reportDiscovery = true;
+ OVR_UNUSED(reportDiscovery);
+
+ CGDirectDisplayID Displays[32];
+ uint32_t NDisplays = 0;
+ CGGetOnlineDisplayList(32, Displays, &NDisplays);
+
+ for (unsigned int i = 0; i < NDisplays; i++)
+ {
+ io_service_t port = CGDisplayIOServicePort(Displays[i]);
+ CFDictionaryRef DispInfo = IODisplayCreateInfoDictionary(port, kNilOptions);
+
+ // Display[i]
+
+ uint32_t vendor = CGDisplayVendorNumber(Displays[i]);
+ uint32_t product = CGDisplayModelNumber(Displays[i]);
+
+ CGRect desktop = CGDisplayBounds(Displays[i]);
+ Vector2i desktopOffset(desktop.origin.x, desktop.origin.y);
+
+ if (vendor == 16082 && ( (product == 1)||(product == 2)||(product == 3) ) ) // 7" or HD
+ {
+ if( result >= inputArraySize ) { return result; }
+
+ Sizei monitorResolution(1280, 800);
+
+ // Obtain and parse EDID data.
+ CFDataRef data =
+ (CFDataRef)CFDictionaryGetValue(DispInfo, CFSTR(kIODisplayEDIDKey));
+ if (!data)
+ {
+ CFRelease(DispInfo);
+ OVR::LogError("[OSX Display] Unable to obtain EDID for Oculus product %d", product);
+ continue;
+ }
+ UByte* edid = (UByte*)CFDataGetBytePtr(data);
+ OSX::DisplayEDID edidResult;
+ parseEdid( edid, edidResult );
+
+ OVR::OSX::DisplayDesc& desc = descriptorArray[result++];
+ desc.DisplayID = Displays[i];
+ desc.ModelName = edidResult.MonitorName; // User friendly string.
+ desc.EdidSerialNumber = edidResult.SerialNumber;
+ desc.LogicalResolutionInPixels = monitorResolution;
+ desc.DesktopDisplayOffset = desktopOffset;
+
+ switch (product)
+ {
+ case 3: desc.DeviceTypeGuess = HmdType_DK2; break;
+ case 2: desc.DeviceTypeGuess = HmdType_DKHDProto; break;
+ case 1: desc.DeviceTypeGuess = HmdType_DK1; break;
+
+ default:
+ case 0: desc.DeviceTypeGuess = HmdType_Unknown; break;
+ }
+
+ // Hard-coded defaults in case the device doesn't have the data itself.
+ // DK2 prototypes (0003) or DK HD Prototypes (0002)
+ if (product == 3 || product == 2)
+ {
+ desc.LogicalResolutionInPixels = Sizei(1920, 1080);
+ desc.NativeResolutionInPixels = Sizei(1080, 1920);
+ }
+ else
+ {
+ desc.LogicalResolutionInPixels = monitorResolution;
+ desc.NativeResolutionInPixels = monitorResolution;
+ }
+
+ //OVR_DEBUG_LOG_TEXT(("Display Found %x:%x\n", vendor, product));
+ }
+ CFRelease(DispInfo);
+ }
+
+ return result;
+}
+
+
+//-------------------------------------------------------------------------------------
+// ***** Display
+
+bool Display::Initialize()
+{
+ // Nothing to initialize. OS X only supports compatibility mode.
+ return true;
+}
+
+DisplaySearchHandle* Display::GetDisplaySearchHandle()
+{
+ return new OSX::OSXDisplaySearchHandle();
+}
+
+bool Display::InCompatibilityMode( bool displaySearch )
+{
+ OVR_UNUSED( displaySearch );
+ return true;
+}
+
+int Display::GetDisplayCount( DisplaySearchHandle* handle, bool extended, bool applicationOnly, bool edidInfo )
+{
+ OVR_UNUSED(applicationOnly);
+
+ static int extendedCount = -1;
+
+ OSX::OSXDisplaySearchHandle* localHandle = (OSX::OSXDisplaySearchHandle*)handle;
+ if (localHandle == NULL)
+ {
+ OVR::LogError("[OSX Display] No search handle passed into GetDisplayCount. Return 0 rifts.");
+ return 0;
+ }
+
+ if (extendedCount == -1 || extended)
+ {
+ extendedCount = discoverExtendedRifts(localHandle->cachedDescriptorArray, OSX::OSXDisplaySearchHandle::DescArraySize, edidInfo);
+ }
+
+ localHandle->extended = true;
+ localHandle->extendedDisplayCount = extendedCount;
+ int totalCount = extendedCount;
+
+ /// FIXME: Implement application mode for OS X.
+ localHandle->application = false;
+ localHandle->applicationDisplayCount = 0;
+
+ localHandle->displayCount = totalCount;
+
+ return totalCount;
+}
+
+Ptr<Display> Display::GetDisplay( int index, DisplaySearchHandle* handle )
+{
+ Ptr<Display> result = NULL;
+
+ if (index < 0)
+ {
+ OVR::LogError("[OSX Display] Invalid index given to GetDisplay.");
+ return NULL;
+ }
+
+ OSX::OSXDisplaySearchHandle* localHandle = (OSX::OSXDisplaySearchHandle*)handle;
+ if (localHandle == NULL)
+ {
+ OVR::LogError("[OSX Display] No search handle passed into GetDisplay. Return 0 rifts.");
+ return NULL;
+ }
+
+ if (localHandle->extended)
+ {
+ if (index >= 0 && index < (int)localHandle->extendedDisplayCount)
+ {
+ return *new OSX::OSXDisplayGeneric(localHandle->cachedDescriptorArray[index]);
+ }
+
+ index -= localHandle->extendedDisplayCount;
+ }
+
+ if (localHandle->application)
+ {
+ OVR::LogError("[OSX Display] Mac does not support application displays.");
+ }
+
+ return result;
+}
+
+
+} // namespace OVR
diff --git a/LibOVR/Src/Displays/OVR_OSX_Display.h b/LibOVR/Src/Displays/OVR_OSX_Display.h
new file mode 100644
index 0000000..1401121
--- /dev/null
+++ b/LibOVR/Src/Displays/OVR_OSX_Display.h
@@ -0,0 +1,139 @@
+/************************************************************************************
+
+Filename : OVR_OSX_Display.h
+Content : OSX-specific Display declarations
+Created : July 2, 2014
+Authors : James Hughes
+
+Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved.
+
+Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License");
+you may not use the Oculus VR Rift SDK except in compliance with the License,
+which is provided at the time of installation or download, or which
+otherwise accompanies this software in either electronic or hard copy form.
+
+You may obtain a copy of the License at
+
+http://www.oculusvr.com/licenses/LICENSE-3.1
+
+Unless required by applicable law or agreed to in writing, the Oculus VR SDK
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+*************************************************************************************/
+
+#ifndef OVR_OSX_Display_h
+#define OVR_OSX_Display_h
+
+#include "OVR_Display.h"
+
+namespace OVR { namespace OSX {
+
+
+//-------------------------------------------------------------------------------------
+// DisplayDesc
+
+// Display information enumerable through OS .
+// TBD: Should we just move this to public header, so it's a const member of Display?
+struct DisplayDesc
+{
+ DisplayDesc() :
+ DeviceTypeGuess(HmdType_None),
+ DisplayID(0),
+ LogicalResolutionInPixels(0),
+ NativeResolutionInPixels(0)
+ {}
+
+ HmdTypeEnum DeviceTypeGuess;
+ uint32_t DisplayID; // This is the device identifier string from MONITORINFO (for app usage)
+ String ModelName; // This is a "DK2" type string
+ String EdidSerialNumber;
+ Sizei LogicalResolutionInPixels;
+ Sizei NativeResolutionInPixels;
+ Vector2i DesktopDisplayOffset;
+};
+
+
+//-------------------------------------------------------------------------------------
+// DisplayEDID
+
+// Describes EDID information as reported from our display driver.
+struct DisplayEDID
+{
+ DisplayEDID() :
+ ModelNumber(0)
+ {}
+
+ String MonitorName;
+ UInt16 ModelNumber;
+ String VendorName;
+ String SerialNumber;
+};
+
+//-------------------------------------------------------------------------------------
+// OSX Display Search Handle
+class OSXDisplaySearchHandle : public DisplaySearchHandle
+{
+public:
+ OSXDisplaySearchHandle() :
+ extended(false),
+ application(false),
+ extendedDisplayCount(0),
+ applicationDisplayCount(0),
+ displayCount(0)
+ {}
+ virtual ~OSXDisplaySearchHandle() {}
+
+ static const int DescArraySize = 16;
+
+ OSX::DisplayDesc cachedDescriptorArray[DescArraySize];
+ bool extended;
+ bool application;
+ int extendedDisplayCount;
+ int applicationDisplayCount;
+ int displayCount;
+};
+
+//-------------------------------------------------------------------------------------
+// OSXDisplayGeneric
+
+// Describes OSX display in Compatibility mode, containing basic data
+class OSXDisplayGeneric : public Display
+{
+public:
+ OSXDisplayGeneric( const DisplayDesc& dd ) :
+ Display(dd.DeviceTypeGuess,
+ dd.DisplayID,
+ dd.ModelName,
+ dd.EdidSerialNumber,
+ dd.LogicalResolutionInPixels,
+ dd.NativeResolutionInPixels,
+ dd.DesktopDisplayOffset,
+ 0,
+ 0,
+ false)
+ {
+ }
+
+ virtual ~OSXDisplayGeneric()
+ {
+ }
+
+ virtual bool InCompatibilityMode() const
+ {
+ return true;
+ }
+
+ // Generic displays are not capable of mirroring
+ virtual MirrorMode SetMirrorMode( MirrorMode newMode )
+ {
+ OVR_UNUSED( newMode );
+ return MirrorDisabled;
+ }
+};
+
+}} // namespace OVR::OSX
+
+#endif // OVR_OSX_Display_h
diff --git a/LibOVR/Src/Kernel/OVR_ThreadsPthread.cpp b/LibOVR/Src/Kernel/OVR_ThreadsPthread.cpp
new file mode 100644
index 0000000..83d3606
--- /dev/null
+++ b/LibOVR/Src/Kernel/OVR_ThreadsPthread.cpp
@@ -0,0 +1,842 @@
+/************************************************************************************
+
+Filename : OVR_ThreadsPthread.cpp
+Content :
+Created :
+Notes :
+
+Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved.
+
+Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License");
+you may not use the Oculus VR Rift SDK except in compliance with the License,
+which is provided at the time of installation or download, or which
+otherwise accompanies this software in either electronic or hard copy form.
+
+You may obtain a copy of the License at
+
+http://www.oculusvr.com/licenses/LICENSE-3.1
+
+Unless required by applicable law or agreed to in writing, the Oculus VR SDK
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+************************************************************************************/
+
+#include "OVR_Threads.h"
+#include "OVR_Hash.h"
+
+#ifdef OVR_ENABLE_THREADS
+
+#include "OVR_Timer.h"
+#include "OVR_Log.h"
+
+#include <pthread.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <errno.h>
+
+
+namespace OVR {
+
+// ***** Mutex implementation
+
+
+// *** Internal Mutex implementation structure
+
+class MutexImpl : public NewOverrideBase
+{
+ // System mutex or semaphore
+ pthread_mutex_t SMutex;
+ bool Recursive;
+ unsigned LockCount;
+ pthread_t LockedBy;
+
+ friend class WaitConditionImpl;
+
+public:
+ // Constructor/destructor
+ MutexImpl(Mutex* pmutex, bool recursive = 1);
+ ~MutexImpl();
+
+ // Locking functions
+ void DoLock();
+ bool TryLock();
+ void Unlock(Mutex* pmutex);
+ // Returns 1 if the mutes is currently locked
+ bool IsLockedByAnotherThread(Mutex* pmutex);
+ bool IsSignaled() const;
+};
+
+pthread_mutexattr_t Lock::RecursiveAttr;
+bool Lock::RecursiveAttrInit = 0;
+
+// *** Constructor/destructor
+MutexImpl::MutexImpl(Mutex* pmutex, bool recursive)
+{
+ OVR_UNUSED(pmutex);
+ Recursive = recursive;
+ LockCount = 0;
+
+ if (Recursive)
+ {
+ if (!Lock::RecursiveAttrInit)
+ {
+ pthread_mutexattr_init(&Lock::RecursiveAttr);
+ pthread_mutexattr_settype(&Lock::RecursiveAttr, PTHREAD_MUTEX_RECURSIVE);
+ Lock::RecursiveAttrInit = 1;
+ }
+
+ pthread_mutex_init(&SMutex, &Lock::RecursiveAttr);
+ }
+ else
+ pthread_mutex_init(&SMutex, 0);
+}
+
+MutexImpl::~MutexImpl()
+{
+ pthread_mutex_destroy(&SMutex);
+}
+
+
+// Lock and try lock
+void MutexImpl::DoLock()
+{
+ while (pthread_mutex_lock(&SMutex))
+ ;
+ LockCount++;
+ LockedBy = pthread_self();
+}
+
+bool MutexImpl::TryLock()
+{
+ if (!pthread_mutex_trylock(&SMutex))
+ {
+ LockCount++;
+ LockedBy = pthread_self();
+ return 1;
+ }
+
+ return 0;
+}
+
+void MutexImpl::Unlock(Mutex* pmutex)
+{
+ OVR_UNUSED(pmutex);
+ OVR_ASSERT(pthread_self() == LockedBy && LockCount > 0);
+
+ unsigned lockCount;
+ LockCount--;
+ lockCount = LockCount;
+
+ pthread_mutex_unlock(&SMutex);
+}
+
+bool MutexImpl::IsLockedByAnotherThread(Mutex* pmutex)
+{
+ OVR_UNUSED(pmutex);
+ // There could be multiple interpretations of IsLocked with respect to current thread
+ if (LockCount == 0)
+ return 0;
+ if (pthread_self() != LockedBy)
+ return 1;
+ return 0;
+}
+
+bool MutexImpl::IsSignaled() const
+{
+ // An mutex is signaled if it is not locked ANYWHERE
+ // Note that this is different from IsLockedByAnotherThread function,
+ // that takes current thread into account
+ return LockCount == 0;
+}
+
+
+// *** Actual Mutex class implementation
+
+Mutex::Mutex(bool recursive)
+{
+ // NOTE: RefCount mode already thread-safe for all waitables.
+ pImpl = new MutexImpl(this, recursive);
+}
+
+Mutex::~Mutex()
+{
+ delete pImpl;
+}
+
+// Lock and try lock
+void Mutex::DoLock()
+{
+ pImpl->DoLock();
+}
+bool Mutex::TryLock()
+{
+ return pImpl->TryLock();
+}
+void Mutex::Unlock()
+{
+ pImpl->Unlock(this);
+}
+bool Mutex::IsLockedByAnotherThread()
+{
+ return pImpl->IsLockedByAnotherThread(this);
+}
+
+
+
+//-----------------------------------------------------------------------------------
+// ***** Event
+
+bool Event::Wait(unsigned delay)
+{
+ Mutex::Locker lock(&StateMutex);
+
+ // Do the correct amount of waiting
+ if (delay == OVR_WAIT_INFINITE)
+ {
+ while(!State)
+ StateWaitCondition.Wait(&StateMutex);
+ }
+ else if (delay)
+ {
+ if (!State)
+ StateWaitCondition.Wait(&StateMutex, delay);
+ }
+
+ bool state = State;
+ // Take care of temporary 'pulsing' of a state
+ if (Temporary)
+ {
+ Temporary = false;
+ State = false;
+ }
+ return state;
+}
+
+void Event::updateState(bool newState, bool newTemp, bool mustNotify)
+{
+ Mutex::Locker lock(&StateMutex);
+ State = newState;
+ Temporary = newTemp;
+ if (mustNotify)
+ StateWaitCondition.NotifyAll();
+}
+
+
+
+// ***** Wait Condition Implementation
+
+// Internal implementation class
+class WaitConditionImpl : public NewOverrideBase
+{
+ pthread_mutex_t SMutex;
+ pthread_cond_t Condv;
+
+public:
+
+ // Constructor/destructor
+ WaitConditionImpl();
+ ~WaitConditionImpl();
+
+ // Release mutex and wait for condition. The mutex is re-aqured after the wait.
+ bool Wait(Mutex *pmutex, unsigned delay = OVR_WAIT_INFINITE);
+
+ // Notify a condition, releasing at one object waiting
+ void Notify();
+ // Notify a condition, releasing all objects waiting
+ void NotifyAll();
+};
+
+
+WaitConditionImpl::WaitConditionImpl()
+{
+ pthread_mutex_init(&SMutex, 0);
+ pthread_cond_init(&Condv, 0);
+}
+
+WaitConditionImpl::~WaitConditionImpl()
+{
+ pthread_mutex_destroy(&SMutex);
+ pthread_cond_destroy(&Condv);
+}
+
+bool WaitConditionImpl::Wait(Mutex *pmutex, unsigned delay)
+{
+ bool result = 1;
+ unsigned lockCount = pmutex->pImpl->LockCount;
+
+ // Mutex must have been locked
+ if (lockCount == 0)
+ return 0;
+
+ pthread_mutex_lock(&SMutex);
+
+ // Finally, release a mutex or semaphore
+ if (pmutex->pImpl->Recursive)
+ {
+ // Release the recursive mutex N times
+ pmutex->pImpl->LockCount = 0;
+ for(unsigned i=0; i<lockCount; i++)
+ pthread_mutex_unlock(&pmutex->pImpl->SMutex);
+ }
+ else
+ {
+ pmutex->pImpl->LockCount = 0;
+ pthread_mutex_unlock(&pmutex->pImpl->SMutex);
+ }
+
+ // Note that there is a gap here between mutex.Unlock() and Wait().
+ // The other mutex protects this gap.
+
+ if (delay == OVR_WAIT_INFINITE)
+ pthread_cond_wait(&Condv,&SMutex);
+ else
+ {
+ timespec ts;
+
+ struct timeval tv;
+ gettimeofday(&tv, 0);
+
+ ts.tv_sec = tv.tv_sec + (delay / 1000);
+ ts.tv_nsec = (tv.tv_usec + (delay % 1000) * 1000) * 1000;
+
+ if (ts.tv_nsec > 999999999)
+ {
+ ts.tv_sec++;
+ ts.tv_nsec -= 1000000000;
+ }
+ int r = pthread_cond_timedwait(&Condv,&SMutex, &ts);
+ OVR_ASSERT(r == 0 || r == ETIMEDOUT);
+ if (r)
+ result = 0;
+ }
+
+ pthread_mutex_unlock(&SMutex);
+
+ // Re-aquire the mutex
+ for(unsigned i=0; i<lockCount; i++)
+ pmutex->DoLock();
+
+ // Return the result
+ return result;
+}
+
+// Notify a condition, releasing the least object in a queue
+void WaitConditionImpl::Notify()
+{
+ pthread_mutex_lock(&SMutex);
+ pthread_cond_signal(&Condv);
+ pthread_mutex_unlock(&SMutex);
+}
+
+// Notify a condition, releasing all objects waiting
+void WaitConditionImpl::NotifyAll()
+{
+ pthread_mutex_lock(&SMutex);
+ pthread_cond_broadcast(&Condv);
+ pthread_mutex_unlock(&SMutex);
+}
+
+
+
+// *** Actual implementation of WaitCondition
+
+WaitCondition::WaitCondition()
+{
+ pImpl = new WaitConditionImpl;
+}
+WaitCondition::~WaitCondition()
+{
+ delete pImpl;
+}
+
+bool WaitCondition::Wait(Mutex *pmutex, unsigned delay)
+{
+ return pImpl->Wait(pmutex, delay);
+}
+// Notification
+void WaitCondition::Notify()
+{
+ pImpl->Notify();
+}
+void WaitCondition::NotifyAll()
+{
+ pImpl->NotifyAll();
+}
+
+
+// ***** Current thread
+
+// Per-thread variable
+/*
+static __thread Thread* pCurrentThread = 0;
+
+// Static function to return a pointer to the current thread
+void Thread::InitCurrentThread(Thread *pthread)
+{
+ pCurrentThread = pthread;
+}
+
+// Static function to return a pointer to the current thread
+Thread* Thread::GetThread()
+{
+ return pCurrentThread;
+}
+*/
+
+
+// *** Thread constructors.
+
+Thread::Thread(UPInt stackSize, int processor)
+{
+ // NOTE: RefCount mode already thread-safe for all Waitable objects.
+ CreateParams params;
+ params.stackSize = stackSize;
+ params.processor = processor;
+ Init(params);
+}
+
+Thread::Thread(Thread::ThreadFn threadFunction, void* userHandle, UPInt stackSize,
+ int processor, Thread::ThreadState initialState)
+{
+ CreateParams params(threadFunction, userHandle, stackSize, processor, initialState);
+ Init(params);
+}
+
+Thread::Thread(const CreateParams& params)
+{
+ Init(params);
+}
+
+void Thread::Init(const CreateParams& params)
+{
+ // Clear the variables
+ ThreadFlags = 0;
+ ThreadHandle = 0;
+ ExitCode = 0;
+ SuspendCount = 0;
+ StackSize = params.stackSize;
+ Processor = params.processor;
+ Priority = params.priority;
+
+ // Clear Function pointers
+ ThreadFunction = params.threadFunction;
+ UserHandle = params.userHandle;
+ if (params.initialState != NotRunning)
+ Start(params.initialState);
+}
+
+Thread::~Thread()
+{
+ // Thread should not running while object is being destroyed,
+ // this would indicate ref-counting issue.
+ //OVR_ASSERT(IsRunning() == 0);
+
+ // Clean up thread.
+ ThreadHandle = 0;
+}
+
+
+
+// *** Overridable User functions.
+
+// Default Run implementation
+int Thread::Run()
+{
+ // Call pointer to function, if available.
+ return (ThreadFunction) ? ThreadFunction(this, UserHandle) : 0;
+}
+void Thread::OnExit()
+{
+}
+
+
+// Finishes the thread and releases internal reference to it.
+void Thread::FinishAndRelease()
+{
+ // Note: thread must be US.
+ ThreadFlags &= (UInt32)~(OVR_THREAD_STARTED);
+ ThreadFlags |= OVR_THREAD_FINISHED;
+
+ // Release our reference; this is equivalent to 'delete this'
+ // from the point of view of our thread.
+ Release();
+}
+
+
+
+// *** ThreadList - used to track all created threads
+
+class ThreadList : public NewOverrideBase
+{
+ //------------------------------------------------------------------------
+ struct ThreadHashOp
+ {
+ size_t operator()(const Thread* ptr)
+ {
+ return (((size_t)ptr) >> 6) ^ (size_t)ptr;
+ }
+ };
+
+ HashSet<Thread*, ThreadHashOp> ThreadSet;
+ Mutex ThreadMutex;
+ WaitCondition ThreadsEmpty;
+ // Track the root thread that created us.
+ pthread_t RootThreadId;
+
+ static ThreadList* volatile pRunningThreads;
+
+ void addThread(Thread *pthread)
+ {
+ Mutex::Locker lock(&ThreadMutex);
+ ThreadSet.Add(pthread);
+ }
+
+ void removeThread(Thread *pthread)
+ {
+ Mutex::Locker lock(&ThreadMutex);
+ ThreadSet.Remove(pthread);
+ if (ThreadSet.GetSize() == 0)
+ ThreadsEmpty.Notify();
+ }
+
+ void finishAllThreads()
+ {
+ // Only original root thread can call this.
+ OVR_ASSERT(pthread_self() == RootThreadId);
+
+ Mutex::Locker lock(&ThreadMutex);
+ while (ThreadSet.GetSize() != 0)
+ ThreadsEmpty.Wait(&ThreadMutex);
+ }
+
+public:
+
+ ThreadList()
+ {
+ RootThreadId = pthread_self();
+ }
+ ~ThreadList() { }
+
+
+ static void AddRunningThread(Thread *pthread)
+ {
+ // Non-atomic creation ok since only the root thread
+ if (!pRunningThreads)
+ {
+ pRunningThreads = new ThreadList;
+ OVR_ASSERT(pRunningThreads);
+ }
+ pRunningThreads->addThread(pthread);
+ }
+
+ // NOTE: 'pthread' might be a dead pointer when this is
+ // called so it should not be accessed; it is only used
+ // for removal.
+ static void RemoveRunningThread(Thread *pthread)
+ {
+ OVR_ASSERT(pRunningThreads);
+ pRunningThreads->removeThread(pthread);
+ }
+
+ static void FinishAllThreads()
+ {
+ // This is ok because only root thread can wait for other thread finish.
+ if (pRunningThreads)
+ {
+ pRunningThreads->finishAllThreads();
+ delete pRunningThreads;
+ pRunningThreads = 0;
+ }
+ }
+};
+
+// By default, we have no thread list.
+ThreadList* volatile ThreadList::pRunningThreads = 0;
+
+
+// FinishAllThreads - exposed publicly in Thread.
+void Thread::FinishAllThreads()
+{
+ ThreadList::FinishAllThreads();
+}
+
+// *** Run override
+
+int Thread::PRun()
+{
+ // Suspend us on start, if requested
+ if (ThreadFlags & OVR_THREAD_START_SUSPENDED)
+ {
+ Suspend();
+ ThreadFlags &= (UInt32)~OVR_THREAD_START_SUSPENDED;
+ }
+
+ // Call the virtual run function
+ ExitCode = Run();
+ return ExitCode;
+}
+
+
+
+
+// *** User overridables
+
+bool Thread::GetExitFlag() const
+{
+ return (ThreadFlags & OVR_THREAD_EXIT) != 0;
+}
+
+void Thread::SetExitFlag(bool exitFlag)
+{
+ // The below is atomic since ThreadFlags is AtomicInt.
+ if (exitFlag)
+ ThreadFlags |= OVR_THREAD_EXIT;
+ else
+ ThreadFlags &= (UInt32) ~OVR_THREAD_EXIT;
+}
+
+
+// Determines whether the thread was running and is now finished
+bool Thread::IsFinished() const
+{
+ return (ThreadFlags & OVR_THREAD_FINISHED) != 0;
+}
+// Determines whether the thread is suspended
+bool Thread::IsSuspended() const
+{
+ return SuspendCount > 0;
+}
+// Returns current thread state
+Thread::ThreadState Thread::GetThreadState() const
+{
+ if (IsSuspended())
+ return Suspended;
+ if (ThreadFlags & OVR_THREAD_STARTED)
+ return Running;
+ return NotRunning;
+}
+
+// Join thread
+bool Thread::Join(int maxWaitMs) const
+{
+ // If polling,
+ if (maxWaitMs == 0)
+ {
+ // Just return if finished
+ return IsFinished();
+ }
+ // If waiting forever,
+ else if (maxWaitMs > 0)
+ {
+ UInt32 t0 = Timer::GetTicksMs();
+
+ while (!IsFinished())
+ {
+ UInt32 t1 = Timer::GetTicksMs();
+
+ // If the wait has expired,
+ int delta = (int)(t1 - t0);
+ if (delta >= maxWaitMs)
+ {
+ return false;
+ }
+
+ Thread::MSleep(10);
+ }
+
+ return true;
+ }
+ else
+ {
+ while (!IsFinished())
+ {
+ pthread_join(ThreadHandle, NULL);
+ }
+ }
+
+ return true;
+}
+
+/*
+static const char* mapsched_policy(int policy)
+{
+ switch(policy)
+ {
+ case SCHED_OTHER:
+ return "SCHED_OTHER";
+ case SCHED_RR:
+ return "SCHED_RR";
+ case SCHED_FIFO:
+ return "SCHED_FIFO";
+
+ }
+ return "UNKNOWN";
+}
+ int policy;
+ sched_param sparam;
+ pthread_getschedparam(pthread_self(), &policy, &sparam);
+ int max_prior = sched_get_priority_max(policy);
+ int min_prior = sched_get_priority_min(policy);
+ printf(" !!!! policy: %s, priority: %d, max priority: %d, min priority: %d\n", mapsched_policy(policy), sparam.sched_priority, max_prior, min_prior);
+#include <stdio.h>
+*/
+// ***** Thread management
+
+// The actual first function called on thread start
+void* Thread_PthreadStartFn(void* phandle)
+{
+ Thread* pthread = (Thread*)phandle;
+ int result = pthread->PRun();
+ // Signal the thread as done and release it atomically.
+ pthread->FinishAndRelease();
+ // At this point Thread object might be dead; however we can still pass
+ // it to RemoveRunningThread since it is only used as a key there.
+ ThreadList::RemoveRunningThread(pthread);
+ return reinterpret_cast<void*>(result);
+}
+
+int Thread::InitAttr = 0;
+pthread_attr_t Thread::Attr;
+
+/* static */
+int Thread::GetOSPriority(ThreadPriority p)
+//static inline int MapToSystemPrority(Thread::ThreadPriority p)
+{
+ OVR_UNUSED(p);
+ return -1;
+}
+
+bool Thread::Start(ThreadState initialState)
+{
+ if (initialState == NotRunning)
+ return 0;
+ if (GetThreadState() != NotRunning)
+ {
+ OVR_DEBUG_LOG(("Thread::Start failed - thread %p already running", this));
+ return 0;
+ }
+
+ if (!InitAttr)
+ {
+ pthread_attr_init(&Attr);
+ pthread_attr_setdetachstate(&Attr, PTHREAD_CREATE_DETACHED);
+ pthread_attr_setstacksize(&Attr, 128 * 1024);
+ sched_param sparam;
+ sparam.sched_priority = Thread::GetOSPriority(NormalPriority);
+ pthread_attr_setschedparam(&Attr, &sparam);
+ InitAttr = 1;
+ }
+
+ ExitCode = 0;
+ SuspendCount = 0;
+ ThreadFlags = (initialState == Running) ? 0 : OVR_THREAD_START_SUSPENDED;
+
+ // AddRef to us until the thread is finished
+ AddRef();
+ ThreadList::AddRunningThread(this);
+
+ int result;
+ if (StackSize != 128 * 1024 || Priority != NormalPriority)
+ {
+ pthread_attr_t attr;
+
+ pthread_attr_init(&attr);
+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+ pthread_attr_setstacksize(&attr, StackSize);
+ sched_param sparam;
+ sparam.sched_priority = Thread::GetOSPriority(Priority);
+ pthread_attr_setschedparam(&attr, &sparam);
+ result = pthread_create(&ThreadHandle, &attr, Thread_PthreadStartFn, this);
+ pthread_attr_destroy(&attr);
+ }
+ else
+ result = pthread_create(&ThreadHandle, &Attr, Thread_PthreadStartFn, this);
+
+ if (result)
+ {
+ ThreadFlags = 0;
+ Release();
+ ThreadList::RemoveRunningThread(this);
+ return 0;
+ }
+ return 1;
+}
+
+
+// Suspend the thread until resumed
+bool Thread::Suspend()
+{
+ OVR_DEBUG_LOG(("Thread::Suspend - cannot suspend threads on this system"));
+ return 0;
+}
+
+// Resumes currently suspended thread
+bool Thread::Resume()
+{
+ return 0;
+}
+
+
+// Quits with an exit code
+void Thread::Exit(int exitCode)
+{
+ // Can only exist the current thread
+ // if (GetThread() != this)
+ // return;
+
+ // Call the virtual OnExit function
+ OnExit();
+
+ // Signal this thread object as done and release it's references.
+ FinishAndRelease();
+ ThreadList::RemoveRunningThread(this);
+
+ pthread_exit(reinterpret_cast<void*>(exitCode));
+}
+
+ThreadId GetCurrentThreadId()
+{
+ return (void*)pthread_self();
+}
+
+// *** Sleep functions
+
+/* static */
+bool Thread::Sleep(unsigned secs)
+{
+ sleep(secs);
+ return 1;
+}
+/* static */
+bool Thread::MSleep(unsigned msecs)
+{
+ usleep(msecs*1000);
+ return 1;
+}
+
+/* static */
+int Thread::GetCPUCount()
+{
+ return 1;
+}
+
+
+#if defined (OVR_OS_MAC)
+void Thread::SetThreadName( const char* name )
+{
+ pthread_setname_np( name );
+}
+#else
+void Thread::SetThreadName( const char* name )
+{
+ pthread_setname_np( pthread_self(), name );
+}
+#endif
+
+}
+
+#endif // OVR_ENABLE_THREADS
diff --git a/LibOVR/Src/Net/OVR_Unix_Socket.cpp b/LibOVR/Src/Net/OVR_Unix_Socket.cpp
new file mode 100644
index 0000000..4320a7c
--- /dev/null
+++ b/LibOVR/Src/Net/OVR_Unix_Socket.cpp
@@ -0,0 +1,586 @@
+/************************************************************************************
+
+Filename : OVR_Unix_Socket.cpp
+Content : Berkley sockets networking implementation
+Created : July 1, 2014
+Authors : Kevin Jenkins
+
+Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved.
+
+Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License");
+you may not use the Oculus VR Rift SDK except in compliance with the License,
+which is provided at the time of installation or download, or which
+otherwise accompanies this software in either electronic or hard copy form.
+
+You may obtain a copy of the License at
+
+http://www.oculusvr.com/licenses/LICENSE-3.1
+
+Unless required by applicable law or agreed to in writing, the Oculus VR SDK
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+************************************************************************************/
+
+#include "OVR_Unix_Socket.h"
+#include "../Kernel/OVR_Std.h"
+#include "../Kernel/OVR_Allocator.h"
+#include "../Kernel/OVR_Threads.h" // Thread::MSleep
+#include "../Kernel/OVR_Log.h"
+
+#include <errno.h>
+
+namespace OVR { namespace Net {
+
+//-----------------------------------------------------------------------------
+// BerkleySocket
+
+void BerkleySocket::Close()
+{
+ if (TheSocket != INVALID_SOCKET)
+ {
+ close(TheSocket);
+ TheSocket = INVALID_SOCKET;
+ }
+}
+
+SInt32 BerkleySocket::GetSockname(SockAddr *pSockAddrOut)
+{
+ struct sockaddr_in6 sa;
+ memset(&sa,0,sizeof(sa));
+ socklen_t size = sizeof(sa);
+ SInt32 i = getsockname(TheSocket, (sockaddr*)&sa, &size);
+ if (i>=0)
+ {
+ pSockAddrOut->Set(&sa);
+ }
+ return i;
+}
+
+
+//-----------------------------------------------------------------------------
+// BitStream overloads for SockAddr
+
+BitStream& operator<<(BitStream& out, SockAddr& in)
+{
+ out.WriteBits((const unsigned char*) &in.Addr6, sizeof(in.Addr6)*8, true);
+ return out;
+}
+
+BitStream& operator>>(BitStream& in, SockAddr& out)
+{
+ bool success = in.ReadBits((unsigned char*) &out.Addr6, sizeof(out.Addr6)*8, true);
+ OVR_ASSERT(success);
+ OVR_UNUSED(success);
+ return in;
+}
+
+
+//-----------------------------------------------------------------------------
+// SockAddr
+
+SockAddr::SockAddr()
+{
+}
+
+SockAddr::SockAddr(SockAddr* address)
+{
+ Set(&address->Addr6);
+}
+
+SockAddr::SockAddr(sockaddr_storage* storage)
+{
+ Set(storage);
+}
+
+SockAddr::SockAddr(sockaddr_in6* address)
+{
+ Set(address);
+}
+
+SockAddr::SockAddr(const char* hostAddress, UInt16 port, int sockType)
+{
+ Set(hostAddress, port, sockType);
+}
+
+void SockAddr::Set(const sockaddr_storage* storage)
+{
+ memcpy(&Addr6, storage, sizeof(Addr6));
+}
+
+void SockAddr::Set(const sockaddr_in6* address)
+{
+ memcpy(&Addr6, address, sizeof(Addr6));
+}
+
+void SockAddr::Set(const char* hostAddress, UInt16 port, int sockType)
+{
+ memset(&Addr6, 0, sizeof(Addr6));
+
+ struct addrinfo* servinfo = 0; // will point to the results
+ struct addrinfo hints;
+
+ // make sure the struct is empty
+ memset(&hints, 0, sizeof (addrinfo));
+
+ hints.ai_socktype = sockType; // SOCK_DGRAM or SOCK_STREAM
+ hints.ai_flags = AI_PASSIVE; // fill in my IP for me
+ hints.ai_family = AF_UNSPEC ;
+
+ if (SOCK_DGRAM == sockType)
+ {
+ hints.ai_protocol = IPPROTO_UDP;
+ }
+ else if (SOCK_STREAM == sockType)
+ {
+ hints.ai_protocol = IPPROTO_TCP;
+ }
+
+ char portStr[32];
+ OVR_itoa(port, portStr, sizeof(portStr), 10);
+ int errcode = getaddrinfo(hostAddress, portStr, &hints, &servinfo);
+
+ if (0 != errcode)
+ {
+ OVR::LogError("getaddrinfo error: %s", gai_strerror(errcode));
+ }
+
+ OVR_ASSERT(0 != servinfo);
+
+ memcpy(&Addr6, servinfo->ai_addr, sizeof(Addr6));
+
+ freeaddrinfo(servinfo);
+}
+
+UInt16 SockAddr::GetPort()
+{
+ return htons(Addr6.sin6_port);
+}
+
+String SockAddr::ToString(bool writePort, char portDelineator) const
+{
+ char dest[INET6_ADDRSTRLEN + 1];
+
+ int ret = getnameinfo((struct sockaddr*)&Addr6,
+ sizeof(struct sockaddr_in6),
+ dest,
+ INET6_ADDRSTRLEN,
+ NULL,
+ 0,
+ NI_NUMERICHOST);
+ if (ret != 0)
+ {
+ dest[0] = '\0';
+ }
+
+ if (writePort)
+ {
+ unsigned char ch[2];
+ ch[0]=portDelineator;
+ ch[1]=0;
+ OVR_strcat(dest, 16, (const char*) ch);
+ OVR_itoa(ntohs(Addr6.sin6_port), dest+strlen(dest), 16, 10);
+ }
+
+ return String(dest);
+}
+bool SockAddr::IsLocalhost() const
+{
+ static const unsigned char localhost_bytes[] =
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
+
+ return memcmp(Addr6.sin6_addr.s6_addr, localhost_bytes, 16) == 0;
+}
+bool SockAddr::operator==( const SockAddr& right ) const
+{
+ return memcmp(&Addr6, &right.Addr6, sizeof(Addr6)) == 0;
+}
+
+bool SockAddr::operator!=( const SockAddr& right ) const
+{
+ return !(*this == right);
+}
+
+bool SockAddr::operator>( const SockAddr& right ) const
+{
+ return memcmp(&Addr6, &right.Addr6, sizeof(Addr6)) > 0;
+}
+
+bool SockAddr::operator<( const SockAddr& right ) const
+{
+ return memcmp(&Addr6, &right.Addr6, sizeof(Addr6)) < 0;
+}
+
+
+// Returns true on success
+static bool SetSocketOptions(SocketHandle sock)
+{
+ bool failed = false;
+ int sock_opt;
+ int sockError = 0;
+
+ // This doubles the max throughput rate
+ sock_opt=1024*256;
+ sockError = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, ( char * ) & sock_opt, sizeof ( sock_opt ) );
+ if (sockError != 0)
+ {
+ int errsv = errno;
+ OVR::LogError("[Socket] Failed SO_RCVBUF setsockopt, errno: %d", errsv);
+ failed = true;
+ }
+
+ // This doesn't make much difference: 10% maybe
+ // Not supported on console 2
+ sock_opt=1024*16;
+ sockError = setsockopt(sock, SOL_SOCKET, SO_SNDBUF, ( char * ) & sock_opt, sizeof ( sock_opt ) );
+ if (sockError != 0)
+ {
+ int errsv = errno;
+ OVR::LogError("[Socket] Failed SO_SNDBUF setsockopt, errno: %d", errsv);
+ failed = true;
+ }
+
+ int value = 1;
+ sockError = setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof(value));
+ if (sockError != 0)
+ {
+ int errsv = errno;
+ OVR::LogError("[Socket] Failed SO_NOSIGPIPE setsockopt, errno: %d", errsv);
+ failed = true;
+ }
+
+ // Reuse address is only needed for posix platforms, as it is the default
+ // on Windows platforms.
+ int optval = 1;
+ sockError = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int));
+ if (sockError != 0)
+ {
+ int errsv = errno;
+ OVR::LogError("[Socket] Failed SO_REUSEADDR setsockopt, errno: %d", errsv);
+ failed = true;
+ }
+
+ return !failed;
+}
+
+void _Ioctlsocket(SocketHandle sock, unsigned long nonblocking)
+{
+ int flags = fcntl(sock, F_GETFL, 0);
+ if (flags < 0) return; // return false
+ if (nonblocking == 0) { flags &= ~O_NONBLOCK; }
+ else { flags |= O_NONBLOCK; }
+ fcntl(sock, F_SETFL, flags);
+}
+
+static SocketHandle BindShared(int ai_family, int ai_socktype, BerkleyBindParameters *pBindParameters)
+{
+ SocketHandle sock;
+
+ struct addrinfo hints;
+ memset(&hints, 0, sizeof (addrinfo)); // make sure the struct is empty
+ hints.ai_family = ai_family;
+ hints.ai_socktype = ai_socktype;
+ hints.ai_flags = AI_PASSIVE; // fill in my IP for me
+ struct addrinfo *servinfo=0, *aip; // will point to the results
+ char portStr[32];
+ OVR_itoa(pBindParameters->Port, portStr, sizeof(portStr), 10);
+
+ int errcode = 0;
+ if (!pBindParameters->Address.IsEmpty())
+ errcode = getaddrinfo(pBindParameters->Address.ToCStr(), portStr, &hints, &servinfo);
+ else
+ errcode = getaddrinfo(0, portStr, &hints, &servinfo);
+
+ if (0 != errcode)
+ {
+ OVR::LogError("getaddrinfo error: %s", gai_strerror(errcode));
+ }
+
+ for (aip = servinfo; aip != NULL; aip = aip->ai_next)
+ {
+ // Open socket. The address type depends on what
+ // getaddrinfo() gave us.
+ sock = socket(aip->ai_family, aip->ai_socktype, aip->ai_protocol);
+ if (sock != 0)
+ {
+ SetSocketOptions(sock);
+ int ret = bind( sock, aip->ai_addr, (int) aip->ai_addrlen );
+ if (ret>=0)
+ {
+ // The actual socket is always non-blocking
+ // I control blocking or not using WSAEventSelect
+ _Ioctlsocket(sock, 1);
+ freeaddrinfo(servinfo);
+ return sock;
+ }
+ else
+ {
+ close(sock);
+ }
+ }
+ }
+
+ if (servinfo) { freeaddrinfo(servinfo); }
+ return INVALID_SOCKET;
+}
+
+
+//-----------------------------------------------------------------------------
+// UDPSocket
+
+UDPSocket::UDPSocket()
+{
+ RecvBuf = new UByte[RecvBufSize];
+}
+
+UDPSocket::~UDPSocket()
+{
+ delete[] RecvBuf;
+}
+
+SocketHandle UDPSocket::Bind(BerkleyBindParameters *pBindParameters)
+{
+ SocketHandle s = BindShared(AF_INET6, SOCK_DGRAM, pBindParameters);
+ if (s < 0)
+ return s;
+
+ Close();
+ TheSocket = s;
+
+ return TheSocket;
+}
+
+void UDPSocket::OnRecv(SocketEvent_UDP* eventHandler, UByte* pData, int bytesRead, SockAddr* address)
+{
+ eventHandler->UDP_OnRecv(this, pData, bytesRead, address);
+}
+
+int UDPSocket::Send(const void* pData, int bytes, SockAddr* address)
+{
+ return (int)sendto(TheSocket, (const char*)pData, bytes, 0, (const sockaddr*)&address->Addr6, sizeof(address->Addr6));
+}
+
+void UDPSocket::Poll(SocketEvent_UDP *eventHandler)
+{
+ struct sockaddr_storage win32_addr;
+ socklen_t fromlen;
+ int bytesRead;
+
+ // FIXME: Implement blocking poll wait for UDP
+
+ // While some bytes are read,
+ while (fromlen = sizeof(win32_addr), // Must set fromlen each time
+ bytesRead = (int)recvfrom(TheSocket, (char*)RecvBuf, RecvBufSize, 0, (sockaddr*)&win32_addr, &fromlen),
+ bytesRead > 0)
+ {
+ SockAddr address(&win32_addr); // Wrap address
+
+ OnRecv(eventHandler, RecvBuf, bytesRead, &address);
+ }
+}
+
+
+//-----------------------------------------------------------------------------
+// TCPSocket
+
+TCPSocket::TCPSocket()
+{
+ IsConnecting = false;
+ IsListenSocket = false;
+}
+TCPSocket::TCPSocket(SocketHandle boundHandle, bool isListenSocket)
+{
+ TheSocket = boundHandle;
+ IsListenSocket = isListenSocket;
+ IsConnecting = false;
+ SetSocketOptions(TheSocket);
+
+ // The actual socket is always non-blocking
+ _Ioctlsocket(TheSocket, 1);
+}
+
+TCPSocket::~TCPSocket()
+{
+}
+
+void TCPSocket::OnRecv(SocketEvent_TCP* eventHandler, UByte* pData, int bytesRead)
+{
+ eventHandler->TCP_OnRecv(this, pData, bytesRead);
+}
+
+SocketHandle TCPSocket::Bind(BerkleyBindParameters* pBindParameters)
+{
+ SocketHandle s = BindShared(AF_INET6, SOCK_STREAM, pBindParameters);
+ if (s < 0)
+ return s;
+
+ Close();
+
+ SetBlockingTimeout(pBindParameters->blockingTimeout);
+ TheSocket = s;
+
+ return TheSocket;
+}
+
+int TCPSocket::Listen()
+{
+ if (IsListenSocket)
+ {
+ return 0;
+ }
+
+ int i = listen(TheSocket, SOMAXCONN);
+ if (i >= 0)
+ {
+ IsListenSocket = true;
+ }
+
+ return i;
+}
+
+int TCPSocket::Connect(SockAddr* address)
+{
+ int retval;
+
+ retval = connect(TheSocket, (struct sockaddr *) &address->Addr6, sizeof(address->Addr6));
+ if (retval < 0)
+ {
+ int errsv = errno;
+ // EINPROGRESS should not be checked on windows but should
+ // be checked on POSIX platforms.
+ if (errsv == EWOULDBLOCK || errsv == EINPROGRESS)
+ {
+ IsConnecting = true;
+ return 0;
+ }
+
+ OVR::LogText( "TCPSocket::Connect failed:Error code - %d\n", errsv );
+ }
+
+ return retval;
+}
+
+int TCPSocket::Send(const void* pData, int bytes)
+{
+ if (bytes <= 0)
+ {
+ return 0;
+ }
+ else
+ {
+ return (int)send(TheSocket, (const char*)pData, bytes, 0);
+ }
+}
+
+
+//// TCPSocketPollState
+
+TCPSocketPollState::TCPSocketPollState()
+{
+ FD_ZERO(&readFD);
+ FD_ZERO(&exceptionFD);
+ FD_ZERO(&writeFD);
+ largestDescriptor = INVALID_SOCKET;
+}
+
+bool TCPSocketPollState::IsValid() const
+{
+ return largestDescriptor != INVALID_SOCKET;
+}
+
+void TCPSocketPollState::Add(TCPSocket* tcpSocket)
+{
+ if (!tcpSocket)
+ {
+ return;
+ }
+
+ SocketHandle handle = tcpSocket->GetSocketHandle();
+
+ if (handle == INVALID_SOCKET)
+ {
+ return;
+ }
+
+ if (largestDescriptor == INVALID_SOCKET ||
+ largestDescriptor < handle)
+ {
+ largestDescriptor = handle;
+ }
+
+ FD_SET(handle, &readFD);
+ FD_SET(handle, &exceptionFD);
+
+ if (tcpSocket->IsConnecting)
+ {
+ FD_SET(handle, &writeFD);
+ }
+}
+
+bool TCPSocketPollState::Poll(long usec, long seconds)
+{
+ timeval tv;
+ tv.tv_sec = seconds;
+ tv.tv_usec = (int)usec;
+
+ return select(largestDescriptor + 1, &readFD, &writeFD, &exceptionFD, &tv) > 0;
+}
+
+void TCPSocketPollState::HandleEvent(TCPSocket* tcpSocket, SocketEvent_TCP* eventHandler)
+{
+ if (!tcpSocket || !eventHandler)
+ {
+ return;
+ }
+
+ SocketHandle handle = tcpSocket->GetSocketHandle();
+
+ if (tcpSocket->IsConnecting && FD_ISSET(handle, &writeFD))
+ {
+ tcpSocket->IsConnecting = false;
+ eventHandler->TCP_OnConnected(tcpSocket);
+ }
+
+ if (FD_ISSET(handle, &readFD))
+ {
+ if (!tcpSocket->IsListenSocket)
+ {
+ static const int BUFF_SIZE = 8096;
+ char data[BUFF_SIZE];
+
+ int bytesRead = (int)recv(handle, data, BUFF_SIZE, 0);
+ if (bytesRead > 0)
+ {
+ tcpSocket->OnRecv(eventHandler, (UByte*)data, bytesRead);
+ }
+ else // Disconnection event:
+ {
+ tcpSocket->IsConnecting = false;
+ eventHandler->TCP_OnClosed(tcpSocket);
+ }
+ }
+ else
+ {
+ struct sockaddr_storage sockAddr;
+ socklen_t sockAddrSize = sizeof(sockAddr);
+
+ SocketHandle newSock = accept(handle, (sockaddr*)&sockAddr, (socklen_t*)&sockAddrSize);
+ if (newSock > 0)
+ {
+ SockAddr sa(&sockAddr);
+ eventHandler->TCP_OnAccept(tcpSocket, &sa, newSock);
+ }
+ }
+ }
+
+ if (FD_ISSET(handle, &exceptionFD))
+ {
+ tcpSocket->IsConnecting = false;
+ eventHandler->TCP_OnClosed(tcpSocket);
+ }
+}
+
+
+}} // namespace OVR::Net
diff --git a/LibOVR/Src/Net/OVR_Unix_Socket.h b/LibOVR/Src/Net/OVR_Unix_Socket.h
new file mode 100644
index 0000000..faec464
--- /dev/null
+++ b/LibOVR/Src/Net/OVR_Unix_Socket.h
@@ -0,0 +1,152 @@
+/************************************************************************************
+
+PublicHeader: n/a
+Filename : OVR_Unix_Socket.h
+Content : Berkley sockets networking implementation
+Created : July 1, 2014
+Authors : Kevin Jenkins
+
+Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved.
+
+Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License");
+you may not use the Oculus VR Rift SDK except in compliance with the License,
+which is provided at the time of installation or download, or which
+otherwise accompanies this software in either electronic or hard copy form.
+
+You may obtain a copy of the License at
+
+http://www.oculusvr.com/licenses/LICENSE-3.1
+
+Unless required by applicable law or agreed to in writing, the Oculus VR SDK
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+************************************************************************************/
+
+#ifndef OVR_Unix_Socket_h
+#define OVR_Unix_Socket_h
+
+#include "OVR_Socket.h"
+#include "OVR_BitStream.h"
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <fcntl.h>
+
+namespace OVR { namespace Net {
+
+//-----------------------------------------------------------------------------
+// SockAddr
+
+// Abstraction for IPV6 socket address, with various convenience functions
+class SockAddr
+{
+public:
+ SockAddr();
+ SockAddr(SockAddr* sa);
+ SockAddr(sockaddr_storage* sa);
+ SockAddr(sockaddr_in6* sa);
+ SockAddr(const char* hostAddress, UInt16 port, int sockType);
+
+public:
+ void Set(const sockaddr_storage* sa);
+ void Set(const sockaddr_in6* sa);
+ void Set(const char* hostAddress, UInt16 port, int sockType); // SOCK_DGRAM or SOCK_STREAM
+
+ UInt16 GetPort();
+
+ String ToString(bool writePort, char portDelineator) const;
+
+ bool IsLocalhost() const;
+
+ void Serialize(BitStream* bs);
+ bool Deserialize(BitStream);
+
+ bool operator==( const SockAddr& right ) const;
+ bool operator!=( const SockAddr& right ) const;
+ bool operator >( const SockAddr& right ) const;
+ bool operator <( const SockAddr& right ) const;
+
+public:
+ sockaddr_in6 Addr6;
+};
+
+
+//-----------------------------------------------------------------------------
+// UDP Socket
+
+// Windows version of TCP socket
+class UDPSocket : public UDPSocketBase
+{
+public:
+ UDPSocket();
+ virtual ~UDPSocket();
+
+public:
+ virtual SocketHandle Bind(BerkleyBindParameters* pBindParameters);
+ virtual int Send(const void* pData, int bytes, SockAddr* address);
+ virtual void Poll(SocketEvent_UDP* eventHandler);
+
+protected:
+ static const int RecvBufSize = 1048576;
+ UByte* RecvBuf;
+
+ virtual void OnRecv(SocketEvent_UDP* eventHandler, UByte* pData,
+ int bytesRead, SockAddr* address);
+};
+
+
+//-----------------------------------------------------------------------------
+// TCP Socket
+
+// Windows version of TCP socket
+class TCPSocket : public TCPSocketBase
+{
+ friend class TCPSocketPollState;
+
+public:
+ TCPSocket();
+ TCPSocket(SocketHandle boundHandle, bool isListenSocket);
+ virtual ~TCPSocket();
+
+public:
+ virtual SocketHandle Bind(BerkleyBindParameters* pBindParameters);
+ virtual int Listen();
+ virtual int Connect(SockAddr* address);
+ virtual int Send(const void* pData, int bytes);
+
+protected:
+ virtual void OnRecv(SocketEvent_TCP* eventHandler, UByte* pData,
+ int bytesRead);
+
+public:
+ bool IsConnecting; // Is in the process of connecting?
+};
+
+
+//-----------------------------------------------------------------------------
+// TCPSocketPollState
+
+// Polls multiple blocking TCP sockets at once
+class TCPSocketPollState
+{
+ fd_set readFD, exceptionFD, writeFD;
+ SocketHandle largestDescriptor;
+
+public:
+ TCPSocketPollState();
+ bool IsValid() const;
+ void Add(TCPSocket* tcpSocket);
+ bool Poll(long usec = 30000, long seconds = 0);
+ void HandleEvent(TCPSocket* tcpSocket, SocketEvent_TCP* eventHandler);
+};
+
+
+}} // OVR::Net
+
+#endif
diff --git a/Samples/CommonSrc/Platform/OSX_Gamepad.cpp b/Samples/CommonSrc/Platform/OSX_Gamepad.cpp
new file mode 100644
index 0000000..69acca4
--- /dev/null
+++ b/Samples/CommonSrc/Platform/OSX_Gamepad.cpp
@@ -0,0 +1,430 @@
+/************************************************************************************
+
+Filename : OSX_Gamepad.cpp
+Content : OSX implementation of Gamepad functionality.
+Created : May 6, 2013
+Authors : Lee Cooper
+
+Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+************************************************************************************/
+
+#include "OSX_Gamepad.h"
+
+
+static const UInt32 Logitech_F710_VendorID = 0x046D;
+//static const UInt32 Logitech_F710_ProductID = 0xC219;
+
+static const UInt32 Sony_DualShock3_VendorID = 0x054C;
+//static const UInt32 Sony_DualShock3_ProductID = 0x0268;
+
+
+namespace OVR { namespace OvrPlatform { namespace OSX {
+
+
+GamepadManager::GamepadManager()
+ : bStateChanged(false)
+{
+
+ HidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
+ IOHIDManagerOpen(HidManager, kIOHIDOptionsTypeNone);
+ IOHIDManagerScheduleWithRunLoop(HidManager,
+ CFRunLoopGetCurrent(),
+ kCFRunLoopDefaultMode);
+
+
+ // Setup device matching.
+ CFStringRef keys[] = { CFSTR(kIOHIDDeviceUsagePageKey),
+ CFSTR(kIOHIDDeviceUsageKey)};
+
+ int value;
+ CFNumberRef values[2];
+ CFDictionaryRef dictionaries[2];
+
+ // Match joysticks.
+ value = kHIDPage_GenericDesktop;
+ values[0] = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &value);
+
+ value = kHIDUsage_GD_Joystick;
+ values[1] = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &value);
+
+ dictionaries[0] = CFDictionaryCreate(kCFAllocatorDefault,
+ (const void **) keys,
+ (const void **) values,
+ 2,
+ &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
+ CFRelease(values[0]);
+ CFRelease(values[1]);
+
+ // Match gamepads.
+ value = kHIDPage_GenericDesktop;
+ values[0] = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &value);
+
+ value = kHIDUsage_GD_GamePad;
+ values[1] = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &value);
+
+ dictionaries[1] = CFDictionaryCreate(kCFAllocatorDefault,
+ (const void **) keys,
+ (const void **) values,
+ 2,
+ &kCFTypeDictionaryKeyCallBacks,
+ &kCFTypeDictionaryValueCallBacks);
+ CFRelease(values[0]);
+ CFRelease(values[1]);
+
+ CFArrayRef array = CFArrayCreate( kCFAllocatorDefault,
+ (const void **) dictionaries,
+ 2,
+ &kCFTypeArrayCallBacks);
+ CFRelease(dictionaries[0]);
+ CFRelease(dictionaries[1]);
+
+ IOHIDManagerSetDeviceMatchingMultiple(HidManager, array);
+
+ CFRelease(array);
+
+
+ IOHIDManagerRegisterDeviceMatchingCallback(HidManager, staticOnDeviceMatched, this);
+ IOHIDManagerRegisterDeviceRemovalCallback(HidManager, staticOnDeviceRemoved, this);
+
+}
+
+GamepadManager::~GamepadManager()
+{
+ CFRelease(HidManager);
+}
+
+UInt32 GamepadManager::GetGamepadCount()
+{
+ return 1;
+}
+
+bool GamepadManager::GetGamepadState(UInt32 index, GamepadState* pState)
+{
+ // For now we just support one gamepad.
+ OVR_UNUSED(index);
+
+ if (!bStateChanged)
+ {
+ return false;
+ }
+
+ bStateChanged = false;
+// State.Debug();
+
+ *pState = State;
+ return true;
+}
+
+int GamepadManager::getIntDeviceProperty(IOHIDDeviceRef device, CFStringRef key)
+{
+ CFTypeRef type = IOHIDDeviceGetProperty(device, key);
+ OVR_ASSERT(type != NULL && CFGetTypeID(type) == CFNumberGetTypeID());
+
+ int value;
+ CFNumberGetValue((CFNumberRef) type, kCFNumberSInt32Type, &value);
+
+ return value;
+}
+
+void GamepadManager::staticOnDeviceMatched(void* context, IOReturn result, void* sender, IOHIDDeviceRef device)
+{
+ OVR_UNUSED(result);
+ OVR_UNUSED(sender);
+ GamepadManager* pManager = (GamepadManager*) context;
+ pManager->onDeviceMatched(device);
+}
+
+void GamepadManager::onDeviceMatched(IOHIDDeviceRef device)
+{
+ IOHIDDeviceRegisterInputValueCallback(device, staticOnDeviceValueChanged, this);
+}
+
+void GamepadManager::staticOnDeviceRemoved(void* context, IOReturn result, void* sender, IOHIDDeviceRef device)
+{
+ OVR_UNUSED(result);
+ OVR_UNUSED(sender);
+ GamepadManager* pManager = (GamepadManager*) context;
+ pManager->onDeviceRemoved(device);
+}
+
+void GamepadManager::onDeviceRemoved(IOHIDDeviceRef device)
+{
+ IOHIDDeviceRegisterInputValueCallback(device, NULL, NULL);
+}
+
+void GamepadManager::staticOnDeviceValueChanged(void* context, IOReturn result, void* sender, IOHIDValueRef value)
+{
+ OVR_UNUSED(result);
+ OVR_UNUSED(sender);
+ GamepadManager* pManager = (GamepadManager*) context;
+ pManager->onDeviceValueChanged(value);
+}
+
+float GamepadManager::mapAnalogAxis(IOHIDValueRef value, IOHIDElementRef element)
+{
+
+ CFIndex val = IOHIDValueGetIntegerValue(value);
+ CFIndex min = IOHIDElementGetLogicalMin(element);
+ CFIndex max = IOHIDElementGetLogicalMax(element);
+
+ float v = (float) (val - min) / (float) (max - min);
+ v = v * 2.0f - 1.0f;
+
+ // Dead zone.
+ if (v < 0.1f && v > -0.1f)
+ {
+ v = 0.0f;
+ }
+
+ return v;
+}
+
+bool GamepadManager::setStateIfDifferent(float& state, float newState)
+{
+ if (state == newState)
+ return false;
+
+ state = newState;
+
+ return true;
+}
+
+void GamepadManager::onDeviceValueChanged(IOHIDValueRef value)
+{
+
+ IOHIDElementRef element = IOHIDValueGetElement(value);
+ IOHIDDeviceRef device = IOHIDElementGetDevice(element);
+
+ unsigned int vendorID = (unsigned int)getIntDeviceProperty(device, CFSTR(kIOHIDVendorIDKey));
+ unsigned int productID = (unsigned int)getIntDeviceProperty(device, CFSTR(kIOHIDProductIDKey));
+ OVR_UNUSED(productID);
+
+ uint32_t usagePage = IOHIDElementGetUsagePage(element);
+ uint32_t usage = IOHIDElementGetUsage(element);
+
+ // The following controller mapping is based on the Logitech F710, however we use it for
+ // all Logitech devices on the assumption that they're likely to share the same mapping.
+ if (vendorID == Logitech_F710_VendorID)
+ {
+ // Logitech F710 mapping.
+ if (usagePage == kHIDPage_Button)
+ {
+ bool buttonState = IOHIDValueGetIntegerValue(value);
+
+ switch(usage)
+ {
+ case kHIDUsage_Button_1:
+ manipulateBitField(State.Buttons, Gamepad_X, buttonState);
+ break;
+ case kHIDUsage_Button_2:
+ manipulateBitField(State.Buttons, Gamepad_A, buttonState);
+ break;
+ case kHIDUsage_Button_3:
+ manipulateBitField(State.Buttons, Gamepad_B, buttonState);
+ break;
+ case kHIDUsage_Button_4:
+ manipulateBitField(State.Buttons, Gamepad_Y, buttonState);
+ break;
+ case 0x05:
+ manipulateBitField(State.Buttons, Gamepad_L1, buttonState);
+ break;
+ case 0x06:
+ manipulateBitField(State.Buttons, Gamepad_R1, buttonState);
+ break;
+ case 0x07:
+ State.LT = buttonState ? 1.0f:0.0f;
+ break;
+ case 0x08:
+ State.RT = buttonState ? 1.0f:0.0f;
+ break;
+ case 0x09:
+ manipulateBitField(State.Buttons, Gamepad_Back, buttonState);
+ break;
+ case 0x0A:
+ manipulateBitField(State.Buttons, Gamepad_Start, buttonState);
+ break;
+ case 0x0B:
+ manipulateBitField(State.Buttons, Gamepad_LStick, buttonState);
+ break;
+ case 0x0C:
+ manipulateBitField(State.Buttons, Gamepad_RStick, buttonState);
+ break;
+ default:
+ return;
+ }
+ }
+ else if (usagePage == kHIDPage_GenericDesktop)
+ {
+ float v;
+ switch(usage)
+ {
+ case kHIDUsage_GD_X:
+ v = mapAnalogAxis(value, element);
+ if (!setStateIfDifferent(State.LX, v))
+ return;
+ break;
+ case kHIDUsage_GD_Y:
+ v = mapAnalogAxis(value, element);
+ if (!setStateIfDifferent(State.LY, -v))
+ return;
+ break;
+ case kHIDUsage_GD_Z:
+ v = mapAnalogAxis(value, element);
+ if (!setStateIfDifferent(State.RX, v))
+ return;
+ break;
+ case kHIDUsage_GD_Rz:
+ v = mapAnalogAxis(value, element);
+ if (!setStateIfDifferent(State.RY, -v))
+ return;
+ break;
+ case kHIDUsage_GD_Hatswitch:
+ {
+ CFIndex integerValue = IOHIDValueGetIntegerValue(value);
+
+ manipulateBitField(State.Buttons,
+ Gamepad_Up,
+ integerValue == 7 || integerValue == 0 || integerValue == 1);
+ manipulateBitField(State.Buttons,
+ Gamepad_Down,
+ integerValue == 3 || integerValue == 4 || integerValue == 5);
+ manipulateBitField(State.Buttons,
+ Gamepad_Left,
+ integerValue == 5 || integerValue == 6 || integerValue == 7);
+ manipulateBitField(State.Buttons,
+ Gamepad_Right,
+ integerValue == 1 || integerValue == 2 || integerValue == 3);
+ }
+ break;
+ default:
+ return;
+ }
+ }
+ }
+ // The following controller mapping is based on the Sony DualShock3, however we use it for
+ // all Sony devices on the assumption that they're likely to share the same mapping.
+ else if (vendorID == Sony_DualShock3_VendorID)
+ {
+ // PS3 Controller.
+ if (usagePage == kHIDPage_Button)
+ {
+ bool buttonState = IOHIDValueGetIntegerValue(value);
+
+ switch(usage)
+ {
+ case kHIDUsage_Button_1:
+ manipulateBitField(State.Buttons, Gamepad_Back, buttonState);
+ break;
+ case kHIDUsage_Button_2:
+ manipulateBitField(State.Buttons, Gamepad_LStick, buttonState);
+ break;
+ case kHIDUsage_Button_3:
+ manipulateBitField(State.Buttons, Gamepad_RStick, buttonState);
+ break;
+ case kHIDUsage_Button_4:
+ manipulateBitField(State.Buttons, Gamepad_Start, buttonState);
+ break;
+ case 0x05:
+ manipulateBitField(State.Buttons, Gamepad_Up, buttonState);
+ break;
+ case 0x06:
+ manipulateBitField(State.Buttons, Gamepad_Right, buttonState);
+ break;
+ case 0x07:
+ manipulateBitField(State.Buttons, Gamepad_Down, buttonState);
+ break;
+ case 0x08:
+ manipulateBitField(State.Buttons, Gamepad_Left, buttonState);
+ break;
+ case 0x09:
+ State.LT = buttonState ? 1.0f:0.0f;
+ break;
+ case 0x0A:
+ State.RT = buttonState ? 1.0f:0.0f;
+ break;
+ case 0x0B:
+ manipulateBitField(State.Buttons, Gamepad_L1, buttonState);
+ break;
+ case 0x0C:
+ manipulateBitField(State.Buttons, Gamepad_R1, buttonState);
+ break;
+ case 0x0D:
+ // PS3 Triangle.
+ manipulateBitField(State.Buttons, Gamepad_TRIANGLE, buttonState);
+ break;
+ case 0x0E:
+ // PS3 Circle
+ manipulateBitField(State.Buttons, Gamepad_CIRCLE, buttonState);
+ break;
+ case 0x0F:
+ // PS3 Cross
+ manipulateBitField(State.Buttons, Gamepad_CROSS, buttonState);
+ break;
+ case 0x10:
+ // PS3 Square
+ manipulateBitField(State.Buttons, Gamepad_SQUARE, buttonState);
+ break;
+ default:
+ return;
+ }
+ }
+ else if (usagePage == kHIDPage_GenericDesktop)
+ {
+ float v;
+ switch(usage)
+ {
+ case kHIDUsage_GD_X:
+ v = mapAnalogAxis(value, element);
+ if (!setStateIfDifferent(State.LX, v))
+ return;
+ break;
+ case kHIDUsage_GD_Y:
+ v = mapAnalogAxis(value, element);
+ if (!setStateIfDifferent(State.LY, -v))
+ return;
+ break;
+ case kHIDUsage_GD_Z:
+ v = mapAnalogAxis(value, element);
+ if (!setStateIfDifferent(State.RX, v))
+ return;
+ break;
+ case kHIDUsage_GD_Rz:
+ v = mapAnalogAxis(value, element);
+ if (!setStateIfDifferent(State.RY, -v))
+ return;
+ break;
+ default:
+ return;
+ }
+ }
+ }
+
+ bStateChanged = true;
+}
+
+void GamepadManager::manipulateBitField(unsigned int& bitfield, unsigned int mask, bool val)
+{
+ if (val)
+ {
+ bitfield |= mask;
+ }
+ else
+ {
+ bitfield &= ~mask;
+ }
+}
+
+}}} // OVR::OvrPlatform::OSX
diff --git a/Samples/CommonSrc/Platform/OSX_Gamepad.h b/Samples/CommonSrc/Platform/OSX_Gamepad.h
new file mode 100644
index 0000000..5abeb56
--- /dev/null
+++ b/Samples/CommonSrc/Platform/OSX_Gamepad.h
@@ -0,0 +1,66 @@
+/************************************************************************************
+
+Filename : OSX_Gamepad.h
+Content : OSX implementation of Gamepad functionality.
+Created : May 6, 2013
+Authors : Lee Cooper
+
+Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+************************************************************************************/
+
+#ifndef OVR_OSX_Gamepad_h
+#define OVR_OSX_Gamepad_h
+
+#include <IOKit/IOKitLib.h>
+#include <IOKit/hid/IOHIDManager.h>
+
+#include "Gamepad.h"
+
+namespace OVR { namespace OvrPlatform { namespace OSX {
+
+
+class GamepadManager : public OvrPlatform::GamepadManager
+{
+public:
+ GamepadManager();
+ ~GamepadManager();
+
+ virtual UInt32 GetGamepadCount();
+ virtual bool GetGamepadState(UInt32 index, GamepadState* pState);
+
+private:
+ static void staticOnDeviceMatched(void* context, IOReturn result, void* sender, IOHIDDeviceRef device);
+ void onDeviceMatched(IOHIDDeviceRef device);
+
+ static void staticOnDeviceRemoved(void* context, IOReturn result, void* sender, IOHIDDeviceRef device);
+ void onDeviceRemoved(IOHIDDeviceRef device);
+
+ static void staticOnDeviceValueChanged(void* context, IOReturn result, void* sender, IOHIDValueRef value);
+ void onDeviceValueChanged(IOHIDValueRef value);
+
+ int getIntDeviceProperty(IOHIDDeviceRef device, CFStringRef key);
+ float mapAnalogAxis(IOHIDValueRef value, IOHIDElementRef element);
+ void manipulateBitField(unsigned int& bitfield, unsigned int mask, bool val);
+ bool setStateIfDifferent(float& state, float newState);
+
+ IOHIDManagerRef HidManager;
+ GamepadState State;
+ bool bStateChanged;
+};
+
+}}}
+
+#endif // OVR_OSX_Gamepad_h
diff --git a/Samples/CommonSrc/Platform/OSX_Platform.h b/Samples/CommonSrc/Platform/OSX_Platform.h
new file mode 100644
index 0000000..6803aab
--- /dev/null
+++ b/Samples/CommonSrc/Platform/OSX_Platform.h
@@ -0,0 +1,113 @@
+/************************************************************************************
+
+Filename : OSX_Platform.h
+Content :
+Created :
+Authors :
+
+Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+************************************************************************************/
+
+#include "../Platform/Platform.h"
+#include "../Render/Render_GL_Device.h"
+
+namespace OVR { namespace OvrPlatform { namespace OSX {
+
+class PlatformCore : public OvrPlatform::PlatformCore
+{
+public:
+ void* Win;
+ void* View;
+ void* NsApp;
+ bool Quit;
+ int ExitCode;
+ int Width, Height;
+ MouseMode MMode;
+
+ void RunIdle();
+
+public:
+ PlatformCore(Application* app, void* nsapp);
+ ~PlatformCore();
+
+ void* SetupWindow(int w, int h);
+ void Exit(int exitcode);
+
+ RenderDevice* SetupGraphics(const SetupGraphicsDeviceSet& setupGraphicsDesc,
+ const char* gtype, const Render::RendererParams& rp);
+
+ void SetMouseMode(MouseMode mm);
+ void GetWindowSize(int* w, int* h) const;
+
+ void SetWindowTitle(const char*title);
+
+ void ShowWindow(bool show);
+ void DestroyWindow();
+ bool SetFullscreen(const Render::RendererParams& rp, int fullscreen);
+ int GetDisplayCount();
+ Render::DisplayId GetDisplay(int screen);
+
+ String GetContentDirectory() const;
+};
+
+}}
+namespace Render { namespace GL { namespace OSX {
+
+class RenderDevice : public Render::GL::RenderDevice
+{
+public:
+ void* Context;
+
+ RenderDevice(const Render::RendererParams& p, void* context)
+ : GL::RenderDevice(p), Context(context) {}
+
+ virtual void Shutdown();
+ virtual void Present(bool useVsync);
+
+ virtual bool SetFullscreen(DisplayMode fullscreen);
+
+ virtual ovrRenderAPIConfig Get_ovrRenderAPIConfig() const;
+
+ // oswnd = X11::PlatformCore*
+ static Render::RenderDevice* CreateDevice(const RendererParams& rp, void* oswnd);
+};
+
+}}}}
+
+
+// OVR_PLATFORM_APP_ARGS specifies the Application class to use for startup,
+// providing it with startup arguments.
+#define OVR_PLATFORM_APP_ARGS_WITH_LOG(AppClass, LogClass, args) \
+OVR::OvrPlatform::Application* OVR::OvrPlatform::Application::CreateApplication() \
+{ static LogClass log; OVR::System::Init(&log); \
+return new AppClass args; } \
+void OVR::OvrPlatform::Application::DestroyApplication(OVR::OvrPlatform::Application* app) \
+{ OVR::OvrPlatform::PlatformCore* platform = app->pPlatform; \
+delete app; delete platform; OVR::System::Destroy(); };
+
+#define OVR_PLATFORM_APP_ARGS(AppClass, args) \
+ OVR::OvrPlatform::Application* OVR::OvrPlatform::Application::CreateApplication() \
+{ OVR::System::Init(OVR::Log::ConfigureDefaultLog(OVR::LogMask_All)); \
+ return new AppClass args; } \
+ void OVR::OvrPlatform::Application::DestroyApplication(OVR::OvrPlatform::Application* app) \
+{ OVR::OvrPlatform::PlatformCore* platform = app->pPlatform; \
+ delete app; delete platform; OVR::System::Destroy(); };
+
+// OVR_PLATFORM_APP_ARGS specifies the Application startup class with no args.
+#define OVR_PLATFORM_APP(AppClass) OVR_PLATFORM_APP_ARGS(AppClass, ())
+
+#define OVR_PLATFORM_APP_WITH_LOG(AppClass,LogClass) OVR_PLATFORM_APP_ARGS_WITH_LOG(AppClass,LogClass, ())
+
diff --git a/Samples/CommonSrc/Platform/OSX_Platform.mm b/Samples/CommonSrc/Platform/OSX_Platform.mm
new file mode 100644
index 0000000..71e333f
--- /dev/null
+++ b/Samples/CommonSrc/Platform/OSX_Platform.mm
@@ -0,0 +1,553 @@
+/************************************************************************************
+
+Filename : OSX_Platform.mm
+Content :
+Created :
+Authors :
+
+Copyright : Copyright 2012 Oculus, Inc. All Rights reserved.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+************************************************************************************/
+
+#import "../Platform/OSX_PlatformObjc.h"
+
+using namespace OVR;
+using namespace OVR::OvrPlatform;
+
+@implementation OVRApp
+
+- (void)dealloc
+{
+ [super dealloc];
+}
+
+- (void)run
+{
+ NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
+ _running = YES;
+ OVR::OvrPlatform::Application* app;
+ {
+ using namespace OVR;
+ using namespace OVR::OvrPlatform;
+
+ // CreateApplication must be the first call since it does OVR::System::Initialize.
+ app = Application::CreateApplication();
+ OSX::PlatformCore* platform = new OSX::PlatformCore(app, self);
+ // The platform attached to an app will be deleted by DestroyApplication.
+ app->SetPlatformCore(platform);
+
+ [self setApp:app];
+ [self setPlatform:platform];
+
+ const char* argv[] = {"OVRApp"};
+ int exitCode = app->OnStartup(1, argv);
+ if (exitCode)
+ {
+ Application::DestroyApplication(app);
+ exit(exitCode);
+ }
+ }
+ [self finishLaunching];
+ [pool drain];
+
+ while ([self isRunning])
+ {
+ pool = [[NSAutoreleasePool alloc] init];
+ NSEvent* event = [self nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES];
+ if (event)
+ {
+ [self sendEvent:event];
+ }
+ _App->OnIdle();
+ [pool drain];
+ }
+ OVR::OvrPlatform::Application::DestroyApplication(app);
+}
+
+@end
+
+static int KeyMap[][2] =
+{
+ { NSDeleteFunctionKey, OVR::Key_Delete },
+ { '\t', OVR::Key_Tab },
+ { '\n', OVR::Key_Return },
+ { NSPauseFunctionKey, OVR::Key_Pause },
+ { 27, OVR::Key_Escape },
+ { 127, OVR::Key_Backspace },
+ { ' ', OVR::Key_Space },
+ { NSPageUpFunctionKey, OVR::Key_PageUp },
+ { NSPageDownFunctionKey, OVR::Key_PageDown },
+ { NSNextFunctionKey, OVR::Key_PageDown },
+ { NSEndFunctionKey, OVR::Key_End },
+ { NSHomeFunctionKey, OVR::Key_Home },
+ { NSLeftArrowFunctionKey, OVR::Key_Left },
+ { NSUpArrowFunctionKey, OVR::Key_Up },
+ { NSRightArrowFunctionKey, OVR::Key_Right },
+ { NSDownArrowFunctionKey, OVR::Key_Down },
+ { NSInsertFunctionKey, OVR::Key_Insert },
+ { NSDeleteFunctionKey, OVR::Key_Delete },
+ { NSHelpFunctionKey, OVR::Key_Insert },
+};
+
+
+static KeyCode MapToKeyCode(wchar_t vk)
+{
+ unsigned key = Key_None;
+
+ if ((vk >= 'a') && (vk <= 'z'))
+ {
+ key = vk - 'a' + Key_A;
+ }
+ else if ((vk >= ' ') && (vk <= '~'))
+ {
+ key = vk;
+ }
+ else if ((vk >= '0') && (vk <= '9'))
+ {
+ key = vk - '0' + Key_Num0;
+ }
+ else if ((vk >= NSF1FunctionKey) && (vk <= NSF15FunctionKey))
+ {
+ key = vk - NSF1FunctionKey + Key_F1;
+ }
+ else
+ {
+ for (unsigned i = 0; i< (sizeof(KeyMap) / sizeof(KeyMap[1])); i++)
+ {
+ if (vk == KeyMap[i][0])
+ {
+ key = KeyMap[i][1];
+ break;
+ }
+ }
+ }
+
+ return (KeyCode)key;
+}
+
+static int MapModifiers(unsigned long xmod)
+{
+ int mod = 0;
+ if (xmod & NSShiftKeyMask)
+ mod |= OVR::OvrPlatform::Mod_Shift;
+ if (xmod & NSCommandKeyMask)
+ mod |= OVR::OvrPlatform::Mod_Control;
+ if (xmod & NSAlternateKeyMask)
+ mod |= OVR::OvrPlatform::Mod_Alt;
+ if (xmod & NSControlKeyMask)
+ mod |= OVR::OvrPlatform::Mod_Meta;
+ return mod;
+}
+
+@implementation OVRView
+
+-(BOOL) acceptsFirstResponder
+{
+ return YES;
+}
+-(BOOL) acceptsFirstMouse:(NSEvent *)ev
+{
+ return YES;
+}
+
++(CGDirectDisplayID) displayFromScreen:(NSScreen *)s
+{
+ NSNumber* didref = (NSNumber*)[[s deviceDescription] objectForKey:@"NSScreenNumber"];
+ CGDirectDisplayID disp = (CGDirectDisplayID)[didref longValue];
+ return disp;
+}
+
+-(void) warpMouseToCenter
+{
+ NSPoint w;
+ w.x = _Platform->Width/2.0f;
+ w.y = _Platform->Height/2.0f;
+ w = [[self window] convertBaseToScreen:w];
+ CGDirectDisplayID disp = [OVRView displayFromScreen:[[self window] screen]];
+ CGPoint p = {w.x, CGDisplayPixelsHigh(disp)-w.y};
+ CGDisplayMoveCursorToPoint(disp, p);
+}
+
+static bool LookupKey(NSEvent* ev, wchar_t& ch, OVR::KeyCode& key, unsigned& mods)
+{
+ NSString* chars = [ev charactersIgnoringModifiers];
+ if ([chars length] == 0)
+ return false;
+ ch = [chars characterAtIndex:0];
+ mods = MapModifiers([ev modifierFlags]);
+
+ // check for Cmd+Latin Letter
+ NSString* modchars = [ev characters];
+ if ([modchars length])
+ {
+ wchar_t modch = [modchars characterAtIndex:0];
+ if (modch >= 'a' && modch <= 'z')
+ ch = modch;
+ }
+ key = MapToKeyCode(ch);
+ return true;
+}
+
+-(void) keyDown:(NSEvent*)ev
+{
+ OVR::KeyCode key;
+ unsigned mods;
+ wchar_t ch;
+ if (!LookupKey(ev, ch, key, mods))
+ return;
+ if (key == Key_Escape && _Platform->MMode == Mouse_Relative)
+ {
+ [self warpMouseToCenter];
+ CGAssociateMouseAndMouseCursorPosition(true);
+ [NSCursor unhide];
+ _Platform->MMode = Mouse_RelativeEscaped;
+ }
+ _App->OnKey(key, ch, true, mods);
+}
+-(void) keyUp:(NSEvent*)ev
+{
+ OVR::KeyCode key;
+ unsigned mods;
+ wchar_t ch;
+ if (LookupKey(ev, ch, key, mods))
+ _App->OnKey(key, ch, false, mods);
+}
+
+static const OVR::KeyCode ModifierKeys[] = {OVR::Key_None, OVR::Key_Shift, OVR::Key_Control, OVR::Key_Alt, OVR::Key_Meta};
+
+-(void)flagsChanged:(NSEvent *)ev
+{
+ unsigned long cmods = [ev modifierFlags];
+ if ((cmods & 0xffff0000) != _Modifiers)
+ {
+ uint32_t mods = MapModifiers(cmods);
+ for (int i = 1; i <= 4; i++)
+ {
+ unsigned long m = (1 << (16+i));
+ if ((cmods & m) != (_Modifiers & m))
+ {
+ if (cmods & m)
+ _App->OnKey(ModifierKeys[i], 0, true, mods);
+ else
+ _App->OnKey(ModifierKeys[i], 0, false, mods);
+ }
+ }
+ _Modifiers = cmods & 0xffff0000;
+ }
+}
+
+-(void)ProcessMouse:(NSEvent*)ev
+{
+ switch ([ev type])
+ {
+ case NSLeftMouseDragged:
+ case NSRightMouseDragged:
+ case NSOtherMouseDragged:
+ case NSMouseMoved:
+ {
+ if (_Platform->MMode == OVR::OvrPlatform::Mouse_Relative)
+ {
+ int dx = [ev deltaX];
+ int dy = [ev deltaY];
+
+ if (dx != 0 || dy != 0)
+ {
+ _App->OnMouseMove(dx, dy, Mod_MouseRelative|MapModifiers([ev modifierFlags]));
+ [self warpMouseToCenter];
+ }
+ }
+ else
+ {
+ NSPoint p = [ev locationInWindow];
+ _App->OnMouseMove(p.x, p.y, MapModifiers([ev modifierFlags]));
+ }
+ }
+ break;
+ case NSLeftMouseDown:
+ case NSRightMouseDown:
+ case NSOtherMouseDown:
+ break;
+ }
+}
+
+-(void) mouseMoved:(NSEvent*)ev
+{
+ [self ProcessMouse:ev];
+}
+-(void) mouseDragged:(NSEvent*)ev
+{
+ [self ProcessMouse:ev];
+}
+-(void) mouseDown:(NSEvent*)ev
+{
+ if (_Platform->MMode == Mouse_RelativeEscaped)
+ {
+ [self warpMouseToCenter];
+ CGAssociateMouseAndMouseCursorPosition(false);
+ [NSCursor hide];
+ _Platform->MMode = Mouse_Relative;
+ }
+}
+
+//-(void)
+
+-(id) initWithFrame:(NSRect)frameRect
+{
+ NSOpenGLPixelFormatAttribute attr[] =
+ {
+// NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
+// NSOpenGLPFAWindow,
+ NSOpenGLPFADoubleBuffer,
+ NSOpenGLPFADepthSize, 24,
+ nil
+ };
+
+ NSOpenGLPixelFormat *pf = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attr] autorelease];
+
+ self = [super initWithFrame:frameRect pixelFormat:pf];
+ GLint swap = 0;
+ [[self openGLContext] setValues:&swap forParameter:NSOpenGLCPSwapInterval];
+ //[self setWantsBestResolutionOpenGLSurface:YES];
+ return self;
+}
+
+-(void) reshape
+{
+ NSRect bounds = [self bounds];
+ _App->OnResize(bounds.size.width, bounds.size.height);
+
+ _Platform->Width = bounds.size.width;
+ _Platform->Height = bounds.size.height;
+
+ if (_Platform->GetRenderer())
+ _Platform->GetRenderer()->SetWindowSize(bounds.size.width, bounds.size.height);
+}
+
+-(BOOL)windowShouldClose:(id)sender
+{
+ if (_Platform)
+ _Platform->Exit(0);
+ else
+ exit(0);
+ return 1;
+}
+
+@end
+
+namespace OVR { namespace OvrPlatform { namespace OSX {
+
+PlatformCore::PlatformCore(Application* app, void* nsapp)
+ : OvrPlatform::PlatformCore(app), NsApp(nsapp), Win(NULL), View(NULL), Quit(0), MMode(Mouse_Normal)
+{
+ pGamepadManager = *new OSX::GamepadManager();
+}
+PlatformCore::~PlatformCore()
+{
+}
+
+void PlatformCore::Exit(int exitcode)
+{
+ OVRApp* nsApp = (OVRApp*)NsApp;
+ [nsApp stop:nil];
+}
+
+String PlatformCore::GetContentDirectory() const
+{
+ NSBundle* bundle = [NSBundle mainBundle];
+ if (bundle)
+ return String([[bundle bundlePath] UTF8String]) + "/Contents/Resources";
+ else
+ return ".";
+}
+
+
+void PlatformCore::SetMouseMode(MouseMode mm)
+{
+ if (mm == MMode)
+ return;
+
+ if (Win)
+ {
+ if (mm == Mouse_Relative)
+ {
+ [NSCursor hide];
+ [(OVRView*)View warpMouseToCenter];
+ CGAssociateMouseAndMouseCursorPosition(false);
+ }
+ else
+ {
+ if (MMode == Mouse_Relative)
+ {
+ CGAssociateMouseAndMouseCursorPosition(true);
+ [NSCursor unhide];
+ [(OVRView*)View warpMouseToCenter];
+ }
+ }
+ }
+ MMode = mm;
+}
+
+
+void PlatformCore::GetWindowSize(int* w, int* h) const
+{
+ *w = Width;
+ *h = Height;
+}
+
+void* PlatformCore::SetupWindow(int w, int h)
+{
+ NSRect winrect;
+ winrect.origin.x = 0;
+ winrect.origin.y = 1000;
+ winrect.size.width = w;
+ winrect.size.height = h;
+ NSWindow* win = [[NSWindow alloc] initWithContentRect:winrect styleMask:NSTitledWindowMask|NSClosableWindowMask backing:NSBackingStoreBuffered defer:NO];
+
+ OVRView* view = [[OVRView alloc] initWithFrame:winrect];
+ [view setPlatform:this];
+ [win setContentView:view];
+ [win setAcceptsMouseMovedEvents:YES];
+ [win setDelegate:view];
+ [view setApp:pApp];
+ Win = win;
+ View = view;
+ return (void*)[win windowNumber];
+}
+
+void PlatformCore::SetWindowTitle(const char* title)
+{
+ [((NSWindow*)Win) setTitle:[[NSString alloc] initWithBytes:title length:strlen(title) encoding:NSUTF8StringEncoding]];
+}
+
+void PlatformCore::ShowWindow(bool show)
+{
+ if (show)
+ [((NSWindow*)Win) makeKeyAndOrderFront:nil];
+ else
+ [((NSWindow*)Win) orderOut:nil];
+}
+
+void PlatformCore::DestroyWindow()
+{
+ [((NSWindow*)Win) close];
+ Win = NULL;
+}
+
+RenderDevice* PlatformCore::SetupGraphics(const SetupGraphicsDeviceSet& setupGraphicsDesc,
+ const char* type, const Render::RendererParams& rp)
+{
+ const SetupGraphicsDeviceSet* setupDesc = setupGraphicsDesc.PickSetupDevice(type);
+ OVR_ASSERT(setupDesc);
+
+ pRender = *setupDesc->pCreateDevice(rp, this);
+ if (pRender)
+ pRender->SetWindowSize(Width, Height);
+
+ return pRender.GetPtr();
+}
+
+int PlatformCore::GetDisplayCount()
+{
+ return (int)[[NSScreen screens] count];
+}
+
+Render::DisplayId PlatformCore::GetDisplay(int i)
+{
+ NSScreen* s = (NSScreen*)[[NSScreen screens] objectAtIndex:i];
+ return Render::DisplayId([OVRView displayFromScreen:s]);
+}
+
+bool PlatformCore::SetFullscreen(const Render::RendererParams& rp, int fullscreen)
+{
+ if (fullscreen == Render::Display_Window)
+ [(OVRView*)View exitFullScreenModeWithOptions:nil];
+ else
+ {
+ NSScreen* usescreen = [NSScreen mainScreen];
+ NSArray* screens = [NSScreen screens];
+ for (int i = 0; i < [screens count]; i++)
+ {
+ NSScreen* s = (NSScreen*)[screens objectAtIndex:i];
+ CGDirectDisplayID disp = [OVRView displayFromScreen:s];
+
+ if (disp == rp.Display.CgDisplayId)
+ usescreen = s;
+ }
+
+ [(OVRView*)View enterFullScreenMode:usescreen withOptions:nil];
+ [(NSWindow*)Win setInitialFirstResponder:(OVRView*)View];
+ [(NSWindow*)Win makeFirstResponder:(OVRView*)View];
+ }
+
+ if (pRender)
+ pRender->SetFullscreen((Render::DisplayMode)fullscreen);
+ return 1;
+}
+
+}}
+// GL
+namespace Render { namespace GL { namespace OSX {
+
+ovrRenderAPIConfig RenderDevice::Get_ovrRenderAPIConfig() const
+{
+ ovrRenderAPIConfig result = ovrRenderAPIConfig();
+ result.Header.API = ovrRenderAPI_OpenGL;
+ result.Header.RTSize = Sizei(WindowWidth, WindowHeight);
+ result.Header.Multisample = Params.Multisample;
+ return result;
+}
+
+Render::RenderDevice* RenderDevice::CreateDevice(const RendererParams& rp, void* oswnd)
+{
+ OvrPlatform::OSX::PlatformCore* PC = (OvrPlatform::OSX::PlatformCore*)oswnd;
+
+ OVRView* view = (OVRView*)PC->View;
+ NSOpenGLContext *context = [view openGLContext];
+ if (!context)
+ return NULL;
+
+ [context makeCurrentContext];
+ [((NSWindow*)PC->Win) makeKeyAndOrderFront:nil];
+
+ return new Render::GL::OSX::RenderDevice(rp, context);
+}
+
+void RenderDevice::Present(bool useVsync)
+{
+ NSOpenGLContext *context = (NSOpenGLContext*)Context;
+ [context flushBuffer];
+}
+
+void RenderDevice::Shutdown()
+{
+ Context = NULL;
+}
+
+bool RenderDevice::SetFullscreen(DisplayMode fullscreen)
+{
+ Params.Fullscreen = fullscreen;
+ return 1;
+}
+
+}}}}
+
+
+int main(int argc, char *argv[])
+{
+ NSApplication* nsapp = [OVRApp sharedApplication];
+ [nsapp run];
+ return 0;
+}
+
diff --git a/Samples/CommonSrc/Platform/OSX_PlatformObjc.h b/Samples/CommonSrc/Platform/OSX_PlatformObjc.h
new file mode 100644
index 0000000..967b5d9
--- /dev/null
+++ b/Samples/CommonSrc/Platform/OSX_PlatformObjc.h
@@ -0,0 +1,53 @@
+/***********************************************************************
+
+Filename : OSX_PlatformObjc.h
+Content :
+Created :
+Authors :
+
+Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+************************************************************************/
+
+#import <Cocoa/Cocoa.h>
+#import "OSX_Platform.h"
+#import "OSX_Gamepad.h"
+
+#import <CoreGraphics/CoreGraphics.h>
+#import <CoreGraphics/CGDirectDisplay.h>
+
+@interface OVRApp : NSApplication
+
+@property (assign) IBOutlet NSWindow* win;
+@property (assign) OVR::OvrPlatform::OSX::PlatformCore* Platform;
+@property (assign) OVR::OvrPlatform::Application* App;
+
+-(void) run;
+
+@end
+
+@interface OVRView : NSOpenGLView <NSWindowDelegate>
+
+@property (assign) OVR::OvrPlatform::OSX::PlatformCore* Platform;
+@property (assign) OVR::OvrPlatform::Application* App;
+@property unsigned long Modifiers;
+
+-(void)ProcessMouse:(NSEvent*)event;
+-(void)warpMouseToCenter;
+
++(CGDirectDisplayID) displayFromScreen:(NSScreen*)s;
+
+@end
+
diff --git a/Samples/CommonSrc/Platform/OSX_WavPlayer.cpp b/Samples/CommonSrc/Platform/OSX_WavPlayer.cpp
new file mode 100644
index 0000000..8b4c744
--- /dev/null
+++ b/Samples/CommonSrc/Platform/OSX_WavPlayer.cpp
@@ -0,0 +1,250 @@
+/************************************************************************************
+
+Filename : WavPlayer_OSX.cpp
+Content : An Apple OSX audio handler.
+Created : March 5, 2013
+Authors : Robotic Arm Software - Peter Hoff, Dan Goodman, Bryan Croteau
+
+Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+************************************************************************************/
+
+#include "OSX_WavPlayer.h"
+
+namespace OVR { namespace OvrPlatform { namespace OSX {
+
+WavPlayer::WavPlayer(const char* fileName)
+{
+ FileName = fileName;
+}
+
+bool WavPlayer::isDataChunk(unsigned char* buffer, int index)
+{
+ unsigned char a = buffer[index];
+ unsigned char b = buffer[index + 1];
+ unsigned char c = buffer[index + 2];
+ unsigned char d = buffer[index + 3];
+ return (a == 'D' || a == 'd') && (b == 'A' || b == 'a') &&
+ (c == 'T' || c == 't') && (d == 'A' || d == 'a');
+}
+
+int WavPlayer::getWord(unsigned char* buffer, int index)
+{
+ unsigned char a = buffer[index];
+ unsigned char b = buffer[index + 1];
+ unsigned char c = buffer[index + 2];
+ unsigned char d = buffer[index + 3];
+ int result = 0;
+ result |= a;
+ result |= b << 8;
+ result |= c << 16;
+ result |= d << 24;
+ return result;
+}
+
+short WavPlayer::getHalf(unsigned char* buffer, int index)
+{
+ unsigned char a = buffer[index];
+ unsigned char b = buffer[index + 1];
+ short result = 0;
+ result |= a;
+ result |= b << 8;
+ return result;
+}
+
+void *WavPlayer::LoadPCM(const char *filename, unsigned long *len)
+{
+ FILE *file;
+ struct stat s;
+ void *pcm;
+
+ if(stat(filename, &s))
+ {
+ return NULL;
+ }
+ *len = s.st_size;
+ pcm = (void *) malloc(s.st_size);
+ if(!pcm)
+ {
+ return NULL;
+ }
+ file = fopen(filename, "rb");
+ if(!file)
+ {
+ free(pcm);
+ return NULL;
+ }
+ fread(pcm, s.st_size, 1, file);
+ fclose(file);
+ return pcm;
+}
+
+int WavPlayer::PlayBuffer(void *pcmbuffer, unsigned long len)
+{
+ AQCallbackStruct aqc;
+ UInt32 err, bufferSize;
+ int i;
+
+ aqc.DataFormat.mSampleRate = SampleRate;
+ aqc.DataFormat.mFormatID = kAudioFormatLinearPCM;
+ if(BitsPerSample == 16)
+ {
+ aqc.DataFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger
+ | kAudioFormatFlagIsPacked;
+ }
+ aqc.DataFormat.mBytesPerPacket = NumChannels * (BitsPerSample / 8);
+ aqc.DataFormat.mFramesPerPacket = 1;
+ aqc.DataFormat.mBytesPerFrame = NumChannels * (BitsPerSample / 8);
+ aqc.DataFormat.mChannelsPerFrame = NumChannels;
+ aqc.DataFormat.mBitsPerChannel = BitsPerSample;
+ aqc.FrameCount = SampleRate / 60;
+ aqc.SampleLen = (UInt32)(len);
+ aqc.PlayPtr = 0;
+ aqc.PCMBuffer = static_cast<unsigned char*>(pcmbuffer);
+
+ err = AudioQueueNewOutput(&aqc.DataFormat,
+ aqBufferCallback,
+ &aqc,
+ NULL,
+ kCFRunLoopCommonModes,
+ 0,
+ &aqc.Queue);
+ if(err)
+ {
+ return err;
+ }
+
+ aqc.FrameCount = SampleRate / 60;
+ bufferSize = aqc.FrameCount * aqc.DataFormat.mBytesPerPacket;
+
+ for(i = 0; i < AUDIO_BUFFERS; i++)
+ {
+ err = AudioQueueAllocateBuffer(aqc.Queue, bufferSize,
+ &aqc.Buffers[i]);
+ if(err)
+ {
+ return err;
+ }
+ aqBufferCallback(&aqc, aqc.Queue, aqc.Buffers[i]);
+ }
+
+ err = AudioQueueStart(aqc.Queue, NULL);
+ if(err)
+ {
+ return err;
+ }
+
+ while(true)
+ {
+ }
+ sleep(1);
+ return 0;
+}
+
+void WavPlayer::aqBufferCallback(void *in, AudioQueueRef inQ, AudioQueueBufferRef outQB)
+{
+ AQCallbackStruct *aqc;
+ unsigned char *coreAudioBuffer;
+
+ aqc = (AQCallbackStruct *) in;
+ coreAudioBuffer = (unsigned char*) outQB->mAudioData;
+
+ printf("Sync: %u / %u\n", aqc->PlayPtr, aqc->SampleLen);
+
+ if(aqc->FrameCount > 0)
+ {
+ outQB->mAudioDataByteSize = aqc->DataFormat.mBytesPerFrame * aqc->FrameCount;
+ for(int i = 0; i < aqc->FrameCount * aqc->DataFormat.mBytesPerFrame; i++)
+ {
+ if(aqc->PlayPtr > aqc->SampleLen)
+ {
+ aqc->PlayPtr = 0;
+ i = 0;
+ }
+ coreAudioBuffer[i] = aqc->PCMBuffer[aqc->PlayPtr];
+ aqc->PlayPtr++;
+ }
+ AudioQueueEnqueueBuffer(inQ, outQB, 0, NULL);
+ }
+}
+
+int WavPlayer::PlayAudio()
+{
+ unsigned long len;
+ void *pcmbuffer;
+ int ret;
+
+ pcmbuffer = LoadPCM(FileName, &len);
+ if(!pcmbuffer)
+ {
+ fprintf(stderr, "%s: %s\n", FileName, strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ unsigned char* bytes = (unsigned char*)pcmbuffer;
+ int index = 0;
+
+ // 'RIFF'
+ getWord(bytes, index);
+ index += 4;
+ // int Length
+ getWord(bytes, index);
+ index += 4;
+ // 'WAVE'
+ getWord(bytes, index);
+ index += 4;
+ // 'fmt '
+ getWord(bytes, index);
+ index += 4;
+
+ // int Format Length
+ int fmtLen = getWord(bytes, index);
+ index += 4;
+ AudioFormat = getHalf(bytes, index);
+ index += 2;
+ NumChannels = getHalf(bytes, index);
+ index += 2;
+ SampleRate = getWord(bytes, index);
+ index += 4;
+ ByteRate = getWord(bytes, index);
+ index += 4;
+ BlockAlign = getHalf(bytes, index);
+ index += 2;
+ BitsPerSample = getHalf(bytes, index);
+ index += 2;
+ index += fmtLen - 16;
+ while(!isDataChunk(bytes, index))
+ {
+ // Any Chunk
+ getWord(bytes, index);
+ index += 4;
+ // Any Chunk Length
+ int anyChunkLen = getWord(bytes, index);
+ index += 4 + anyChunkLen;
+ }
+ // 'data'
+ getWord(bytes, index);
+ index += 4;
+ // int Data Length
+ unsigned long dataLen = getWord(bytes, index);
+ index += 4;
+ unsigned char* target = &bytes[index];
+
+ ret = PlayBuffer((void *)target, dataLen);
+ free(pcmbuffer);
+ return ret;
+}
+
+}}}
diff --git a/Samples/CommonSrc/Platform/OSX_WavPlayer.h b/Samples/CommonSrc/Platform/OSX_WavPlayer.h
new file mode 100644
index 0000000..c6f2571
--- /dev/null
+++ b/Samples/CommonSrc/Platform/OSX_WavPlayer.h
@@ -0,0 +1,72 @@
+/************************************************************************************
+
+Filename : WavPlayer_OSX.h
+Content : An Apple OSX audio handler.
+Created : March 5, 2013
+Authors : Robotic Arm Software - Peter Hoff, Dan Goodman, Bryan Croteau
+
+Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+************************************************************************************/
+
+#ifndef OVR_WavPlayer_h
+#define OVR_WavPlayer_h
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <AudioToolbox/AudioQueue.h>
+
+#define AUDIO_BUFFERS 4
+
+namespace OVR { namespace OvrPlatform { namespace OSX {
+
+typedef struct AQCallbackStruct
+{
+ AudioQueueRef Queue;
+ UInt32 FrameCount;
+ AudioQueueBufferRef Buffers[AUDIO_BUFFERS];
+ AudioStreamBasicDescription DataFormat;
+ UInt32 PlayPtr;
+ UInt32 SampleLen;
+ unsigned char* PCMBuffer;
+} AQCallbackStruct;
+
+class WavPlayer
+{
+public:
+ WavPlayer(const char* fileName);
+ int PlayAudio();
+private:
+ bool isDataChunk(unsigned char* buffer, int index);
+ int getWord(unsigned char* buffer, int index);
+ short getHalf(unsigned char* buffer, int index);
+ void *LoadPCM(const char *filename, unsigned long *len);
+ int PlayBuffer(void *pcm, unsigned long len);
+ static void aqBufferCallback(void *in, AudioQueueRef inQ, AudioQueueBufferRef outQB);
+
+ short AudioFormat;
+ short NumChannels;
+ int SampleRate;
+ int ByteRate;
+ short BlockAlign;
+ short BitsPerSample;
+ const char* FileName;
+};
+
+}}}
+
+#endif