diff options
-rw-r--r-- | src/libgamma-method.cc | 223 | ||||
-rw-r--r-- | src/libgamma-method.hh | 258 | ||||
-rw-r--r-- | src/test.cc | 57 |
3 files changed, 533 insertions, 5 deletions
diff --git a/src/libgamma-method.cc b/src/libgamma-method.cc index 4acd857..a52c455 100644 --- a/src/libgamma-method.cc +++ b/src/libgamma-method.cc @@ -19,6 +19,17 @@ #include <cstdlib> #include <cstring> +#include <cerrno> + + +/** + * TODO temporary + */ +std::string create_error(int error_code) +{ + (void) error_code; + return nullptr; +} namespace libgamma @@ -49,7 +60,7 @@ namespace libgamma /** * Constructor. * - * @param caps The information in the native structure + * @param caps The information in the native structure. */ MethodCapabilities::MethodCapabilities(libgamma_method_capabilities_t* caps) : crtc_information(caps->crtc_information), @@ -177,7 +188,7 @@ namespace libgamma /** * Constructor. * - * @param info The information in the native structure + * @param info The information in the native structure. */ CRTCInformation::CRTCInformation(libgamma_crtc_information_t* info) : edid(info->edid), @@ -329,5 +340,213 @@ namespace libgamma return *this; } + + + /** + * Constructor. + */ + Site::Site() : + method(0), + site(nullptr), + partitions_available(0), + native(nullptr) + { + /* Do nothing. */ + } + + /** + * Constructor. + * + * @param method The adjustment method of the site. + * @param site The site identifier, will be moved into + * the structure, must be `delete`:able. + */ + Site::Site(int method, std::string* site) : + method(method), + site(site), + partitions_available(0), + native(nullptr) + { + char* cstr = nullptr; + int r; + + if (site != nullptr) + { + const char* cstr_ = site->c_str(); + cstr = (char*)malloc((strlen(cstr_) + 1) * sizeof(char)); + memcpy(cstr, cstr_, (strlen(cstr_) + 1) * sizeof(char)); + } + this->native = (libgamma_site_state_t*)malloc(sizeof(libgamma_site_state_t)); + r = libgamma_site_initialise(this->native, method, cstr); + if (r < 0) + { + int saved_errno = errno; + free(this->native); + this->native = nullptr; + errno = saved_errno; + throw create_error(r); + } + this->partitions_available = this->native->partitions_available; + } + + /** + * Destructor. + */ + Site::~Site() + { + if (this->site != nullptr) + delete this->site; + if (this->native != nullptr) + libgamma_site_free(this->native); + } + + /** + * Restore the gamma ramps all CRTC:s with a site to + * the system settings. + */ + void Site::restore() + { + int r; + r = libgamma_site_restore(this->native); + if (r != 0) + throw create_error(r); + } + + + + /** + * Constructor. + */ + Partition::Partition() : + site(nullptr), + partition(0), + crtcs_available(0), + native(nullptr) + { + /* Do nothing. */ + } + + /** + * Constructor. + * + * @param site The site of the partition. + * @param partition The index of the partition. + */ + Partition::Partition(Site* site, size_t partition) : + site(site), + partition(partition), + crtcs_available(0), + native(nullptr) + { + int r; + this->native = (libgamma_partition_state_t*)malloc(sizeof(libgamma_partition_state_t)); + r = libgamma_partition_initialise(this->native, site->native, partition); + if (r < 0) + { + int saved_errno = errno; + free(this->native); + this->native = nullptr; + errno = saved_errno; + throw create_error(r); + } + this->crtcs_available = this->native->crtcs_available; + } + + /** + * Destructor. + */ + Partition::~Partition() + { + if (this->native != nullptr) + libgamma_partition_free(this->native); + } + + /** + * Restore the gamma ramps all CRTC:s with a partition + * to the system settings. + */ + void Partition::restore() + { + int r; + r = libgamma_partition_restore(this->native); + if (r != 0) + throw create_error(r); + } + + + + /** + * Constructor. + */ + CRTC::CRTC() : + partition(nullptr), + crtc(0), + native(nullptr) + { + /* Do nothing. */ + } + + /** + * Constructor. + * + * @param partition The partition of the CRTC. + * @param crtc The index of the CRTC. + */ + CRTC::CRTC(Partition* partition, size_t crtc) : + partition(partition), + crtc(crtc), + native(nullptr) + { + int r; + this->native = (libgamma_crtc_state_t*)malloc(sizeof(libgamma_crtc_state_t)); + r = libgamma_crtc_initialise(this->native, partition->native, crtc); + if (r < 0) + { + int saved_errno = errno; + free(this->native); + this->native = nullptr; + errno = saved_errno; + throw create_error(r); + } + } + + /** + * Destructor. + */ + CRTC::~CRTC() + { + if (this->native != nullptr) + libgamma_crtc_free(this->native); + } + + /** + * Restore the gamma ramps for a CRTC to the system + * settings for that CRTC. + */ + void CRTC::restore() + { + int r; + r = libgamma_crtc_restore(this->native); + if (r != 0) + throw create_error(r); + } + + /** + * Read information about a CRTC. + * + * @param output Instance of a data structure to fill with the information about the CRTC. + * @param fields OR:ed identifiers for the information about the CRTC that should be read. + * @return Whether an error has occurred and is stored in a `*_error` field. + */ + bool CRTC::information(CRTCInformation* output, int32_t fields) + { + libgamma_crtc_information_t info; + int r; + + r = libgamma_get_crtc_information(&info, this->native, fields); + *output = CRTCInformation(&info); + return r != 0; + } + } diff --git a/src/libgamma-method.hh b/src/libgamma-method.hh index 514ae31..9bec563 100644 --- a/src/libgamma-method.hh +++ b/src/libgamma-method.hh @@ -36,6 +36,40 @@ namespace libgamma */ class MethodCapabilities; + /** + * Site state. + * + * On operating systems that integrate a graphical environment + * there is usually just one site. However, one systems with + * pluggable graphics, like Unix-like systems such as GNU/Linux + * and the BSD:s, there can usually be any (feasible) number of + * sites. In X.org parlance they are called displays. + */ + class Site; + + /** + * Partition state. + * + * Probably the majority of display server only one partition + * per site. However, X.org can, and traditional used to have + * on multi-headed environments, multiple partitions per site. + * In X.org partitions are called 'screens'. It is not to be + * confused with monitor. A screen is a collection of monitors, + * and the mapping from monitors to screens is a surjection. + * On hardware-level adjustment methods, such as Direct + * Rendering Manager, a partition is a graphics card. + */ + class Partition; + + /** + * Cathode ray tube controller state. + * + * The CRTC controls the gamma ramps for the + * monitor that is plugged in to the connector + * that the CRTC belongs to. + */ + class CRTC; + /** @@ -52,7 +86,7 @@ namespace libgamma /** * Constructor. * - * @param caps The information in the native structure + * @param caps The information in the native structure. */ MethodCapabilities(libgamma_method_capabilities_t* caps); @@ -187,7 +221,7 @@ namespace libgamma /** * Constructor. * - * @param info The information in the native structure + * @param info The information in the native structure. */ CRTCInformation(libgamma_crtc_information_t* info); @@ -459,7 +493,225 @@ namespace libgamma int gamma_error; }; - + + + +#ifdef __GCC__ +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Weffc++" + /* Lets ignore that we do not override the copy constructor + * and the copy operator. */ +#endif + + /** + * Site state. + * + * On operating systems that integrate a graphical environment + * there is usually just one site. However, one systems with + * pluggable graphics, like Unix-like systems such as GNU/Linux + * and the BSD:s, there can usually be any (feasible) number of + * sites. In X.org parlance they are called displays. + */ + class Site + { + public: + /** + * Constructor. + */ + Site(); + + /** + * Constructor. + * + * @param method The adjustment method of the site. + * @param site The site identifier, will be moved into + * the structure, must be `delete`:able. + */ + Site(int method, std::string* site = nullptr); + + /** + * Destructor. + */ + ~Site(); + + /** + * Restore the gamma ramps all CRTC:s with a site to + * the system settings. + */ + void restore(); + + + + /** + * This field specifies, for the methods if this library, + * which adjustment method (display server and protocol) + * is used to adjust the gamma ramps. + */ + int method; + + /** + * The site identifier. It can either be `nullptr` or a string. + * `nullptr` indicates the default site. On systems like the + * Unix-like systems, where the graphics are pluggable, this + * is usually resolved by an environment variable, such as + * "DISPLAY" for X.org. + */ + std::string* site; + + /** + * The number of partitions that is available on this site. + * Probably the majority of display server only one partition + * per site. However, X.org can, and traditional used to have + * on multi-headed environments, multiple partitions per site. + * In X.org partitions are called 'screens'. It is not to be + * confused with monitor. A screen is a collection of monitors, + * and the mapping from monitors to screens is a surjection. + * On hardware-level adjustment methods, such as Direct + * Rendering Manager, a partition is a graphics card. + */ + size_t partitions_available; + + /** + * The state in the native structure. + */ + libgamma_site_state_t* native; + + }; + + + + /** + * Partition state. + * + * Probably the majority of display server only one partition + * per site. However, X.org can, and traditional used to have + * on multi-headed environments, multiple partitions per site. + * In X.org partitions are called 'screens'. It is not to be + * confused with monitor. A screen is a collection of monitors, + * and the mapping from monitors to screens is a surjection. + * On hardware-level adjustment methods, such as Direct + * Rendering Manager, a partition is a graphics card. + */ + class Partition + { + public: + /** + * Constructor. + */ + Partition(); + + /** + * Constructor. + * + * @param site The site of the partition. + * @param partition The index of the partition. + */ + Partition(Site* site, size_t partition); + + /** + * Destructor. + */ + ~Partition(); + + /** + * Restore the gamma ramps all CRTC:s with a partition + * to the system settings. + */ + void restore(); + + + + /** + * The site this partition belongs to. + */ + Site* site; + + /** + * The index of the partition. + */ + size_t partition; + + /** + * The number of CRTC:s that are available under this + * partition. Note that the CRTC:s are not necessarily + * online. + */ + size_t crtcs_available; + + /** + * The state in the native structure. + */ + libgamma_partition_state_t* native; + + }; + + + + /** + * Cathode ray tube controller state. + * + * The CRTC controls the gamma ramps for the + * monitor that is plugged in to the connector + * that the CRTC belongs to. + */ + class CRTC + { + public: + /** + * Constructor. + */ + CRTC(); + + /** + * Constructor. + * + * @param partition The partition of the CRTC. + * @param crtc The index of the CRTC. + */ + CRTC(Partition* partition, size_t crtc); + + /** + * Destructor. + */ + ~CRTC(); + + /** + * Restore the gamma ramps for a CRTC to the system + * settings for that CRTC. + */ + void restore(); + + /** + * Read information about a CRTC. + * + * @param output Instance of a data structure to fill with the information about the CRTC. + * @param fields OR:ed identifiers for the information about the CRTC that should be read. + * @return Whether an error has occurred and is stored in a `*_error` field. + */ + bool information(CRTCInformation* output, int32_t fields); + + + + /** + * The partition this CRTC belongs to. + */ + Partition* partition; + + /** + * The index of the CRTC within its partition. + */ + size_t crtc; + + /** + * The state in the native structure. + */ + libgamma_crtc_state_t* native; + + }; + +#ifdef __GCC__ +# pragma GCC diagnostic pop +#endif } diff --git a/src/test.cc b/src/test.cc index b09017e..5118e8e 100644 --- a/src/test.cc +++ b/src/test.cc @@ -18,11 +18,18 @@ #include "libgamma.hh" #include <iostream> +#include <cstdlib> int main(void) { + libgamma::Site* site; + libgamma::Partition* partition; + libgamma::CRTC* crtc; + libgamma::CRTCInformation info; + std::string* str; + char* cstr; libgamma::perror("test", 0); libgamma::perror("test", 2); @@ -39,6 +46,56 @@ int main(void) delete str; std::cout << std::endl; + site = new libgamma::Site(LIBGAMMA_METHOD_X_RANDR, new std::string(":0")); + std::cout << site->partitions_available << std::endl; + partition = new libgamma::Partition(site, 0); + std::cout << partition->crtcs_available << std::endl; + crtc = new libgamma::CRTC(partition, 0); + std::cout << crtc->information(&info, ~0) << std::endl; + std::cout << std::endl; + + cstr = libgamma_behex_edid(info.edid, info.edid_length); + std::cout << "edid: " << cstr << std::endl; + free(cstr); + std::cout << "edid_length: " << info.edid_length << std::endl; + std::cout << "edid_error: " << info.edid_error << std::endl; + std::cout << "width_mm: " << info.width_mm << std::endl; + std::cout << "width_mm_error: " << info.width_mm_error << std::endl; + std::cout << "height_mm: " << info.height_mm << std::endl; + std::cout << "height_mm_error: " << info.height_mm_error << std::endl; + std::cout << "width_mm_edid: " << info.width_mm_edid << std::endl; + std::cout << "width_mm_edid_error: " << info.width_mm_edid_error << std::endl; + std::cout << "height_mm_edid: " << info.height_mm_edid << std::endl; + std::cout << "height_mm_edid_error: " << info.height_mm_edid_error << std::endl; + std::cout << "red_gamma_size: " << info.red_gamma_size << std::endl; + std::cout << "green_gamma_size: " << info.green_gamma_size << std::endl; + std::cout << "blue_gamma_size: " << info.blue_gamma_size << std::endl; + std::cout << "gamma_size_error: " << info.gamma_size_error << std::endl; + std::cout << "gamma_depth: " << info.gamma_depth << std::endl; + std::cout << "gamma_depth_error: " << info.gamma_depth_error << std::endl; + std::cout << "gamma_support: " << info.gamma_support << std::endl; + std::cout << "gamma_support_error: " << info.gamma_support_error << std::endl; + std::cout << "subpixel_order: " << info.subpixel_order << std::endl; + std::cout << "subpixel_order_error: " << info.subpixel_order_error << std::endl; + std::cout << "active: " << info.active << std::endl; + std::cout << "active_error: " << info.active_error << std::endl; + if (info.connector_name == nullptr) + std::cout << "connector_name: " << "(nullptr)" << std::endl; + else + std::cout << "connector_name: " << *(info.connector_name) << std::endl; + std::cout << "connector_name_error: " << info.connector_name_error << std::endl; + std::cout << "connector_type: " << info.connector_type << std::endl; + std::cout << "connector_type_error: " << info.connector_type_error << std::endl; + std::cout << "gamma_red: " << info.gamma_red << std::endl; + std::cout << "gamma_green: " << info.gamma_green << std::endl; + std::cout << "gamma_blue: " << info.gamma_blue << std::endl; + std::cout << "gamma_error: " << info.gamma_error << std::endl; + std::cout << std::endl; + + delete crtc; + delete partition; + delete site; + return 0; } |