Add X11 loader

pull/131/head
jackun 4 years ago
parent 108bca7d4f
commit 329b5d66c2
No known key found for this signature in database
GPG Key ID: 119DB3F1D05A9ED3

@ -0,0 +1,87 @@
#include "loader_x11.h"
libx11_loader::libx11_loader() : loaded_(false) {
}
libx11_loader::~libx11_loader() {
CleanUp(loaded_);
}
bool libx11_loader::Load(const std::string& library_name) {
if (loaded_) {
return false;
}
library_ = dlopen(library_name.c_str(), RTLD_LAZY);
if (!library_)
return false;
XOpenDisplay =
reinterpret_cast<decltype(this->XOpenDisplay)>(
dlsym(library_, "XOpenDisplay"));
if (!XOpenDisplay) {
CleanUp(true);
return false;
}
XCloseDisplay =
reinterpret_cast<decltype(this->XCloseDisplay)>(
dlsym(library_, "XCloseDisplay"));
if (!XCloseDisplay) {
CleanUp(true);
return false;
}
XQueryKeymap =
reinterpret_cast<decltype(this->XQueryKeymap)>(
dlsym(library_, "XQueryKeymap"));
if (!XQueryKeymap) {
CleanUp(true);
return false;
}
XKeysymToKeycode =
reinterpret_cast<decltype(this->XKeysymToKeycode)>(
dlsym(library_, "XKeysymToKeycode"));
if (!XKeysymToKeycode) {
CleanUp(true);
return false;
}
XStringToKeysym =
reinterpret_cast<decltype(this->XStringToKeysym)>(
dlsym(library_, "XStringToKeysym"));
if (!XStringToKeysym) {
CleanUp(true);
return false;
}
XGetGeometry =
reinterpret_cast<decltype(this->XGetGeometry)>(
dlsym(library_, "XGetGeometry"));
if (!XGetGeometry) {
CleanUp(true);
return false;
}
loaded_ = true;
return true;
}
void libx11_loader::CleanUp(bool unload) {
if (unload) {
dlclose(library_);
library_ = NULL;
}
loaded_ = false;
XOpenDisplay = NULL;
XCloseDisplay = NULL;
XQueryKeymap = NULL;
XKeysymToKeycode = NULL;
XGetGeometry = NULL;
}
std::shared_ptr<libx11_loader> g_x11(new libx11_loader("libX11.so.6"));

@ -0,0 +1,36 @@
#pragma once
#include <X11/Xlib.h>
#include <memory>
#include <string>
#include <dlfcn.h>
class libx11_loader {
public:
libx11_loader();
libx11_loader(const std::string& library_name) { Load(library_name); }
~libx11_loader();
bool Load(const std::string& library_name);
bool IsLoaded() { return loaded_; }
decltype(&::XOpenDisplay) XOpenDisplay;
decltype(&::XCloseDisplay) XCloseDisplay;
decltype(&::XQueryKeymap) XQueryKeymap;
decltype(&::XKeysymToKeycode) XKeysymToKeycode;
decltype(&::XStringToKeysym) XStringToKeysym;
decltype(&::XGetGeometry) XGetGeometry;
private:
void CleanUp(bool unload);
void* library_;
bool loaded_;
// Disallow copy constructor and assignment operator.
libx11_loader(const libx11_loader&);
void operator=(const libx11_loader&);
};
extern std::shared_ptr<libx11_loader> g_x11;
Loading…
Cancel
Save