-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Linux/MacOSX port of DriverLoader #10
Changes from 1 commit
c1922d0
93a0275
c981ac6
79c81e9
e2968e0
0f5bd0d
24fd52a
c16a2fa
66fb7e2
d2c79dd
48ff871
c3cc22e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,10 @@ | |
#define WIN32_LEAN_AND_MEAN | ||
#define NO_MINMAX | ||
#include <windows.h> | ||
#elif defined(OSVR_LINUX) | ||
#include <dlfcn.h> | ||
#elif defined(OSVR_MACOSX) | ||
#include <dlfcn.h> | ||
#endif | ||
|
||
namespace osvr { | ||
|
@@ -48,10 +52,8 @@ namespace vive { | |
/// Platform-specific handle to dynamic library | ||
#if defined(OSVR_WINDOWS) | ||
HMODULE driver_ = nullptr; | ||
#elif defined(OSVR_MACOSX) | ||
#error "Implementation incomplete!" | ||
#elif defined(OSVR_LINUX) | ||
#error "Implementation incomplete!" | ||
#elif defined(OSVR_MACOSX) || defined(OSVR_LINUX) | ||
void *driver_ = nullptr; | ||
#endif | ||
|
||
/// Destructor: should contain platform-specific code to unload dynamic | ||
|
@@ -61,10 +63,10 @@ namespace vive { | |
if (driver_) { | ||
FreeLibrary(driver_); | ||
} | ||
#elif defined(OSVR_MACOSX) | ||
#error "Implementation incomplete! Unload dynamic library here!" | ||
#elif defined(OSVR_LINUX) | ||
#error "Implementation incomplete! Unload dynamic library here!" | ||
#elif defined(OSVR_MACOSX) || defined(OSVR_LINUX) | ||
if (driver_) { | ||
dlclose(driver_); | ||
} | ||
#endif | ||
} | ||
}; | ||
|
@@ -91,12 +93,19 @@ namespace vive { | |
throw CouldNotLoadEntryPoint(); | ||
} | ||
factory_ = reinterpret_cast<DriverFactory>(proc); | ||
#elif defined(OSVR_MACOSX) | ||
#error \ | ||
"Implementation incomplete! Load dynamic library here and retrieve entry point function here!" | ||
#elif defined(OSVR_LINUX) | ||
#error \ | ||
"Implementation incomplete! Load dynamic library here and retrieve entry point function here!" | ||
#elif defined(OSVR_LINUX) || defined(OSVR_MACOSX) | ||
impl_->driver_ = dlopen(driverFile.c_str()); | ||
if (!impl_->driver_) { | ||
reset(); | ||
throw CouldNotLoadDriverModule(dlerror()); | ||
} | ||
|
||
auto proc = dlsym(impl_->driver_, ENTRY_POINT_FUNCTION_NAME); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dlerror() returns NULL when is no error. But in that case, this exception will be never thrown. Also, calling dlerror() without taking returned pointer to string -- is really bad idea. |
||
if (!proc) { | ||
reset(); | ||
throw CouldNotLoadEntryPoint(dlerror()); | ||
} | ||
factory_ = reinterpret_cast<DriverFactory>(proc); \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is there a backslash at the end of this line (line 108)? Seems to break the build for me |
||
#endif | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,14 +40,28 @@ | |
namespace osvr { | ||
namespace vive { | ||
struct CouldNotLoadDriverModule : std::runtime_error { | ||
#if defined(OSVR_LINUX) || defined(OSVR_MACOSX) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To have these defines, you need to include There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know, is Windows can give a error string. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually meant leave both constructors - a default constructor and one that takes an extra error message, with no platform ifdefs in this header at all... |
||
CouldNotLoadDriverModule(const char *errString) | ||
: std::runtime_error( | ||
"Could not load driver module." + | ||
std::string(errString)) {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should probably add a space between the hard-coded C-string and the appended error string. |
||
#else | ||
CouldNotLoadDriverModule() | ||
: std::runtime_error("Could not load driver module.") {} | ||
#endif | ||
}; | ||
|
||
struct CouldNotLoadEntryPoint : std::runtime_error { | ||
#if defined(OSVR_LINUX) || defined(OSVR_MACOSX) | ||
CouldNotLoadEntryPoint(const char *errString) | ||
: std::runtime_error( | ||
"Could not load entry point function from driver." + | ||
std::string(errString)) {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a space here, too. |
||
#else | ||
CouldNotLoadEntryPoint() | ||
: std::runtime_error( | ||
"Could not load entry point function from driver.") {} | ||
#endif | ||
}; | ||
|
||
struct CouldNotGetInterface : std::runtime_error { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
dlfcn.h
how you open a.dylib
on Mac OS X, or just how you open a.so
? The terribly confusing part is that I know OS X has both of those... (@godbyk? @d235j?)Update: It looks like it's correct for loading whatever
CMAKE_SHARED_MODULE_SUFFIX
is on Mac. Can't work worse than it does right now, so that's good :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I load libraries the same way in my project. Works perfect on both OSes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth having the same code in separate
OSVR_LINUX
andOSVR_MACOSX
branches or should they be conflated?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just a mistype.