diff options
Diffstat (limited to 'src/lib')
27 files changed, 0 insertions, 10637 deletions
diff --git a/src/lib/edid.c b/src/lib/edid.c deleted file mode 100644 index fc7b498..0000000 --- a/src/lib/edid.c +++ /dev/null @@ -1,105 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include "edid.h" - -#include "libgamma-method.h" -#include "libgamma-error.h" - -#include <stdint.h> - - -/* - * EDID structure revision 1.1: - * http://en.wikipedia.org/w/index.php?title=Extended_display_identification_data&oldid=46295569 - * - * EDID structure revision 1.3: - * http://en.wikipedia.org/wiki/Extended_display_identification_data - */ - - -/** - * Parse the EDID of a monitor - * - * @param this Instance of a data structure to fill with the information about the EDID; - * it must contain the EDID and its length - * @param fields OR:ed identifiers for the information about the EDID that should be parsed; - * fields that do not have to do with EDID are ignored - * @return Non-zero on error - */ -int -libgamma_parse_edid(libgamma_crtc_information_t *restrict this, int32_t fields) -{ -#define __test_version(edid, major, minor_min, minor_max)\ - ((edid)[18] == (major) && (minor_min) <= (edid)[19] && (edid)[19] <= (minor_max)) -#define __m(value)\ - (this->edid[index++] != (value)) - - int error = 0, checksum = 0; - size_t i, index = 0; - - /* If the length of the EDID is not 128 bytes, we know that it is not of EDID - structure revision 1.0–1.3, and thus we do not support it. Additionally - this make sure we do not do segmentation violation on the next test. */ - if (this->edid_length != 128) - error = LIBGAMMA_EDID_LENGTH_UNSUPPORTED; - /* Check that the magic number of that of the EDID structure. */ - else if (__m(0x00) || __m(0xFF) || __m(0xFF) || __m(0xFF) || __m(0xFF) || __m(0xFF) || __m(0xFF) || __m(0x00)) - error = LIBGAMMA_EDID_WRONG_MAGIC_NUMBER; - /* Check that EDID structure revision 1.1–1.3 is used, those are the only - version we support. EDID structure revision 1.3 is also by far the most - commonly use revision and it is currently the newest revision. We know - that parsing works for both revision 1.1 and revision 1.3, because of - this we assume it is also correct for revision 1.2. However, we are not - assuming this for revision 1.0 which appeared in August 1994 and was - replaced by revision 1.1 in April 1996. */ - else if (!__test_version(this->edid, 1, 1, 3)) - error = LIBGAMMA_EDID_REVISION_UNSUPPORTED; - - /* If we have encountered an error, report it for the fields that require - the EDID to be parsed. Note that it is not stored for the EDID field - itself because it is not considered an error just because we do not - support the used version. */ - this->width_mm_edid_error = this->height_mm_edid_error = this->gamma_error = error; - - /* Retrieve the size of the viewport. This is done even if it is not - requested because it is not worth it branch. */ - this->width_mm_edid = (size_t)this->edid[21] * 10; - this->height_mm_edid = (size_t)this->edid[22] * 10; - - /* Retrieve the monitor's gamma characteristics. */ - if ((fields & LIBGAMMA_CRTC_INFO_GAMMA) && !error) { - if (this->edid[23] == 0xFF) { - /* If the gamma charactistics is FFh (3,55) it should be interpreted as not specified. */ - this->gamma_error = LIBGAMMA_GAMMA_NOT_SPECIFIED; - } else { - this->gamma_blue = (float)((int)this->edid[23] + 100) / 100.f; - this->gamma_red = this->gamma_green = this->gamma_blue; - } - } - - /* If not error has occurred, calculate and test the checksum. - It is not considered an error that the gamma characteristics - is left unspecified in the EDID. */ - if (!error) { - for (i = 0; i < this->edid_length; i++) - checksum += (int)this->edid[i]; - } - /* The checksum should be zero. */ - if (checksum & 255) { - /* Store the error in all fields that require the EDID to be parsed, - as well as the EDID field itself. */ - error = LIBGAMMA_EDID_CHECKSUM_ERROR; - this->edid_error = this->width_mm_edid_error = this->height_mm_edid_error = error; - /* If the gamma characteristics is not specified, that is kept, - and the checksum error is augmented. */ - this->gamma_error = this->gamma_error == LIBGAMMA_GAMMA_NOT_SPECIFIED - ? LIBGAMMA_GAMMA_NOT_SPECIFIED_AND_EDID_CHECKSUM_ERROR : error; - } - - /* Return whether or not we encountered an error or if - the gamma characteristics was requested but is not - specified in the monitor's EDID. */ - return error | this->gamma_error; - -#undef __m -#undef __test_version -} diff --git a/src/lib/edid.h b/src/lib/edid.h deleted file mode 100644 index 51529ae..0000000 --- a/src/lib/edid.h +++ /dev/null @@ -1,22 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_EDID_H -#define LIBGAMMA_EDID_H - -#include "libgamma-method.h" - -#include <stdint.h> - - -/** - * Parse the EDID of a monitor - * - * @param this Instance of a data structure to fill with the information about the EDID; - * it must contain the EDID and its length - * @param fields OR:ed identifiers for the information about the EDID that should be parsed; - * fields that do not have to do with EDID are ignored - * @return Non-zero on error - */ -int libgamma_parse_edid(libgamma_crtc_information_t *restrict, int32_t); - - -#endif diff --git a/src/lib/fake-quartz-cg.c b/src/lib/fake-quartz-cg.c deleted file mode 100644 index 75f1fb2..0000000 --- a/src/lib/fake-quartz-cg.c +++ /dev/null @@ -1,447 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef FAKE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS -# error Compiling fake-quartz-cg.c without FAKE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS -#endif - -/* This file very sloppily translates Mac OS X Quartz calls to X RandR calls. - * It should by no means be used, without additional modification, as a - * part of a compatibility layer. The purpose of this file is only to make - * it possible to test for logical errors in Max OS X specific code on - * a GNU/Linux system under X. */ - - -#include "fake-quartz-cg.h" - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - - -#ifndef HAVE_LIBGAMMA_METHOD_X_RANDR -/* Use dummy translation */ - - -/** - * Get a list of all online displays on the system - * - * @param max_size The number of elements allocated for `displays_out` - * @param displays_out List ot fill with the ID for each online display on the system - * @param count_out Output parameter for the number of elements stored in `displays_out` - * if `displays_out` is too small to fit all display ID:s, - * `*count_out` will be `max_size` - * @return `kCGErrorSuccess` on success, and error number of failure - */ -CGError -CGGetOnlineDisplayList(uint32_t max_size, CGDirectDisplayID *restrict displays_out, uint32_t *restrict count_out) -{ - /* Pretend that we have 2 CRTC:s */ - uint32_t i; - for (i = 0; i < max_size && i < 2; i++) - displays_out[i] = (CGDirectDisplayID)i; - *count_out = i; - return kCGErrorSuccess; -} - - -/** - * Set the gamma ramps for a display - * - * @param display The ID of the display - * @param gamma_size The number of stops in gamma ramps - * @param red The gamma ramp for the red channel - * @param green The gamma ramp for the green channel - * @param blue The gamma ramp for the blue channel - * @return `kCGErrorSuccess` on success, and error number of failure - */ -CGError -CGSetDisplayTransferByTable(CGDirectDisplayID display, uint32_t gamma_size, const CGGammaValue *red, - const CGGammaValue *green, const CGGammaValue *blue) -{ - (void) display; - (void) red; - (void) green; - (void) blue; - - /* We pretend that our gamma ramps are of size 256 */ - if (gamma_size != 256) { - fprintf(stderr, "Gamma size should be 256\n"); - abort(); - } - return kCGErrorSuccess; -} - - -/** - * Get the current gamma ramps for a display - * - * @param display The ID of the display - * @param gamma_size The number of stops you have allocated for the gamma ramps - * @param red Table allocated for the gamma ramp for the red channel - * @param green Table allocated for the gamma ramp for the green channel - * @param blue Table allocated for the gamma ramp for the blue channel - * @param gamma_size_out Output parameter for the actual number of stops in the gamma ramps - * @return `kCGErrorSuccess` on success, and error number of failure - */ -CGError -CGGetDisplayTransferByTable(CGDirectDisplayID display, uint32_t gamma_size, CGGammaValue *restrict red, - CGGammaValue *restrict green, CGGammaValue *restrict blue, uint32_t *restrict gamma_size_out) -{ - long i; - - (void) display; - - /* We pretend that our gamma ramps are of size 256 */ - if (gamma_size != 256) { - fprintf(stderr, "Gamma size should be 256\n"); - abort(); - } - - /* We pretend that our gamma ramps are of size 256 */ - *gamma_size_out = 256; - - /* Pretend that our gamma ramps are identity mappings */ - for (i = 0; i < 256; i++) - red[i] = green[i] = blue[i] = (CGGammaValue)i / 255; - - return kCGErrorSuccess; -} - - -/** - * Restore each display's gamma ramps to the settings in ColorSync - */ -void -CGDisplayRestoreColorSyncSettings(void) -{ - /* Do nothing */ -} - - -/** - * Get the number of stops in the gamma ramps for a display - * - * @param display The ID of the display - * @return The number of stops in the gamma ramps - */ -uint32_t -CGDisplayGammaTableCapacity(CGDirectDisplayID display) -{ - /* We pretend that our gamma ramps are of size 256 */ - (void) display; - return 256; -} - - -/** - * Release resources used by the backend - */ -void -close_fake_quartz(void) -{ - /* Do nothing */ -} - - -#else -/* Use translation to X RandR */ - - -#include <xcb/xcb.h> -#include <xcb/randr.h> - - - -/** - * Connection to the X RandR display - */ -static xcb_connection_t *restrict connection = NULL; - -/** - * Resouces for the screen - * - * We only have one screen, again this - * is a very sloppy compatibility layer - */ -static xcb_randr_get_screen_resources_current_reply_t *restrict res_reply = NULL; - -/** - * The number of available CRTC:s - */ -static uint32_t crtc_count = 0; - -/** - * List of X RandR CRTC:s - */ -static xcb_randr_crtc_t *restrict crtcs = NULL; - -/** - * The original gamma ramps, used to emulate gamma ramp restoration to system settings - */ -static uint16_t *restrict original_ramps = NULL; - - - -/* xcb violates the rule to never return struct:s */ -#ifdef __GNUC__ -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Waggregate-return" -#endif - - -/** - * Get a list of all online displays on the system - * - * @param max_size The number of elements allocated for `displays_out` - * @param displays_out List ot fill with the ID for each online display on the system - * @param count_out Output parameter for the number of elements stored in `displays_out`, - * if `displays_out` is too small to fit all display ID:s, - * `*count_out` will be `max_size` - * @return `kCGErrorSuccess` on success, and error number of failure - */ -CGError -CGGetOnlineDisplayList(uint32_t max_size, CGDirectDisplayID *restrict displays_out, uint32_t *restrict count_out) -{ - xcb_generic_error_t *error; - xcb_screen_iterator_t iter; - xcb_randr_get_screen_resources_current_cookie_t res_cookie; - xcb_randr_get_crtc_gamma_cookie_t gamma_cookie; - xcb_randr_get_crtc_gamma_reply_t *restrict gamma_reply; - uint32_t i; - - /* Connect to the display and get screen data if not already done so */ - if (!connection) { - /* Connect to the display */ - connection = xcb_connect(NULL, NULL); - /* Get the first screen */ - iter = xcb_setup_roots_iterator(xcb_get_setup(connection)); - /* Get the resources of the screen */ - res_cookie = xcb_randr_get_screen_resources_current(connection, iter.data->root); - res_reply = xcb_randr_get_screen_resources_current_reply(connection, res_cookie, &error); - if (error) { - fprintf(stderr, "Failed to open X connection\n"); - xcb_disconnect(connection); - crtc_count = 0; - return ~kCGErrorSuccess; - } - - /* Get the number of CRTC:s */ - crtc_count = (uint32_t)(res_reply->num_crtcs); - /* Get the CRTC ID:s */ - crtcs = xcb_randr_get_screen_resources_current_crtcs(res_reply); - - /* Allocate memory where we store the gamma ramps as - * they looked when this adjustment method was first - * used. This is used to emulate the functionality of - * `CGDisplayRestoreColorSyncSettings` which restore the - * all gamma ramps on the system to the system settnigs. - */ - original_ramps = malloc(crtc_count * 3 * 256 * sizeof(uint16_t)); - if (!original_ramps) { - perror("malloc"); - xcb_disconnect(connection); - crtc_count = 0; - return ~kCGErrorSuccess; - } - - /* Fill the gamma ramps we just allocated */ - for (i = 0; i < crtc_count; i++) { - /* Read current gamma ramps */ - gamma_cookie = xcb_randr_get_crtc_gamma(connection, crtcs[i]); - gamma_reply = xcb_randr_get_crtc_gamma_reply(connection, gamma_cookie, &error); - if (error) { - fprintf(stderr, "Failed to read gamma ramps.\n"); - xcb_disconnect(connection); - crtc_count = 0; - return ~kCGErrorSuccess; - } - - /* Copy over the gamma ramps to the memory area we have allocated */ -#define __DEST(C) original_ramps + (C + 3 * i) * 256 -#define __SRC(C) xcb_randr_get_crtc_gamma_##C(gamma_reply) - memcpy(__DEST(0), __SRC(red), 256 * sizeof(uint16_t)); - memcpy(__DEST(1), __SRC(green), 256 * sizeof(uint16_t)); - memcpy(__DEST(2), __SRC(blue), 256 * sizeof(uint16_t)); -#undef __SRC -#undef __DEST - - /* Release resouces */ - free(gamma_reply); - } - } - - /* Return CRTC ID:s */ - for (i = 0; i < max_size && i < crtc_count; i++) - displays_out[i] = (CGDirectDisplayID)i; - - /* Return the number of CRTC ID:s we returned */ - *count_out = i; - return kCGErrorSuccess; -} - - -/** - * Set the gamma ramps for a display - * - * @param display The ID of the display - * @param gamma_size The number of stops in gamma ramps - * @param red The gamma ramp for the red channel - * @param green The gamma ramp for the green channel - * @param blue The gamma ramp for the blue channel - * @return `kCGErrorSuccess` on success, and error number of failure - */ -CGError -CGSetDisplayTransferByTable(CGDirectDisplayID display, uint32_t gamma_size, const CGGammaValue *red, - const CGGammaValue *green, const CGGammaValue *blue) -{ - xcb_void_cookie_t gamma_cookie; - uint16_t r_int[256], g_int[256], b_int[256]; - int32_t v; - long i; - - /* This is a sloppy compatibility layer that assumes the gamma ramp size is 256 */ - if (gamma_size != 256) { - fprintf(stderr, "Gamma size should be 256.\n"); - abort(); - } - - /* Translate the gamma ramps from float (CoreGraphics) to 16-bit unsigned integer (X RandR) */ - for (i = 0; i < 256; i++) { - /* Red channel */ - v = (int32_t)(red[i] * UINT16_MAX); - r_int[i] = (uint16_t)(v < 0 ? 0 : v > UINT16_MAX ? UINT16_MAX : v); - - /* Green channel */ - v = (int32_t)(green[i] * UINT16_MAX); - g_int[i] = (uint16_t)(v < 0 ? 0 : v > UINT16_MAX ? UINT16_MAX : v); - - /* Blue channel */ - v = (int32_t)(blue[i] * UINT16_MAX); - b_int[i] = (uint16_t)(v < 0 ? 0 : v > UINT16_MAX ? UINT16_MAX : v); - } - - /* Apply gamma ramps */ - gamma_cookie = xcb_randr_set_crtc_gamma_checked(connection, crtcs[display], (uint16_t)gamma_size, r_int, g_int, b_int); - /* Check for errors */ - return !xcb_request_check(connection, gamma_cookie) ? kCGErrorSuccess : ~kCGErrorSuccess; -} - - -/** - * Get the current gamma ramps for a display - * - * @param display The ID of the display - * @param gamma_size The number of stops you have allocated for the gamma ramps - * @param red Table allocated for the gamma ramp for the red channel - * @param green Table allocated for the gamma ramp for the green channel - * @param blue Table allocated for the gamma ramp for the blue channel - * @param gamma_size_out Output parameter for the actual number of stops in the gamma ramps - * @return `kCGErrorSuccess` on success, and error number of failure - */ -CGError -CGGetDisplayTransferByTable(CGDirectDisplayID display, uint32_t gamma_size, CGGammaValue *restrict red, - CGGammaValue *restrict green, CGGammaValue *restrict blue, uint32_t *restrict gamma_size_out) -{ - xcb_randr_get_crtc_gamma_cookie_t gamma_cookie; - xcb_randr_get_crtc_gamma_reply_t *restrict gamma_reply; - xcb_generic_error_t *error; - uint16_t *restrict r_int; - uint16_t *restrict g_int; - uint16_t *restrict b_int; - long i; - - /* This is a sloppy compatibility layer that assumes the gamma ramp size is 256 */ - if (gamma_size != 256) { - fprintf(stderr, "Gamma size should be 256\n"); - abort(); - } - - /* The gamma ramp size should be returned to the caller */ - *gamma_size_out = 256; - - /* Read current gamma ramps */ - gamma_cookie = xcb_randr_get_crtc_gamma(connection, crtcs[display]); - gamma_reply = xcb_randr_get_crtc_gamma_reply(connection, gamma_cookie, &error); - if (error) { - fprintf(stderr, "Failed to write gamma ramps\n"); - return ~kCGErrorSuccess; - } - - /* Get gamma ramp values */ - r_int = xcb_randr_get_crtc_gamma_red(gamma_reply); - g_int = xcb_randr_get_crtc_gamma_green(gamma_reply); - b_int = xcb_randr_get_crtc_gamma_blue(gamma_reply); - - /* Translate gamma ramps to float format, that is what CoreGraphics uses */ - for (i = 0; i < 256; i++) { - red[i] = (CGGammaValue)r_int[i] / UINT16_MAX; - green[i] = (CGGammaValue)g_int[i] / UINT16_MAX; - blue[i] = (CGGammaValue)b_int[i] / UINT16_MAX; - } - - free(gamma_reply); - return kCGErrorSuccess; -} - - -/** - * Restore each display's gamma ramps to the settings in ColorSync - */ -void -CGDisplayRestoreColorSyncSettings(void) -{ - xcb_generic_error_t *error; - xcb_void_cookie_t gamma_cookie; - uint32_t i; - - /* Restore the gamma ramps on each monitor to - * the ramps that we used when we started */ - for (i = 0; i < crtc_count; i++) { - /* We assume that our gamma ramps are of the size - * 256 (this is a sloppy compatibility layer) */ - gamma_cookie = xcb_randr_set_crtc_gamma_checked(connection, crtcs[i], 256, - &original_ramps[(0 + 3 * i) * 256], - &original_ramps[(1 + 3 * i) * 256], - &original_ramps[(2 + 3 * i) * 256]); - if ((error = xcb_request_check(connection, gamma_cookie))) - fprintf(stderr, "Quartz gamma reset emulation with RandR returned %i\n", error->error_code); - } -} - - -/** - * Get the number of stops in the gamma ramps for a display - * - * @param display The ID of the display - * @return The number of stops in the gamma ramps - */ -uint32_t -CGDisplayGammaTableCapacity(CGDirectDisplayID display) -{ - /* We assume that our gamma ramps are of the size - * 256 (this is a sloppy compatibility layer) */ - (void) display; - return 256; -} - - -#ifdef __GNUC__ -# pragma GCC diagnostic pop -#endif - - -/** - * Release resources used by the backend - */ -void close_fake_quartz_cg(void) -{ - free(res_reply); - res_reply = NULL; - if (connection) { - xcb_disconnect(connection); - connection = NULL; - } - free(original_ramps); - original_ramps = NULL; -} - -#endif diff --git a/src/lib/fake-quartz-cg.h b/src/lib/fake-quartz-cg.h deleted file mode 100644 index 361a73c..0000000 --- a/src/lib/fake-quartz-cg.h +++ /dev/null @@ -1,120 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_FAKE_QUARTZ_CG_H -#define LIBGAMMA_FAKE_QUARTZ_CG_H - -#ifndef FAKE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS -# error Including fake-quartz-cg.h without FAKE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS -#endif - - -#ifndef __GNUC__ -# define __attribute__(x) -#endif - - -/* This header file contains some capabilities of - * <CoreGraphics/CGDirectDisplay.h> and <CoreGraphics/CGError.h>, - * and can be used modify gamma ramps without Mac OS X and Quartz - * but with its API. - * - * The content of this file is based on the documentation found on: - * - * https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/Quartz_Services_Ref/Reference/reference.html - * - * https://developer.apple.com/library/mac/documentation/CoreGraphics/Reference/CoreGraphicsConstantsRef/Reference/reference.html#//apple_ref/c/tdef/CGError */ - - -#include <stdint.h> - - -/** - * Numerical `typedef` for errors that occur in CoreGraphics calls - */ -typedef int32_t CGError; - -/** - * The call was successful - */ -#define kCGErrorSuccess 0 - -/** - * The data type that is used for the values in the gamma ramps - */ -typedef float CGGammaValue; - -/** - * The data type for display ID:s - */ -typedef uint32_t CGDirectDisplayID; - - -/** - * Get a list of all online displays on the system - * - * @param max_size The number of elements allocated for `displays_out` - * @param displays_out List ot fill with the ID for each online display on the system - * @param count_out Output parameter for the number of elements stored in `displays_out` - * if `displays_out` is too small to fit all display ID:s, - * `*count_out` will be `max_size` - * @return `kCGErrorSuccess` on success, and error number of failure - */ -CGError CGGetOnlineDisplayList(uint32_t, CGDirectDisplayID *restrict, uint32_t *restrict); - -/** - * Set the gamma ramps for a display - * - * @param display The ID of the display - * @param gamma_size The number of stops in gamma ramps - * @param red The gamma ramp for the red channel - * @param green The gamma ramp for the green channel - * @param blue The gamma ramp for the blue channel - * @return `kCGErrorSuccess` on success, and error number of failure - */ -CGError CGSetDisplayTransferByTable(CGDirectDisplayID, uint32_t, const CGGammaValue *, const CGGammaValue *, const CGGammaValue *); - -/** - * Get the current gamma ramps for a display - * - * @param display The ID of the display - * @param gamma_size The number of stops you have allocated for the gamma ramps - * @param red Table allocated for the gamma ramp for the red channel - * @param green Table allocated for the gamma ramp for the green channel - * @param blue Table allocated for the gamma ramp for the blue channel - * @param gamma_size_out Output parameter for the actual number of stops in the gamma ramps - * @return `kCGErrorSuccess` on success, and error number of failure - */ -CGError CGGetDisplayTransferByTable(CGDirectDisplayID, uint32_t, CGGammaValue *restrict, - CGGammaValue *restrict, CGGammaValue *restrict, uint32_t *restrict); - -/** - * Restore each display's gamma ramps to the settings in ColorSync - */ -void CGDisplayRestoreColorSyncSettings(void); - -/** - * Get the number of stops in the gamma ramps for a display - * - * @param display The ID of the display - * @return The number of stops in the gamma ramps - */ -uint32_t CGDisplayGammaTableCapacity(CGDirectDisplayID) __attribute__((const)); - - -/* The follow part most only be used when this module is used, - * it cannot be used when the real CoreGraphics is used. - * CoreGraphics does not have this function, it is added so - * that there is a way to cleanly close the X connection - * and free resources needed by this module. */ - -/** - * Release resources used by the backend - */ -void close_fake_quartz_cg(void); - - - -#ifndef __GNUC__ -# undef __attribute__ -#endif - -#endif diff --git a/src/lib/fake-w32-gdi.c b/src/lib/fake-w32-gdi.c deleted file mode 100644 index 68847ca..0000000 --- a/src/lib/fake-w32-gdi.c +++ /dev/null @@ -1,479 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef HAVE_LIBGAMMA_METHOD_W32_GDI -# error Compiling fake-w32-gdi.c without FAKE_LIBGAMMA_METHOD_W32_GDI -#endif - -/* This file very sloppily translates Windows GDI calls to X RandR calls. - * It should by no means be used, without additional modification, as a - * part of a compatibility layer. The purpose of this file is only to make - * it possible to test for logical errors in Windows specific code on - * a GNU/Linux system under X. */ - - -#include "fake-w32-gdi.h" - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - - -#ifndef HAVE_LIBGAMMA_METHOD_X_RANDR -/* Use dummy translation */ - - - -/** - * Get the device context for a window or the entire screen - * - * @param hWnd The windows, `NULL` for the entire screen - * @return The device context - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144871(v=vs.85).aspx - */ -HDC -GetDC(HWND hWnd) -{ - /* Just a non-NULL value */ - (void) hWnd; - return (HDC *)16; -} - - -/** - * Free a device context - * - * @param hWnd The window whose device context is `hDC`, `NULL` for the entire screen - * @param hDC The device context to free - * @return Whether the call was successful - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd162920(v=vs.85).aspx - */ -int -ReleaseDC(HWND hWnd, HDC hDC) -{ - /* Always successful */ - (void) hWnd; - (void) hDC; - return 1; -} - - - -/** - * Get information (capabilities) for a device context - * - * @param hDC The device context - * @param nIndex The information to retrieve, may be `COLORMGMTCAPS` - * @return The details of the queried information, can return `CM_GAMMA_RAMP` - * if `nIndex` is `COLORMGMTCAPS` - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877(v=vs.85).aspx - */ -int -GetDeviceCaps(HDC hDC, int nIndex) -{ - /* We have gamma ramps if the user asks for colour management capabilities */ - (void) hDC; - return CM_GAMMA_RAMP + nIndex - COLORMGMTCAPS; -} - - -/** - * Set the gamma ramps for a device - * - * @param hDC The device context - * @param lpRamp The gamma ramps joined in the order: red, green, blue - * This is a `WORD*` casted to `void*` - * @return Whether the call was successful - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd372194(v=vs.85).aspx - */ -BOOL -SetDeviceGammaRamp(HDC hDC, LPVOID restrict lpRamp) -{ - /* Always successful */ - (void) hDC; - (void) lpRamp; - return TRUE; -} - - -/** - * Get the current gamma ramps for a device - * - * @param hDC The device context - * @param lpRamp The output for the gamma ramps joined in the order: red, green, blue - * This is a `WORD *` casted to `void *` - * @return Whether the call was successful - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd316946(v=vs.85).aspx - */ -BOOL -GetDeviceGammaRamp(HDC hDC, LPVOID restrict lpRamp) -{ - /* Always successful */ - (void) hDC; - (void) lpRamp; - return TRUE; -} - - - -/** - * Get the context for a device - * - * @param lpszDriver The driver or a display device, use "DISPLAY" if you want a display - * @param lpszDevice The name of the device. If you want a display use can use the member - * name `DeviceName` in the third parameter, an output parameter, of - * `EnumDisplayDevices` - * @param lpszOutput We will always use `NULL` here - * @param lpInitData We will always use `NULL` here; this should actually by a `const DEVMODE *` - * @return The context for the device - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183490(v=vs.85).aspx - */ -HDC -CreateDC(LPCTSTR restrict lpszDriver, LPCTSTR restrict lpszDevice, LPCTSTR restrict lpszOutput, const void *restrict lpInitData) -{ - /* `NULL` if not asking for a CRTC, otherwise a non-NULL value */ - (void) lpszOutput; - (void) lpInitData; - (void) lpszDevice; - return !strcmp(lpszDriver, "DISPLAY") ? (HDC *)16 : NULL; -} - - -/** - * Get a display device by its name or index - * - * @param lpDevice The name of the device, use `NULL` to base the call on `iDevNum` instead - * @param iDevNum The index of the device - * @param lpDisplayDevice Output for the found device - * @param dwFlags Flags, we will always use zero - * @return Whether the call was successful; zero is also returned if `iDevNum` - * is greater than the largest device index on the system - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd162609(v=vs.85).aspx - */ -BOOL -EnumDisplayDevices(LPCTSTR lpDevice, restrict DWORD iDevNum, PDISPLAY_DEVICE restrict lpDisplayDevice, DWORD dwFlags) -{ - (void) dwFlags; - /* Check correctness of `lpDevice` */ - if (lpDevice) { - fprintf(stderr, "lpDevice (argument 1) for EnumDisplayDevices should be NULL\n"); - abort(); - return FALSE; - } - /* Pretend that we have two CRTC:s */ - if (iDevNum >= 2) - return FALSE; - /* Check correctness of `lpDisplayDevice` */ - if (lpDisplayDevice->cb != sizeof(DISPLAY_DEVICE)) { - fprintf(stderr, "lpDisplayDevice->cb for EnumDisplayDevices is not sizeof(DISPLAY_DEVICE)\n"); - abort(); - return FALSE; - } - /* Store an arbitrary name for the monitor */ - strcmp(lpDisplayDevice->DeviceName, "some monitor"); - /* The connector is always enabled */ - lpDisplayDevice->StateFlags = DISPLAY_DEVICE_ACTIVE; - return TRUE; -} - - -#else -/* Use translation to X RandR */ - - -#include <xcb/xcb.h> -#include <xcb/randr.h> - - -/** - * The gamma ramp size that devices will - * always have in Windows GDI - */ -#define GAMMA_RAMP_SIZE 256 - - -/** - * Connection to the X RandR display - */ -static xcb_connection_t *restrict connection = NULL; - -/** - * Resouces for the screen - * - * We only have one screen, again this - * is a very sloppy compatibility layer - */ -static xcb_randr_get_screen_resources_current_reply_t *restrict res_reply = NULL; - -/** - * The number of available CRTC:s, -1 if not known yet - */ -static ssize_t crtc_count = -1; - -/** - * List of X RandR CRTC:s - */ -static xcb_randr_crtc_t *restrict crtcs = NULL; - -/** - * The number of opened CRTC:s - */ -static size_t dc_count = 0; - - - -/** - * Get the device context for a window or the entire screen - * - * @param hWnd The windows, `NULL` for the entire screen - * @return The device context - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144871(v=vs.85).aspx - */ -HDC -GetDC(HWND hWnd) -{ - /* Return the primary CRTC */ - (void) hWnd; - return CreateDC(TEXT("DISPLAY"), "0", NULL, NULL); -} - - -/** - * Free a device context - * - * @param hWnd The window whose device context is `hDC`, `NULL` for the entire screen - * @param hDC The device context to free - * @return Whether the call was successful - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd162920(v=vs.85).aspx - */ -int -ReleaseDC(HWND hWnd, HDC hDC) -{ - /* Disconnect from the RandR display when all monitors have been closed */ - (void) hWnd; - (void) hDC; - if (!dc_count--) { - if (connection) { - xcb_disconnect(connection); - connection = NULL; - } - free(res_reply); - res_reply = NULL; - } - return 1; -} - - - -/** - * Get information (capabilities) for a device context - * - * @param hDC The device context - * @param nIndex The information to retrieve, may be `COLORMGMTCAPS` - * @return The details of the queried information, can return `CM_GAMMA_RAMP` - * if `nIndex` is `COLORMGMTCAPS` - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877(v=vs.85).aspx - */ -int -GetDeviceCaps(HDC hDC, int nIndex) -{ - /* We have gamma ramps if the user asks for colour management capabilities */ - (void) hDC; - return CM_GAMMA_RAMP + nIndex - COLORMGMTCAPS; -} - - -/* xcb violates the rule to never return `struct`:s */ -#ifdef __GNUC__ -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Waggregate-return" -#endif - - -/** - * Set the gamma ramps for a device - * - * @param hDC The device context - * @param lpRamp The gamma ramps joined in the order: red, green, blue - * This is a `WORD *` casted to `void *` - * @return Whether the call was successful - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd372194(v=vs.85).aspx - */ -BOOL -SetDeviceGammaRamp(HDC hDC, LPVOID restrict lpRamp) -{ - /* We assume that our gamma ramps are of the same size - * as used by Windows GDI (we are so sloppy) */ - xcb_void_cookie_t gamma_cookie = - xcb_randr_set_crtc_gamma_checked(connection, *(xcb_randr_crtc_t *)hDC, GAMMA_RAMP_SIZE, - &((uint16_t *)lpRamp)[0 * GAMMA_RAMP_SIZE], - &((uint16_t *)lpRamp)[1 * GAMMA_RAMP_SIZE], - &((uint16_t *)lpRamp)[2 * GAMMA_RAMP_SIZE)]; - xcb_generic_error_t *error = xcb_request_check(connection, gamma_cookie); - return !error ? TRUE : FALSE; -} - - -/** - * Get the current gamma ramps for a device - * - * @param hDC The device context - * @param lpRamp The output for the gamma ramps joined in the order: red, green, blue - * This is a `WORD *` casted to `void *` - * @return Whether the call was successful - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd316946(v=vs.85).aspx - */ -BOOL -GetDeviceGammaRamp(HDC hDC, LPVOID restrict lpRamp) -{ - xcb_randr_get_crtc_gamma_cookie_t gamma_cookie; - xcb_randr_get_crtc_gamma_reply_t *restrict gamma_reply; - xcb_generic_error_t *error; - - /* Read current gamma ramps */ - gamma_cookie = xcb_randr_get_crtc_gamma(connection, *(xcb_randr_crtc_t *)hDC); - gamma_reply = xcb_randr_get_crtc_gamma_reply(connection, gamma_cookie, &error); - if (error) - return FALSE; - -#define DEST_RAMP(I) (&((uint16_t *)lpRamp)[(I) * GAMMA_RAMP_SIZE]) -#define SRC_RAMP(C) (xcb_randr_get_crtc_gamma_##C(gamma_reply)) - - /* Copy the ramps into the output parameter (coalesced) */ - memcpy(DEST_RAMP(0), SRC_RAMP(red), GAMMA_RAMP_SIZE * sizeof(uint16_t)); - memcpy(DEST_RAMP(1), SRC_RAMP(green), GAMMA_RAMP_SIZE * sizeof(uint16_t)); - memcpy(DEST_RAMP(2), SRC_RAMP(blue), GAMMA_RAMP_SIZE * sizeof(uint16_t)); - -#undef SRC_RAMP -#undef DEST_RAMP - - free(gamma_reply); - return TRUE; -} - - - -/** - * Get the context for a device - * - * @param lpszDriver The driver or a display device, use "DISPLAY" if you want a display - * @param lpszDevice The name of the device. If you want a display use can use the member - * name `DeviceName` in the third parameter, an output parameter, of - * `EnumDisplayDevices` - * @param lpszOutput We will always use `NULL` here - * @param lpInitData We will always use `NULL` here; this should actually by a `const DEVMODE *` - * @return The context for the device - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183490(v=vs.85).aspx - */ -HDC -CreateDC(LPCTSTR restrict lpszDriver, LPCTSTR restrict lpszDevice, LPCTSTR restrict lpszOutput, const void *restrict lpInitData) -{ - int crtc_index = atoi(lpszDevice); - xcb_generic_error_t *error; - xcb_screen_iterator_t iter; - xcb_randr_get_screen_resources_current_cookie_t res_cookie; - - (void) lpszOutput; - (void) lpInitData; - - /* Check correctness of input */ - if (strcmp(lpszDriver, "DISPLAY")) - return NULL; - - /* Connect to the display and get screen data if not already done so */ - if (!dc_count) { - /* Connect to the display */ - connection = xcb_connect(NULL, NULL); - /* Get the first screen */ - iter = xcb_setup_roots_iterator(xcb_get_setup(connection)); - /* Get the resources of the screen */ - res_cookie = xcb_randr_get_screen_resources_current(connection, iter.data->root); - res_reply = xcb_randr_get_screen_resources_current_reply(connection, res_cookie, &error); - if (error) { - fprintf(stderr, "Failed to open X connection\n"); - xcb_disconnect(connection); - crtc_count = -1; - return NULL; - } - - /* Get the number of CRTC:s */ - crtc_count = res_reply->num_crtcs; - /* Get the CRTC ID:s */ - crtcs = xcb_randr_get_screen_resources_current_crtcs(res_reply); - } - - /* Was the index too high */ - if (crtc_index >= crtc_count) { - /* Disconnect and release resouces and mark that - we do not know the number of available CRTC:s - if we have not opened any monitors yet */ - if (!dc_count) { - xcb_disconnect(connection); - free(res_reply); - res_reply = NULL; - crtc_count = -1; - } - return NULL; - } - - /* We have opened a new CRTC */ - dc_count++; - /* Return the ID of the CRTC */ - return &crtcs[crtc_index]; -} - - -#ifdef __GNUC__ -# pragma GCC diagnostic pop -#endif - - -/** - * Get a display device by its name or index - * - * @param lpDevice The name of the device, use `NULL` to base the call on `iDevNum` instead - * @param iDevNum The index of the device - * @param lpDisplayDevice Output for the found device - * @param dwFlags Flags, we will always use zero - * @return Whether the call was successful; zero is also returned if `iDevNum` - * is greater than the largest device index on the system - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd162609(v=vs.85).aspx - */ -BOOL -EnumDisplayDevices(LPCTSTR restrict lpDevice, DWORD iDevNum, PDISPLAY_DEVICE restrict lpDisplayDevice, DWORD dwFlags) -{ - size_t count = (size_t)crtc_count; - - (void) dwFlags; - - /* Check the correctness of the input */ - if (lpDevice) { - fprintf(stderr, "lpDevice (argument 1) for EnumDisplayDevices should be NULL\n"); - abort(); - return FALSE; - } - /* Do we know how many CRTC:s are available? */ - if (crtc_count < 0) { - /* If not open the first CRTC so that will be figured out */ - if (!GetDC(NULL)) - return FALSE; - count = (size_t)crtc_count; - /* Close the primary monitor that we just closed */ - ReleaseDC(NULL, NULL); - } - /* Check that the request CRTC exists */ - if (iDevNum >= count) - return FALSE; - /* Check the correctness of the input */ - if (lpDisplayDevice->cb != sizeof(DISPLAY_DEVICE)) { - fprintf(stderr, "lpDisplayDevice->cb for EnumDisplayDevices is not sizeof(DISPLAY_DEVICE)\n"); - abort(); - return FALSE; - } - /* Store name for the CRTC */ - sprintf(lpDisplayDevice->DeviceName, "%i", iDevNum); - /* The connector that the CRTC belongs to is enabled */ - lpDisplayDevice->StateFlags = DISPLAY_DEVICE_ACTIVE; - return TRUE; -} - - -#endif diff --git a/src/lib/fake-w32-gdi.h b/src/lib/fake-w32-gdi.h deleted file mode 100644 index bcc2fa9..0000000 --- a/src/lib/fake-w32-gdi.h +++ /dev/null @@ -1,248 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_FAKE_W32_GDI_H -#define LIBGAMMA_FAKE_W32_GDI_H - -#ifndef FAKE_LIBGAMMA_METHOD_W32_GDI -# error Including fake-w32-gdi.h without FAKE_LIBGAMMA_METHOD_W32_GDI -#endif - - -#ifndef __GNUC__ -# define __attribute__(x) -#endif - -#include <stdint.h> - - -/** - * One of the Windows `typedef`:s for unsigned 16-bit integer - * This is the one used in these functions - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#WORD - */ -typedef uint16_t WORD; - -/** - * One of the Windows `typedef`:s for unsigned 32-bit integer - * This is the one used in these functions - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#DWORD - */ -typedef uint32_t DWORD; - -/** - * One of the Windows `typedef` for booleanic values, the `int` variant - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#BOOL - */ -typedef int BOOL; - -/** - * Windows `typedef` for a device context (the 'h' stands for 'handle') - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#HDC - */ -typedef void *HDC; - -/** - * Windows `typedef` for a window (the 'h' stands for 'handle') - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#HWND - */ -typedef void *HWND; - -/** - * One of the Windows `typedef`:s for `void *`, a generic pointer - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#LPVOID - */ -typedef void *LPVOID; - -/** - * A silly Windows `typedef` for a `const char *`, a constant string - * - * It is also defined as NUL-terminated, but so are all C-strings - * and it makes no difference. However if `UNICODE` were to be defined - * it would be `const wchar_t *`. - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#LPCTSTR - */ -typedef const char *LPCTSTR; - -/** - * Even more silly. A Windows `typedef` for either `wchar_t` or - * `char`, depending on whether `UNICODE` is defined (we will - * assume `UNICODE` is not definied because that is just silly) - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#TCHAR - */ -typedef char TCHAR; - -/** - * Apperently we need `TRUE` to be defined to understand code, - * at least in Windows... - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#BOOL - */ -#define TRUE 1 - -/** - * Apperently we need `FALSE` to be defined to understand code, - * at least in Windows... - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#BOOL - */ -#define FALSE 0 - - - -/** - * Get the device context for a window or the entire screen - * - * @param hWnd The window, `NULL` for the entire screen - * @return The device context - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144871(v=vs.85).aspx - */ -HDC GetDC(HWND); - -/** - * Free a device context - * - * @param hWnd The window whose device context is `hDC`, `NULL` for the entire screen - * @param hDC The device context to free - * @return Whether the call was successful - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd162920(v=vs.85).aspx - */ -int ReleaseDC(HWND, HDC); - - -/** - * Get information (capabilities) for a device context - * - * @param hDC The device context - * @param nIndex The information to retrieve, may be `COLORMGMTCAPS` - * @return The details of the queried information, can return `CM_GAMMA_RAMP` - * if `nIndex` is `COLORMGMTCAPS` - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877(v=vs.85).aspx - */ -int GetDeviceCaps(HDC, int) __attribute__((const)); - -/** - * Colour management capabilities - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877(v=vs.85).aspx - */ -#define COLORMGMTCAPS 1 - -/** - * Colour management capabilities: has gamma ramps - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877(v=vs.85).aspx - */ -#define CM_GAMMA_RAMP 1 - - -/** - * Set the gamma ramps for a device - * - * @param hDC The device context - * @param lpRamp The gamma ramps joined in the order: red, green, blue; - * this is a `WORD *` casted to `void *` - * @return Whether the call was successful - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd372194(v=vs.85).aspx - */ -BOOL SetDeviceGammaRamp(HDC, LPVOID restrict); - -/** - * Get the current gamma ramps for a device - * - * @param hDC The device context - * @param lpRamp The output for the gamma ramps joined in the order: red, green, blue - * This is a `WORD *` casted to `void *` - * @return Whether the call was successful - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd316946(v=vs.85).aspx - */ -BOOL GetDeviceGammaRamp(HDC, LPVOID restrict); - - -/** - * Get the context for a device - * - * @param lpszDriver The driver or a display device, use "DISPLAY" if you want a display - * @param lpszDevice The name of the device. If you want a display use can use the member - * name `DeviceName` in the third parameter, an output parameter, of - * `EnumDisplayDevices`. - * @param lpszOutput We will always use `NULL` here - * @param lpInitData We will always use `NULL` here; this should actually by a `const DEVMODE *` - * @return The context for the device - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183490(v=vs.85).aspx - */ -HDC CreateDC(LPCTSTR restrict, LPCTSTR restrict, LPCTSTR restrict, const void *restrict); - -/** - * This macro does not seem to have an official documentation that is correct - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183490(v=vs.85).aspx - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd374074(v=vs.85).aspx - */ -#define TEXT(X) ((LPCTSTR)(X)) - - -/** - * Information about a display device - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183569(v=vs.85).aspx - */ -typedef struct { - /** - * Set this to `sizeof(DISPLAY_DEVICE)` - */ - DWORD cb; - - /** - * The name of the device - */ - TCHAR DeviceName[32]; - - /** - * The status of the device, can include `DISPLAY_DEVICE_ACTIVE` - */ - DWORD StateFlags; - -} DISPLAY_DEVICE; - -/** - * Appearently we are incapable of putting asterisks at the - * end of types names, so istead we preprend them with 'P' - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183569(v=vs.85).aspx - */ -typedef DISPLAY_DEVICE *PDISPLAY_DEVICE; - -/** - * The monitor is "on" - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183569(v=vs.85).aspx - */ -#define DISPLAY_DEVICE_ACTIVE 1 - -/** - * Get a display device by its name or index - * - * @param lpDevice The name of the device, use `NULL` to base the call on `iDevNum` instead - * @param iDevNum The index of the device - * @param lpDisplayDevice Output for the found device - * @param dwFlags Flags, we will always use zero - * @return Whether the call was successful; zero is also returned if `iDevNum` - * is greater than the largest device index on the system - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd162609(v=vs.85).aspx - */ -BOOL EnumDisplayDevices(LPCTSTR restrict, DWORD, PDISPLAY_DEVICE restrict, DWORD) - - - -#ifndef __GNUC__ -# undef __attribute__ -#endif - -#endif diff --git a/src/lib/gamma-dummy.c.gpp b/src/lib/gamma-dummy.c.gpp deleted file mode 100644 index a9aa724..0000000 --- a/src/lib/gamma-dummy.c.gpp +++ /dev/null @@ -1,993 +0,0 @@ -/* -*- c -*- */ -/* See LICENSE file for copyright and license details. */ -#ifndef HAVE_LIBGAMMA_METHOD_DUMMY -# error Compiling gamma-dummy.c without HAVE_LIBGAMMA_METHOD_DUMMY -#endif - -#include "gamma-dummy.h" - -#include "libgamma-error.h" -#include "libgamma-method.h" -#include "libgamma-facade.h" -#include "edid.h" -#include "gamma-helper.h" - -#include <errno.h> -#include <stdint.h> -#include <stdlib.h> -#include <string.h> - - - -/** - * Configuration set for the dummy adjustment method - */ -typedef struct libgamma_dummy_configurations { - /** - * The method's capabilities - * - * Some fields are ignored: - * - real - * - fake - */ - libgamma_method_capabilities_t capabilities; - - /** - * Template for CRTC:s information - * - * Some fields are ignored: - * - width_mm_edid - * - width_mm_edid_error - * - height_mm_edid - * - height_mm_edid_error - * - gamma_red - * - gamma_green - * - gamma_blue - * - gamma_error - */ - libgamma_crtc_information_t crtc_info_template; - - /** - * The adjustment method to use - */ - int real_method; - - /** - * The number of sites on the system - */ - size_t site_count; - - /** - * The number of paritions on a site before it has been configured - */ - size_t default_partition_count; - - /** - * The number of CRTC:s on a paritions before it has been configured - */ - size_t default_crtc_count; - - /** - * Whether the sites should be inherited from the real method - */ - unsigned inherit_sites : 1; - - /** - * Whether the partitions should be inherited from the real method - */ - unsigned inherit_partition_count : 1; - - /** - * Whether the CRTC:s should be inherited from the real method - */ - unsigned inherit_crtc_count : 1; - - /** - * When a site has been created, stall until the partition count has - * been configured - */ - unsigned stall_for_partition_count : 1; - - /** - * When a parition has been created, stall until the CRTC count has - * been configured - */ - unsigned stall_for_crtc_count : 1; - - /** - * Methods should stall until the system has been configured - * unless $LIBGAMMA_DUMMY_STALL is not true - */ - unsigned stalled_start : 1; - - /** - * Whether to print what is going on in the phony system - */ - unsigned verbose : 1; - -} libgamma_dummy_configurations_t; - - -/** - * Dummy adjustment method internal data for a CRTC - */ -typedef struct libgamma_dummy_crtc { - /** - * The gamma ramp for the red channel - */ - void *restrict gamma_red; - - /** - * The gamma ramp for the green channel - */ - void *restrict gamma_green; - - /** - * The gamma ramp for the blue channel - */ - void *restrict gamma_blue; - - /** - * Information about the CRTC and monitor - * - * Some fields are ignored: - * - width_mm_edid - * - width_mm_edid_error - * - height_mm_edid - * - height_mm_edid_error - * - gamma_red - * - gamma_green - * - gamma_blue - * - gamma_error - */ - libgamma_crtc_information_t info; - - /** - * Partition state that contains this information - */ - libgamma_crtc_state_t *state; - -} libgamma_dummy_crtc_t; - - -/** - * Dummy adjustment method internal data for a partition - */ -typedef struct libgamma_dummy_partition { - /** - * The CRTC:s on the system - */ - libgamma_dummy_crtc_t *crtcs; - - /** - * The number of CRTC:s on the system - */ - size_t crtc_count; - - /** - * Partition state that contains this information - */ - libgamma_partition_state_t *state; - -} libgamma_dummy_partition_t; - - -/** - * Dummy adjustment method internal data for a site - */ -typedef struct libgamma_dummy_site { - /** - * The partitions on the system - */ - libgamma_dummy_partition_t *partitions; - - /** - * The number of partitions on the system - */ - size_t partition_count; - - /** - * Site state that contains this information - */ - libgamma_site_state_t *state; - -} libgamma_dummy_site_t; - - - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * and ignore the method's capabilities - * - * @param this The CRTC data - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -static int libgamma_dummy_crtc_restore_forced(libgamma_dummy_crtc_t *restrict data); - - - -/** - * Configurations for the dummy adjustment method. - */ -static libgamma_dummy_configurations_t libgamma_dummy_configurations = { - .capabilities = { - .crtc_information = (1 << LIBGAMMA_CRTC_INFO_COUNT) - 1, - .default_site_known = 1, - .multiple_sites = 1, - .multiple_partitions = 1, - .multiple_crtcs = 1, - .partitions_are_graphics_cards = 1, - .site_restore = 1, - .partition_restore = 1, - .crtc_restore = 1, - .identical_gamma_sizes = 0, - .fixed_gamma_size = 0, - .fixed_gamma_depth = 0 - }, - .crtc_info_template = { - .edid = NULL, - .edid_length = 0, - .edid_error = LIBGAMMA_EDID_NOT_FOUND, - .width_mm = 400, - .width_mm_error = 0, - .height_mm = 300, - .height_mm_error = 0, - .red_gamma_size = 1024, - .green_gamma_size = 2048, - .blue_gamma_size = 512, - .gamma_size_error = 0, - .gamma_depth = 64, - .gamma_depth_error = 0, - .gamma_support = 1, - .gamma_support_error = 0, - .subpixel_order = LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_RGB, - .subpixel_order_error = 0, - .active = 1, - .active_error = 0, - .connector_name = NULL, - .connector_name_error = LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED, - .connector_type = LIBGAMMA_CONNECTOR_TYPE_Unknown, - .connector_type_error = 0 - }, - .real_method = LIBGAMMA_METHOD_DUMMY, - .site_count = 2, - .default_partition_count = 2, - .default_crtc_count = 2, - .inherit_sites = 1, - .inherit_partition_count = 1, - .inherit_crtc_count = 1, - .stall_for_partition_count = 0, - .stall_for_crtc_count = 0, - .stalled_start = 1, - .verbose = 0 -}; - - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void -libgamma_dummy_method_capabilities(libgamma_method_capabilities_t *restrict this) -{ - int real_method = libgamma_dummy_configurations.real_method; - *this = libgamma_dummy_configurations.capabilities; - this->real = real_method != LIBGAMMA_METHOD_DUMMY; - this->fake = this->real; - this->auto_restore = real_method == LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS; -} - - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_dummy_site_initialise(libgamma_site_state_t *restrict this, char *restrict site) -{ - libgamma_dummy_site_t *data = NULL; - size_t i, sites, crtcs; - - sites = libgamma_dummy_configurations.site_count; - if (!libgamma_dummy_configurations.capabilities.multiple_sites) - sites = !!sites; - this->data = NULL; - - if (site && *site && (atoll(site) < 0 || sites <= (unsigned long long)atoll(site))) - return LIBGAMMA_NO_SUCH_SITE; - - data = malloc(sizeof(libgamma_dummy_site_t)); - if (!data) - goto fail; - - this->data = data; - data->state = this; - - data->partition_count = libgamma_dummy_configurations.default_partition_count; - if (!libgamma_dummy_configurations.capabilities.multiple_partitions) - data->partition_count = !!data->partition_count; - - crtcs = libgamma_dummy_configurations.default_crtc_count; - if (!libgamma_dummy_configurations.capabilities.multiple_crtcs) - crtcs = !!crtcs; - - data->partitions = malloc(data->partition_count * sizeof(libgamma_dummy_partition_t)); - if (!data->partitions) - goto fail; - - for (i = 0; i < data->partition_count; i++) - data->partitions[i].crtc_count = crtcs; - - this->partitions_available = data->partition_count; - - return 0; - -fail: - free(data); - this->data = NULL; - return LIBGAMMA_ERRNO_SET; -} - - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void -libgamma_dummy_site_destroy(libgamma_site_state_t *restrict this) -{ - libgamma_dummy_site_t *data = this->data; - if (!data) - return; - - free(data->partitions); - free(data); -} - - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_dummy_site_restore(libgamma_site_state_t *restrict this) -{ - libgamma_dummy_site_t *data = this->data; - size_t i, j; - - if (!libgamma_dummy_configurations.capabilities.site_restore) { - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; - } - - for (j = 0; j < data->partition_count; j++) - for (i = 0; i < data->partitions[j].crtc_count; i++) - if (libgamma_dummy_crtc_restore_forced(data->partitions[j].crtcs + i) < 0) - return -1; - - return 0; -} - - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_dummy_partition_initialise(libgamma_partition_state_t *restrict this, - libgamma_site_state_t *restrict site, size_t partition) -{ - libgamma_crtc_information_t template = libgamma_dummy_configurations.crtc_info_template; - libgamma_dummy_site_t *site_data = site->data; - libgamma_dummy_partition_t *data = &site_data->partitions[partition]; - libgamma_dummy_crtc_t *crtc_data; - size_t i, n; - - this->data = NULL; - - if (partition >= site_data->partition_count) - return LIBGAMMA_NO_SUCH_PARTITION; - - this->data = data; - data->state = this; - - data->crtcs = calloc(data->crtc_count, sizeof(libgamma_dummy_crtc_t)); - if (!data->crtcs) - goto fail; - for (i = 0; i < data->crtc_count; i++) { - crtc_data = data->crtcs + i; - crtc_data->info = template; - - /* Duplicate strings */ - if (crtc_data->info.edid) { - crtc_data->info.edid = malloc(crtc_data->info.edid_length * sizeof(char)); - if (!crtc_data->info.edid) - goto fail; - memcpy(crtc_data->info.edid, template.edid, crtc_data->info.edid_length * sizeof(char)); - } - if (crtc_data->info.connector_name) { - n = strlen(crtc_data->info.connector_name); - crtc_data->info.connector_name = malloc((n + 1) * sizeof(char)); - if (crtc_data->info.connector_name == NULL) - goto fail; - memcpy(crtc_data->info.connector_name, template.connector_name, (n + 1) * sizeof(char)); - } - } - - this->crtcs_available = data->crtc_count; - - return 0; - -fail: - for (i = 0; i < data->crtc_count; i++) { - free(data->crtcs[i].info.edid); - free(data->crtcs[i].info.connector_name); - } - free(data->crtcs); - data->crtcs = NULL; - return LIBGAMMA_ERRNO_SET; -} - - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void -libgamma_dummy_partition_destroy(libgamma_partition_state_t *restrict this) -{ - libgamma_dummy_partition_t *data = this->data; - size_t i; - - if (data) { - for (i = 0; i < data->crtc_count; i++) { - free(data->crtcs[i].info.edid); - free(data->crtcs[i].info.connector_name); - } - free(data->crtcs); - data->crtcs = NULL; - } -} - - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_dummy_partition_restore(libgamma_partition_state_t *restrict this) -{ - libgamma_dummy_partition_t *data = this->data; - size_t i; - - if (!libgamma_dummy_configurations.capabilities.partition_restore) { - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; - } - - for (i = 0; i < data->crtc_count; i++) - if (libgamma_dummy_crtc_restore_forced(data->crtcs + i) < 0) - return -1; - - return 0; -} - - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_dummy_crtc_initialise(libgamma_crtc_state_t *restrict this, - libgamma_partition_state_t *restrict partition, size_t crtc) -{ - libgamma_dummy_partition_t *partition_data = partition->data; - libgamma_dummy_crtc_t *data = &partition_data->crtcs[crtc]; - size_t stop_size; - - this->data = NULL; - - if (crtc >= partition_data->crtc_count) - return LIBGAMMA_NO_SUCH_CRTC; - - this->data = data; - data->state = this; - - if (data->info.gamma_depth == -1) - stop_size = sizeof(float); - else if (data->info.gamma_depth == -2) - stop_size = sizeof(double); - else - stop_size = (size_t)data->info.gamma_depth / 8; - - data->gamma_red = malloc(data->info.red_gamma_size * stop_size); - if (!data->gamma_red) - goto fail; - data->gamma_green = malloc(data->info.green_gamma_size * stop_size); - if (!data->gamma_green) - goto fail; - data->gamma_blue = malloc(data->info.blue_gamma_size * stop_size); - if (!data->gamma_blue) - goto fail; - - return libgamma_dummy_crtc_restore_forced(data); - -fail: - free(data->gamma_red), data->gamma_red = NULL; - free(data->gamma_green), data->gamma_green = NULL; - free(data->gamma_blue), data->gamma_blue = NULL; - return LIBGAMMA_ERRNO_SET; -} - - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void -libgamma_dummy_crtc_destroy(libgamma_crtc_state_t *restrict this) -{ - libgamma_dummy_crtc_t *data = this->data; - if (data) { - free(data->gamma_red), data->gamma_red = NULL; - free(data->gamma_green), data->gamma_green = NULL; - free(data->gamma_blue), data->gamma_blue = NULL; - } -} - - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_dummy_crtc_restore(libgamma_crtc_state_t *restrict this) -{ - if (!libgamma_dummy_configurations.capabilities.crtc_restore) { - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; - } - return libgamma_dummy_crtc_restore_forced(this->data); -} - - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * and ignore the method's capabilities - * - * @param this The CRTC data - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -static int -libgamma_dummy_crtc_restore_forced(libgamma_dummy_crtc_t *restrict data) -{ - size_t rn = data->info. red_gamma_size; - size_t gn = data->info.green_gamma_size; - size_t bn = data->info. blue_gamma_size; - size_t i; - - if (!data->gamma_red) - return 0; - -#define __reset_ramps(TYPE, MAX)\ - do {\ - TYPE *red = data->gamma_red;\ - TYPE *green = data->gamma_green;\ - TYPE *blue = data->gamma_blue;\ - for (i = 0; i < rn; i++) red [i] = (TYPE)((double)(MAX) * ((double)i / (double)(rn - 1)));\ - for (i = 0; i < gn; i++) green[i] = (TYPE)((double)(MAX) * ((double)i / (double)(gn - 1)));\ - for (i = 0; i < bn; i++) blue [i] = (TYPE)((double)(MAX) * ((double)i / (double)(bn - 1)));\ - } while (0) - - if (data->info.gamma_depth == 8) __reset_ramps(uint8_t, INT8_MAX); - else if (data->info.gamma_depth == 16) __reset_ramps(uint16_t, INT16_MAX); - else if (data->info.gamma_depth == 32) __reset_ramps(uint32_t, INT32_MAX); - else if (data->info.gamma_depth == 64) __reset_ramps(uint64_t, INT64_MAX); - else if (data->info.gamma_depth == -1) __reset_ramps(float, 1); - else __reset_ramps(double, 1); - -#undef __reset_ramps - - return 0; -} - - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error. On error refer to the error reports in `this` - */ -int -libgamma_dummy_get_crtc_information(libgamma_crtc_information_t *restrict this, - libgamma_crtc_state_t *restrict crtc, int32_t fields) -{ - libgamma_dummy_crtc_t *restrict data = crtc->data; - int supported = libgamma_dummy_configurations.capabilities.crtc_information; - int e = 0; - size_t n; - - /* Copy over information */ - *this = data->info; - - /* Duplicate strings */ - if (this->edid) { - this->edid = malloc(this->edid_length * sizeof(char)); - if (!this->edid) - this->edid_error = errno; - memcpy(this->edid, data->info.edid, this->edid_length * sizeof(char)); - } - if (this->connector_name) { - n = strlen(this->connector_name); - this->connector_name = malloc((n + 1) * sizeof(char)); - if (!this->connector_name) - this->connector_name_error = errno; - memcpy(this->connector_name, data->info.connector_name, (n + 1) * sizeof(char)); - } - - /* Parse EDID */ - if (this->edid_error) - this->width_mm_edid_error = this->height_mm_edid_error = this->gamma_error = this->edid_error; - else if (fields & (LIBGAMMA_CRTC_INFO_MACRO_EDID ^ LIBGAMMA_CRTC_INFO_EDID)) - e |= libgamma_parse_edid(this, fields); - - /* Test errors */ -#define _E(FIELD, VAR)\ - ((fields & FIELD) ? ((supported & FIELD) ? VAR : (VAR = LIBGAMMA_CRTC_INFO_NOT_SUPPORTED)) : 0) - e |= _E(LIBGAMMA_CRTC_INFO_EDID, this->edid_error); - e |= _E(LIBGAMMA_CRTC_INFO_WIDTH_MM, this->width_mm_error); - e |= _E(LIBGAMMA_CRTC_INFO_HEIGHT_MM, this->height_mm_error); - e |= _E(LIBGAMMA_CRTC_INFO_GAMMA_SIZE, this->gamma_size_error); - e |= _E(LIBGAMMA_CRTC_INFO_GAMMA_DEPTH, this->gamma_depth_error); - e |= _E(LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT, this->gamma_support_error); - e |= _E(LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER, this->subpixel_order_error); - e |= _E(LIBGAMMA_CRTC_INFO_ACTIVE, this->active_error); - e |= _E(LIBGAMMA_CRTC_INFO_CONNECTOR_NAME, this->connector_name_error); - e |= _E(LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE, this->connector_type_error); - - if ((fields & LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID) && !(supported & LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID)) - e |= this->width_mm_edid_error = LIBGAMMA_CRTC_INFO_NOT_SUPPORTED; - if ((fields & LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID) && !(supported & LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID)) - e |= this->height_mm_edid_error = LIBGAMMA_CRTC_INFO_NOT_SUPPORTED; - if ((fields & LIBGAMMA_CRTC_INFO_GAMMA) && !(supported & LIBGAMMA_CRTC_INFO_GAMMA)) - e |= this->gamma_error = LIBGAMMA_CRTC_INFO_NOT_SUPPORTED; -#undef _E - - return e ? -1 : 0; -} - - -/** - * Get the current gamma ramps for a CRTC - * - * @param 1 The data type for the ramp stop elements - * @param 2 The `ramp*` pattern for the ramp structure and function to call. - * @param 3 Either of `bit8`, `bit16`, `bit32`, `bit64`, `float_single`, `float_double`; - * rather self-explanatory - * @param 4 The number of bits in the gamma depth, -1 for single precision float, - * (`float`) and -2 for double percition float (`double`) - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_get_gamma_ramps () -$>{ -int -libgamma_dummy_crtc_get_gamma_${2}(libgamma_crtc_state_t *restrict this, libgamma_gamma_${2}_t *restrict ramps) -{ - libgamma_dummy_crtc_t *data = this->data; - libgamma_gamma_ramps_any_t ramps_; - ${1}* r_ramp = data->gamma_red; - ${1}* g_ramp = data->gamma_green; - ${1}* b_ramp = data->gamma_blue; - size_t rn = data->info.red_gamma_size; - size_t gn = data->info.green_gamma_size; - size_t bn = data->info.blue_gamma_size; - size_t i; - -#ifdef DEBUG - /* Check gamma ramp sizes */ - if (libgamma_dummy_configurations.capabilities.identical_gamma_sizes) - if (ramps->red_size != ramps->green_size || ramps->red_size != ramps->blue_size) - return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; - if (ramps->red_size != rn || ramps->green_size != gn || ramps->blue_size != bn) - return LIBGAMMA_WRONG_GAMMA_RAMP_SIZE; -#endif - - if (!data->info.gamma_support) - return LIBGAMMA_GAMMA_RAMP_READ_FAILED; - -#define __trans(DEPTH, SUFFIX)\ - do {\ - if (data->info.gamma_depth == DEPTH) {\ - ramps_.${3} = *ramps;\ - return libgamma_translated_ramp_get(this, &ramps_, ${4}, DEPTH, libgamma_crtc_get_gamma_ramps ## SUFFIX);\ - }\ - } while (0) - -$>if [ ! ${4} = 8 ]; then - __trans(8, 8); -$>fi -$>if [ ! ${4} = 16 ]; then - __trans(16, 16); -$>fi -$>if [ ! ${4} = 32 ]; then - __trans(32, 32); -$>fi -$>if [ ! ${4} = 64 ]; then - __trans(64, 64); -$>fi -$>if [ ! ${4} = -1 ]; then - __trans(-1, f); -$>fi -$>if [ ! ${4} = -2 ]; then - __trans(-2, d); -$>fi - -#undef __trans - - for (i = 0; i < rn; i++) ramps->red[i] = r_ramp[i]; - for (i = 0; i < gn; i++) ramps->green[i] = g_ramp[i]; - for (i = 0; i < bn; i++) ramps->blue[i] = b_ramp[i]; - - return 0; -} -$>} - - -/** - * Set the gamma ramps for a CRTC - * - * @param 1 The data type for the ramp stop elements - * @param 2 The `ramp*` pattern for the ramp structure and function to call. - * @param 3 Either of `bit8`, `bit16`, `bit32`, `bit64`, `float_single`, `float_double`; - * rather self-explanatory - * @param 4 The number of bits in the gamma depth, -1 for single precision float, - * (`float`) and -2 for double percition float (`double`) - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_set_gamma_ramps () -$>{ -int -libgamma_dummy_crtc_set_gamma_${2}(libgamma_crtc_state_t *restrict this, libgamma_gamma_${2}_t ramps) -{ - libgamma_dummy_crtc_t *data = this->data; - libgamma_gamma_ramps_any_t ramps_; - ${1} *r_ramp = data->gamma_red; - ${1} *g_ramp = data->gamma_green; - ${1} *b_ramp = data->gamma_blue; - size_t rn = data->info.red_gamma_size; - size_t gn = data->info.green_gamma_size; - size_t bn = data->info.blue_gamma_size; - size_t i; - -#ifdef DEBUG - /* Check gamma ramp sizes */ - if (libgamma_dummy_configurations.capabilities.identical_gamma_sizes) - if (ramps.red_size != ramps.green_size || ramps.red_size != ramps.blue_size) - return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; - if (ramps.red_size != rn || ramps.green_size != gn || ramps.blue_size != bn) - return LIBGAMMA_WRONG_GAMMA_RAMP_SIZE; -#endif - - if (!data->info.gamma_support) - return LIBGAMMA_GAMMA_RAMP_READ_FAILED; - -#define __trans(DEPTH, SUFFIX)\ - do {\ - if (data->info.gamma_depth == DEPTH) {\ - ramps_.${3} = ramps;\ - return libgamma_translated_ramp_set(this, ramps_, ${4}, DEPTH, libgamma_crtc_set_gamma_ramps ## SUFFIX);\ - }\ - } while (0) - -$>if [ ! ${4} = 8 ]; then - __trans(8, 8); -$>fi -$>if [ ! ${4} = 16 ]; then - __trans(16, 16); -$>fi -$>if [ ! ${4} = 32 ]; then - __trans(32, 32); -$>fi -$>if [ ! ${4} = 64 ]; then - __trans(64, 64); -$>fi -$>if [ ! ${4} = -1 ]; then - __trans(-1, f); -$>fi -$>if [ ! ${4} = -2 ]; then - __trans(-2, d); -$>fi - -#undef __trans - - for (i = 0; i < rn; i++) r_ramp[i] = ramps.red[i]; - for (i = 0; i < gn; i++) g_ramp[i] = ramps.green[i]; - for (i = 0; i < bn; i++) b_ramp[i] = ramps.blue[i]; - - return 0; -} -$>} - - - -/** - * Get the current gamma ramps for a CRTC, 8-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_get_gamma_ramps uint8_t ramps8 bits8 8 - - -/** - * Set the gamma ramps for a CRTC, 8-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_set_gamma_ramps uint8_t ramps8 bits8 8 - - - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_get_gamma_ramps uint16_t ramps16 bits16 16 - - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_set_gamma_ramps uint16_t ramps16 bits16 16 - - - -/** - * Get the current gamma ramps for a CRTC, 32-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_get_gamma_ramps uint32_t ramps32 bits32 32 - - -/** - * Set the gamma ramps for a CRTC, 32-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_set_gamma_ramps uint32_t ramps32 bits32 32 - - - -/** - * Get the current gamma ramps for a CRTC, 64-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_get_gamma_ramps uint64_t ramps64 bits64 64 - - -/** - * Set the gamma ramps for a CRTC, 64-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_set_gamma_ramps uint64_t ramps64 bits64 64 - - - -/** - * Get the current gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_get_gamma_ramps float rampsf float_single -1 - - -/** - * Set the gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_set_gamma_ramps float rampsf float_single -1 - - - -/** - * Get the current gamma ramps for a CRTC, `double` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_get_gamma_ramps double rampsd float_double -2 - - -/** - * Set the gamma ramps for a CRTC, `double` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_set_gamma_ramps double rampsd float_double -2 diff --git a/src/lib/gamma-dummy.h b/src/lib/gamma-dummy.h deleted file mode 100644 index a52b087..0000000 --- a/src/lib/gamma-dummy.h +++ /dev/null @@ -1,243 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_GAMMA_DUMMY_H -#define LIBGAMMA_GAMMA_DUMMY_H - -#ifndef HAVE_LIBGAMMA_METHOD_DUMMY -# error Including gamma-dummy.h without HAVE_LIBGAMMA_METHOD_DUMMY -#endif - - -#include "libgamma-method.h" - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void libgamma_dummy_method_capabilities(libgamma_method_capabilities_t *restrict); - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_site_initialise(libgamma_site_state_t *restrict, char *restrict); - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void libgamma_dummy_site_destroy(libgamma_site_state_t *restrict); - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_site_restore(libgamma_site_state_t *restrict); - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void libgamma_dummy_partition_destroy(libgamma_partition_state_t *restrict); - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_partition_restore(libgamma_partition_state_t *restrict); - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void libgamma_dummy_crtc_destroy(libgamma_crtc_state_t *restrict); - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_restore(libgamma_crtc_state_t *restrict); - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error; on error refer to the error reports in `this` - */ -int libgamma_dummy_get_crtc_information(libgamma_crtc_information_t *restrict, libgamma_crtc_state_t *restrict, int32_t); - -/** - * Get the current gamma ramps for a CRTC, 8-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_get_gamma_ramps8(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps8_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 8-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_set_gamma_ramps8(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps8_t); - - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t); - - -/** - * Get the current gamma ramps for a CRTC, 32-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_get_gamma_ramps32(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps32_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 32-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_set_gamma_ramps32(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps32_t); - - -/** - * Get the current gamma ramps for a CRTC, 64-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_get_gamma_ramps64(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps64_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 64-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_set_gamma_ramps64(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps64_t); - - -/** - * Get the current gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_get_gamma_rampsf(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsf_t *restrict); - -/** - * Set the gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_set_gamma_rampsf(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsf_t); - -/** - * Get the current gamma ramps for a CRTC, `double` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_get_gamma_rampsd(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsd_t *restrict); - -/** - * Set the gamma ramps for a CRTC, `double` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_set_gamma_rampsd(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsd_t); - - -#endif diff --git a/src/lib/gamma-helper.c b/src/lib/gamma-helper.c deleted file mode 100644 index f67e107..0000000 --- a/src/lib/gamma-helper.c +++ /dev/null @@ -1,372 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include "gamma-helper.h" - -#include "libgamma-method.h" -#include "libgamma-error.h" - -#include <errno.h> -#include <stdlib.h> -#include <stdint.h> - - -/** - * Just an arbitrary version - */ -#define ANY bits64 - -/** - * Concatenation of all ramps - */ -#define ALL red - - -/** - * Preform installation in an `for (i = 0; i < n; i++)` - * loop and do a `break` afterwords - */ -#define __translate(instruction) for (i = 0; i < n; i++) instruction; break - - -/** - * Convert a [0, 1] `float` to a full range `uint64_t` - * and mark sure rounding errors does not cause the - * value be 0 instead of ~0 and vice versa - * - * @param value To `float` to convert - * @return The value as an `uint64_t` - */ -static inline uint64_t -float_to_64(float value) -{ - /* TODO Which is faster? */ - -#if defined(HAVE_INT128) && __WORDSIZE == 64 - /* `__int128` is a GNU C extension, which - (because it is not ISO C) emits a warning - under -pedantic */ -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wpedantic" - - /* In GCC we can use `__int128`, this is - a signed 128-bit integer. It fits all - uint64_t values but also native values, - which is a nice because it eleminates - some overflow condition tests. It is - also more readable. */ - - /* Convert to integer */ - __int128 product = (__int128)(value * (float)UINT64_MAX); - /* Negative overflow */ - if (product > UINT64_MAX) - return UINT64_MAX; - /* Positive overflow */ - if (product < 0) - return 0; - /* Did not overflow */ - return (uint64_t)product; - -# pragma GCC diagnostic pop -#else - - /* If we are not using GCC we cannot be - sure that we have `__int128` so we have - to use `uint64_t` and perform overflow - checkes based on the input value */ - - /* Convert to integer. */ - uint64_t product = (uint64_t)(value * (float)UINT64_MAX); - /* Negative overflow, - if the input is less than 0.5 but - the output is greater then we got - -1 when we should have gotten 0 */ - if (value < 0.1f && product > 0xF000000000000000ULL) - return 0; - /* Positive overflow, - if the input is greater than 0.5 - but the output is less then we got - 0 when we should have gotten ~0 */ - else if (value > 0.9f && product < 0x1000000000000000ULL) - return (uint64_t)~0; - /* Did not overflow */ - return product; - -#endif -} - - -/** - * Convert a [0, 1] `double` to a full range `uint64_t` - * and mark sure rounding errors does not cause the - * value be 0 instead of ~0 and vice versa - * - * @param value To `double` to convert - * @return The value as an `uint64_t` - */ -static inline uint64_t -double_to_64(double value) -{ - /* XXX Which is faster? */ - -#if defined(HAVE_INT128) && __WORDSIZE == 64 - /* `__int128` is a GNU C extension, which - (because it is not ISO C) emits a warning - under -pedantic */ -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wpedantic" - - /* In GCC we can use `__int128`, this is - a signed 128-bit integer. It fits all - uint64_t values but also native values, - which is a nice because it eleminates - some overflow condition tests. It is - also more readable. */ - - /* Convert to integer */ - __int128 product = (__int128)(value * (double)UINT64_MAX); - /* Negative overflow */ - if (product > UINT64_MAX) - return UINT64_MAX; - /* Positive overflow */ - if (product < 0) - return 0; - /* Did not overflow */ - return (uint64_t)product; - -# pragma GCC diagnostic pop -#else - - /* If we are not using GCC we cannot be - sure that we have `__int128` so we have - to use `uint64_t` and perform overflow - checkes based on the input value. */ - - /* Convert to integer. */ - uint64_t product = (uint64_t)(value * (double)UINT64_MAX); - /* Negative overflow, - if the input is less than 0.5 but - the output is greater then we got - -1 when we should have gotten 0 */ - if (value < (double)0.1f && product > 0xF000000000000000ULL) - product = 0; - /* Positive overflow, - if the input is greater than 0.5 - but the output is less then we got - 0 when we should have gotten ~0 */ - else if ((value > (double)0.9f) && (product < 0x1000000000000000ULL)) - product = (uint64_t)~0; - /* Did not overflow */ - return product; - -#endif -} - - -/** - * Convert any set of gamma ramps into a 64-bit integer array with all channels - * - * @param depth The depth of the gamma ramp, `-1` for `float`, `-2` for `double` - * @param n The grand size of gamma ramps (sum of all channels' sizes) - * @param out Output array - * @param in Input gamma ramps - */ -static void -translate_to_64(signed depth, size_t n, uint64_t *restrict out, libgamma_gamma_ramps_any_t in) -{ - size_t i; - switch (depth) { - /* Translate integer */ - case 8: __translate(out[i] = (uint64_t)(in.bits8. ALL[i]) * 0x0101010101010101ULL); - case 16: __translate(out[i] = (uint64_t)(in.bits16.ALL[i]) * 0x0001000100010001ULL); - case 32: __translate(out[i] = (uint64_t)(in.bits32.ALL[i]) * 0x0000000100000001ULL); - /* Identity translation */ - case 64: __translate(out[i] = in.bits64.ALL[i]); - /* Translate floating point */ - case -1: __translate(out[i] = float_to_64(in.float_single.ALL[i])); - case -2: __translate(out[i] = double_to_64(in.float_double.ALL[i])); - default: - /* This is not possible */ - abort(); - break; - } -} - - -/** - * Undo the actions of `translate_to_64` - * - * @param depth The depth of the gamma ramp, `-1` for `float`, `-2` for `double` - * @param n The grand size of gamma ramps (sum of all channels' sizes) - * @param out Output gamma ramps - * @param in Input array, may be modified - */ -static void -translate_from_64(signed depth, size_t n, libgamma_gamma_ramps_any_t out, uint64_t *restrict in) -{ - size_t i; - switch (depth) { - /* Translate integer */ - case 8: __translate(out.bits8. ALL[i] = (uint8_t)(in[i] / 0x0101010101010101ULL)); - case 16: __translate(out.bits16.ALL[i] = (uint16_t)(in[i] / 0x0001000100010001ULL)); - case 32: __translate(out.bits32.ALL[i] = (uint32_t)(in[i] / 0x0000000100000001ULL)); - /* Identity translation */ - case 64: __translate(out.bits64.ALL[i] = in[i]); - /* Translate floating point */ - case -1: __translate(out.float_single.ALL[i] = (float)(in[i]) / (float)UINT64_MAX); - case -2: __translate(out.float_double.ALL[i] = (double)(in[i]) / (double)UINT64_MAX); - default: - /* This is not possible */ - abort(); - break; - } -} - - -/** - * Allocate and initalise a gamma ramp with any depth - * - * @param ramps_sys Output gamma ramps - * @param ramps The gamma ramps whose sizes should be duplicated - * @param depth The depth of the gamma ramps to allocate, - * `-1` for `float`, `-2` for `double` - * @param elements Output reference for the grand size of the gamma ramps - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -static int -allocated_any_ramp(libgamma_gamma_ramps_any_t *restrict ramps_sys, - libgamma_gamma_ramps_any_t ramps, signed depth, size_t *restrict elements) -{ - /* Calculate the size of the allocation to do */ - size_t d, n = ramps.ANY.red_size + ramps.ANY.green_size + ramps.ANY.blue_size; - switch (depth) { - case 8: d = sizeof(uint8_t); break; - case 16: d = sizeof(uint16_t); break; - case 32: d = sizeof(uint32_t); break; - case 64: d = sizeof(uint64_t); break; - case -1: d = sizeof(float); break; - case -2: d = sizeof(double); break; - default: - return errno = EINVAL, LIBGAMMA_ERRNO_SET; - } - - /* Copy the gamma ramp sizes */ - ramps_sys->ANY = ramps.ANY; - /* Allocate the new ramps */ -#ifdef HAVE_LIBGAMMA_METHOD_LINUX_DRM - /* Valgrind complains about us reading uninitialize memory if we just use malloc */ - ramps_sys->ANY.red = calloc(n, d); -#else - ramps_sys->ANY.red = malloc(n * d); -#endif - ramps_sys->ANY.green = (void *)&((char *)ramps_sys->ANY. red)[ramps.ANY. red_size * d / sizeof(char)]; - ramps_sys->ANY.blue = (void *)&((char *)ramps_sys->ANY.green)[ramps.ANY.green_size * d / sizeof(char)]; - - /* Report the total gamma ramp size */ - *elements = n; - /* Report successfulness */ - return ramps_sys->ANY.red ? 0 : LIBGAMMA_ERRNO_SET; -} - - -/** - * Get the current gamma ramps for a CRTC, re-encoding version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @param depth_user The depth of the gamma ramps that are provided by the user, - * `-1` for `float`, `-2` for `double` - * @param depth_system The depth of the gamma ramps as required by the adjustment method, - * `-1` for `float`, `-2` for `double` - * @param fun Function that is to be used read the ramps, its parameters have - * the same function as those of this function with the same names, - * and the return value too is identical - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_translated_ramp_get_(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps_any_t *restrict ramps, - signed depth_user, signed depth_system, libgamma_get_ramps_any_fun *fun) -{ - size_t n; - int r; - libgamma_gamma_ramps_any_t ramps_sys; - uint64_t *restrict ramps_full; - - /* Allocate ramps with proper data type */ - if ((r = allocated_any_ramp(&ramps_sys, *ramps, depth_system, &n))) - return r; - - /* Fill the ramps */ - if ((r = fun(this, &ramps_sys))) - return free(ramps_sys.ANY.red), r; - - /* Allocate intermediary ramps */ - ramps_full = malloc(n * sizeof(uint64_t)); - if (!ramps_full) { - free(ramps_sys.ANY.red); - return LIBGAMMA_ERRNO_SET; - } - - /* Translate ramps to 64-bit integers */ - translate_to_64(depth_system, n, ramps_full, ramps_sys); - free(ramps_sys.ANY.red); - - /* Translate ramps to the user's format */ - translate_from_64(depth_user, n, *ramps, ramps_full); - free(ramps_full); - return 0; -} - - -/** - * Set the gamma ramps for a CRTC, re-encoding version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @param depth_user The depth of the gamma ramps that are provided by the user, - * `-1` for `float`, `-2` for `double` - * @param depth_system The depth of the gamma ramps as required by the adjustment method, - * `-1` for `float`, `-2` for `double` - * @param fun Function that is to be used write the ramps, its parameters have - * the same function as those of this function with the same names, - * and the return value too is identical - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_translated_ramp_set_(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps_any_t ramps, - signed depth_user, signed depth_system, libgamma_set_ramps_any_fun *fun) -{ - size_t n; - int r; - libgamma_gamma_ramps_any_t ramps_sys; - uint64_t *restrict ramps_full; - - /* Allocate ramps with proper data type */ - if ((r = allocated_any_ramp(&ramps_sys, ramps, depth_system, &n))) - return r; - - /* Allocate intermediary ramps */ - ramps_full = malloc(n * sizeof(uint64_t)); - if (!ramps_full) { - free(ramps_sys.ANY.red); - return LIBGAMMA_ERRNO_SET; - } - - /* Translate ramps to 64-bit integers. */ - translate_to_64(depth_user, n, ramps_full, ramps); - /* Translate ramps to the proper format. */ - translate_from_64(depth_system, n, ramps_sys, ramps_full); - free(ramps_full); - - /* Apply the ramps */ - r = fun(this, ramps_sys); - - free(ramps_sys.ANY.red); - return r; -} - - -#undef __translate -#undef ALL -#undef ANY diff --git a/src/lib/gamma-helper.h b/src/lib/gamma-helper.h deleted file mode 100644 index 38d678d..0000000 --- a/src/lib/gamma-helper.h +++ /dev/null @@ -1,144 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_GAMMA_HELPER_H -#define LIBGAMMA_GAMMA_HELPER_H - - -#include "libgamma-method.h" - - -/** - * Gamma ramp structure union for different depths - */ -typedef union libgamma_gamma_ramps_any { - /** - * 8-bit gamma ramps - */ - libgamma_gamma_ramps8_t bits8; - - /** - * 16-bit gamma ramps - */ - libgamma_gamma_ramps16_t bits16; - - /** - * 32-bit gamma ramps - */ - libgamma_gamma_ramps32_t bits32; - - /** - * 64-bit gamma ramps - */ - libgamma_gamma_ramps64_t bits64; - - /** - * Single precision float gamma ramps - */ - libgamma_gamma_rampsf_t float_single; - - /** - * Double precision float gamma ramps - */ - libgamma_gamma_rampsd_t float_double; - -} libgamma_gamma_ramps_any_t; - - -/** - * A function for reading the gamma ramps from a CRTC - * - * @param this The CRTC state - * @param ramps The store for the gamma ramps - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -typedef int libgamma_get_ramps_any_fun(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps_any_t *restrict); - -/** - * A function for writing the gamma ramps to a CRTC - * - * @param this The CRTC state - * @param ramps The gamma ramps - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -typedef int libgamma_set_ramps_any_fun(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps_any_t); - - - -/** - * Get the current gamma ramps for a CRTC, re-encoding version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @param depth_user The depth of the gamma ramps that are provided by the user, - * `-1` for `float`, `-2` for `double` - * @param depth_system The depth of the gamma ramps as required by the adjustment method, - * `-1` for `float`, `-2` for `double` - * @param fun Function that is to be used read the ramps, its parameters have - * the same function as those of this function with the same names, - * and the return value too is identical - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -#define libgamma_translated_ramp_get(this, ramps, depth_user, depth_system, fun)\ - libgamma_translated_ramp_get_((this), (ramps), (depth_user), (depth_system), (libgamma_get_ramps_any_fun *)(fun)) - - -/** - * Set the gamma ramps for a CRTC, re-encoding version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @param depth_user The depth of the gamma ramps that are provided by the user, - * `-1` for `float`, `-2` for `double` - * @param depth_system The depth of the gamma ramps as required by the adjustment method, - * `-1` for `float`, `-2` for `double` - * @param fun Function that is to be used write the ramps, its parameters have - * the same function as those of this function with the same names, - * and the return value too is identical - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -#define libgamma_translated_ramp_set(this, ramps, depth_user, depth_system, fun)\ - libgamma_translated_ramp_set_((this), (ramps), (depth_user), (depth_system), (libgamma_set_ramps_any_fun *)fun) - - -/** - * Get the current gamma ramps for a CRTC, re-encoding version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @param depth_user The depth of the gamma ramps that are provided by the user, - * `-1` for `float`, `-2` for `double` - * @param depth_system The depth of the gamma ramps as required by the adjustment method, - * `-1` for `float`, `-2` for `double` - * @param fun Function that is to be used read the ramps, its parameters have - * the same function as those of this function with the same names, - * and the return value too is identical - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_translated_ramp_get_(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps_any_t *restrict, - signed, signed, libgamma_get_ramps_any_fun *); - - -/** - * Set the gamma ramps for a CRTC, re-encoding version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @param depth_user The depth of the gamma ramps that are provided by the user, - * `-1` for `float`, `-2` for `double` - * @param depth_system The depth of the gamma ramps as required by the adjustment method, - * `-1` for `float`, `-2` for `double` - * @param fun Function that is to be used write the ramps, its parameters have - * the same function as those of this function with the same names, - * and the return value too is identical - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_translated_ramp_set_(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps_any_t, - signed, signed, libgamma_set_ramps_any_fun *); - - -#endif diff --git a/src/lib/gamma-linux-drm.c b/src/lib/gamma-linux-drm.c deleted file mode 100644 index c2fc646..0000000 --- a/src/lib/gamma-linux-drm.c +++ /dev/null @@ -1,902 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef HAVE_LIBGAMMA_METHOD_LINUX_DRM -# error Compiling gamma-linux-drm.c without HAVE_LIBGAMMA_METHOD_LINUX_DRM -#endif - -#define _GNU_SOURCE - -#include "gamma-linux-drm.h" - -#include "libgamma-error.h" -#include "edid.h" - -#include <limits.h> -#include <stdlib.h> -#include <errno.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <grp.h> -#include <unistd.h> -#include <stdio.h> -#include <string.h> -#include <inttypes.h> - -#include <xf86drm.h> -#include <xf86drmMode.h> - -#ifndef O_CLOEXEC -# define O_CLOEXEC 02000000 -#endif -#ifndef NGROUPS_MAX -# define NGROUPS_MAX 65536 -#endif -#ifndef PATH_MAX -# define PATH_MAX 4096 -#endif - - - -/** - * Graphics card data for the Direct Rendering Manager adjustment method - */ -typedef struct libgamma_drm_card_data { - /** - * File descriptor for the connection to the graphics card - */ - int fd; - - /** - * The graphics card's mode resources - */ - drmModeRes *res; - - /** - * Resources for open connectors - */ - drmModeConnector **connectors; - - /** - * Resources for open encoders - */ - drmModeEncoder **encoders; - -} libgamma_drm_card_data_t; - - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void libgamma_linux_drm_method_capabilities(libgamma_method_capabilities_t *restrict this) -{ - /* Support for all information except gamma ramp support */ - this->crtc_information = LIBGAMMA_CRTC_INFO_MACRO_EDID - | LIBGAMMA_CRTC_INFO_MACRO_VIEWPORT - | LIBGAMMA_CRTC_INFO_MACRO_RAMP - | LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER - | LIBGAMMA_CRTC_INFO_ACTIVE - | LIBGAMMA_CRTC_INFO_MACRO_CONNECTOR; - /* DRM supports multiple partitions and CRTC:s but not sites */ - this->default_site_known = 1; - this->multiple_sites = 0; - this->multiple_partitions = 1; - this->multiple_crtcs = 1; - /* Partitions are graphics cards in DRM */ - this->partitions_are_graphics_cards = 1; - /* Linux does not have system restore capabilities */ - this->site_restore = 0; - this->partition_restore = 0; - this->crtc_restore = 0; - /* Gamma ramp sizes are identical but not fixed */ - this->identical_gamma_sizes = 1; - this->fixed_gamma_size = 0; - /* Gamma ramp depths are fixed */ - this->fixed_gamma_depth = 1; - /* DRM is a real non-faked adjustment method */ - this->real = 1; - this->fake = 0; - /* Gamma ramp adjustments are persistent */ - this->auto_restore = 0; -} - - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_linux_drm_site_initialise(libgamma_site_state_t *restrict this, char *restrict site) -{ - char pathname[PATH_MAX]; - struct stat _attr; - - if (site) - return LIBGAMMA_NO_SUCH_SITE; - - /* Count the number of available graphics cards by - `stat`:ing their existence in an API filesystem */ - this->partitions_available = 0; - for (;;) { - /* Construct pathname of graphics card device */ - snprintf(pathname, sizeof(pathname) / sizeof(char), - DRM_DEV_NAME, DRM_DIR_NAME, (int)(this->partitions_available)); - /* `stat` the graphics card's existence */ - if (stat(pathname, &_attr)) - break; - /* Move on to next graphics card */ - if (this->partitions_available++ > INT_MAX) - return LIBGAMMA_IMPOSSIBLE_AMOUNT; - } - return 0; -} - - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void -libgamma_linux_drm_site_destroy(libgamma_site_state_t *restrict this) -{ - (void) this; -} - - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_linux_drm_site_restore(libgamma_site_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - -/** - * Figure out why `open` failed for a graphics card - * - * @param pathname The pathname of the error card - * @return The error code to report - */ -static int -figure_out_card_open_error(const char *pathname) -{ - gid_t supplemental_groups[NGROUPS_MAX]; - struct group *group; - struct stat attr; - int i, n; - - - /* Check which the device exists */ - if (errno == ENXIO || errno == ENODEV) - return LIBGAMMA_NO_SUCH_PARTITION; - - - /* If we did not get access permission, figure out why */ - - if (errno != EACCES) { - /* If we could not figure out what went - * wrong, just return the error we got */ - return LIBGAMMA_ERRNO_SET; - } - -#define __test(R, W) ((attr.st_mode & ((R) | (W))) == ((R) | (W))) - - /* Get permission requirement for the file */ - if (stat(pathname, &attr)) - return errno == EACCES ? LIBGAMMA_NO_SUCH_PARTITION : LIBGAMMA_ERRNO_SET; - - /* Test owner's, group's and others' permissions */ - if ((attr.st_uid == geteuid() && __test(S_IRUSR, S_IWUSR)) || - (attr.st_gid == getegid() && __test(S_IRGRP, S_IWGRP)) || __test(S_IROTH, S_IWOTH)) - return LIBGAMMA_DEVICE_ACCESS_FAILED; - - /* The group should be "video", but perhaps - it is "root" to restrict users */ - if (!attr.st_gid /* root group */ || __test(S_IRGRP, S_IWGRP)) - return LIBGAMMA_DEVICE_RESTRICTED; - - - /* Get the user's supplemental group membership list */ - n = getgroups(NGROUPS_MAX, supplemental_groups); - if (n < 0) - return LIBGAMMA_ERRNO_SET; - - /* Test whether any of the supplemental - group should be satisfactory */ - for (i = 0; i < n; i++) - if (supplemental_groups[i] == attr.st_gid) - break; - - /* If one of the supplemental groups should be satisfactory, - then we do not know anything more than that access failed */ - if (i != n) - return LIBGAMMA_DEVICE_ACCESS_FAILED; - - /* Otherwise, try to get the name of the group that is - required and report the missing group membership */ - { - static __thread char buf[1024]; /* My output of `sysconf(_SC_GETGR_R_SIZE_MAX)`. */ - struct group _grp; - - errno = getgrgid_r(attr.st_gid, &_grp, buf, sizeof(buf) / sizeof(char), &group); - if (errno == ERANGE) - { - /* The lenght of the group's name is absurdly long, degrade to thread-unsafe. */ - errno = 0; - group = getgrgid(attr.st_gid); - } - else if (errno) - return LIBGAMMA_ERRNO_SET; - } - - libgamma_group_gid = attr.st_gid; - libgamma_group_name = group ? group->gr_name : NULL; - return LIBGAMMA_DEVICE_REQUIRE_GROUP; -#undef __test -} - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_linux_drm_partition_initialise(libgamma_partition_state_t *restrict this, - libgamma_site_state_t *restrict site, size_t partition) -{ - int rc = 0; - libgamma_drm_card_data_t *restrict data; - char pathname[PATH_MAX]; - - (void) site; - - /* Check for partition index overflow */ - if (partition > INT_MAX) - return LIBGAMMA_NO_SUCH_PARTITION; - - /* Allocate and initialise graphics card data */ - this->data = NULL; - data = malloc(sizeof(libgamma_drm_card_data_t)); - if (!data) - return LIBGAMMA_ERRNO_SET; - data->fd = -1; - data->res = NULL; - data->encoders = NULL; - data->connectors = NULL; - - /* Get the pathname for the graphics card */ - snprintf(pathname, sizeof(pathname) / sizeof(char), DRM_DEV_NAME, DRM_DIR_NAME, (int)partition); - - /* Acquire access to the graphics card */ - data->fd = open(pathname, O_RDWR | O_CLOEXEC); - if (data->fd < 0) { - rc = figure_out_card_open_error(pathname); - goto fail_data; - } - - /* Acquire mode resources */ - data->res = drmModeGetResources(data->fd); - if (!data->res) { - rc = LIBGAMMA_ACQUIRING_MODE_RESOURCES_FAILED; - goto fail_fd; - } - - /* Get the number of CRTC:s that are available in the partition */ - if (data->res->count_crtcs < 0) { - rc = LIBGAMMA_NEGATIVE_CRTC_COUNT; - goto fail_res; - } - this->crtcs_available = (size_t)data->res->count_crtcs; - this->data = data; - return 0; - -fail_res: - drmModeFreeResources(data->res); -fail_fd: - close(data->fd); -fail_data: - free(data); - return rc; -} - - -/** - * Release all connectors and encoders - * - * @param this The graphics card data - */ -static void -release_connectors_and_encoders(libgamma_drm_card_data_t *restrict this) -{ - size_t i, n; - - /* Release individual encoders */ - if (this->encoders) - for (i = 0, n = (size_t)(this->res->count_connectors); i < n; i++) - if (this->encoders[i]) - drmModeFreeEncoder(this->encoders[i]); - /* Release encoder array */ - free(this->encoders); - this->encoders = NULL; - - /* Release individual connectors */ - if (this->connectors) - for (i = 0, n = (size_t)(this->res->count_connectors); i < n; i++) - if (this->connectors[i]) - drmModeFreeConnector(this->connectors[i]); - /* Release connector array */ - free(this->connectors); - this->connectors = NULL; -} - - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void -libgamma_linux_drm_partition_destroy(libgamma_partition_state_t *restrict this) -{ - libgamma_drm_card_data_t *restrict data = this->data; - release_connectors_and_encoders(data); - if (data->res) - drmModeFreeResources(data->res); - if (data->fd >= 0) - close(data->fd); - free(data); -} - - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings. - * - * @param this The partition state. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. - */ -int -libgamma_linux_drm_partition_restore(libgamma_partition_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_linux_drm_crtc_initialise(libgamma_crtc_state_t *restrict this, - libgamma_partition_state_t *restrict partition, size_t crtc) -{ - libgamma_drm_card_data_t *restrict card = partition->data; - - if (crtc >= partition->crtcs_available) - return LIBGAMMA_NO_SUCH_CRTC; - this->data = (void*)(size_t)card->res->crtcs[crtc]; - return 0; -} - - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void -libgamma_linux_drm_crtc_destroy(libgamma_crtc_state_t *restrict this) -{ - (void) this; -} - - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC. - * - * @param this The CRTC state. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. - */ -int -libgamma_linux_drm_crtc_restore(libgamma_crtc_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - -/** - * Find the connector that a CRTC belongs to - * - * @param this The CRTC state - * @param error Output of the error value to store of error report - * fields for data that requires the connector -'* @return The CRTC's conncetor, `NULL` on error - */ -static drmModeConnector * -find_connector(libgamma_crtc_state_t *restrict this, int *restrict error) -{ - uint32_t crtc_id = (uint32_t)(size_t)this->data; - libgamma_drm_card_data_t *restrict card = this->partition->data; - size_t i, n = (size_t)card->res->count_connectors; - /* Open connectors and encoders if not already opened */ - if (!card->connectors) { - /* Allocate connector and encoder arrays; we use `calloc` - so all non-loaded elements are `NULL` after an error */ - card->connectors = calloc(n, sizeof(drmModeConnector *)); - if (!card->connectors) - goto fail; - card->encoders = calloc(n, sizeof(drmModeEncoder *)); - if (!card->encoders) - goto fail; - /* Fill connector and encoder arrays */ - for (i = 0; i < n; i++) { - /* Get connector */ - card->connectors[i] = drmModeGetConnector(card->fd, card->res->connectors[i]); - if (!card->connectors[i]) - goto fail; - /* Get encoder if the connector is enabled. If it is disabled it - will not have an encoder, which is indicated by the encoder - ID being 0. In such case, leave the encoder to be `NULL`. */ - if (card->connectors[i]->encoder_id) { - card->encoders[i] = drmModeGetEncoder(card->fd, card->connectors[i]->encoder_id); - if (!card->encoders[i]) - goto fail; - } - } - } - /* No error has occurred yet */ - *error = 0; - /* Find connector */ - for (i = 0; i < n; i++) - if (card->encoders[i] && card->connectors[i] && card->encoders[i]->crtc_id == crtc_id) - return card->connectors[i]; - /* We did not find the connector */ - *error = LIBGAMMA_CONNECTOR_UNKNOWN; - return NULL; - -fail: - /* Report the error that got us here, release - resouces and exit with `NULL` for failure */ - *error = errno; - release_connectors_and_encoders(card); - return NULL; -} - - -/** - * Get the size of the gamma ramps for a CRTC - * - * @param out Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @return The value stored in `out->gamma_size_error` - */ -static int -get_gamma_ramp_size(libgamma_crtc_information_t *restrict out, const libgamma_crtc_state_t *restrict crtc) -{ - libgamma_drm_card_data_t *restrict card = crtc->partition->data; - uint32_t crtc_id = card->res->crtcs[crtc->crtc]; - drmModeCrtc *restrict crtc_info; - /* Get CRTC information */ - errno = 0; - crtc_info = drmModeGetCrtc(card->fd, crtc_id); - out->gamma_size_error = crtc_info ? 0 : errno; - /* Get gamma ramp size */ - if (!out->gamma_size_error) { - /* Store gamma ramp size */ - out->red_gamma_size = out->green_gamma_size = out->blue_gamma_size = (size_t)crtc_info->gamma_size; - /* Sanity check gamma ramp size */ - out->gamma_size_error = crtc_info->gamma_size < 2 ? LIBGAMMA_SINGLETON_GAMMA_RAMP : 0; - /* Release CRTC information */ - drmModeFreeCrtc(crtc_info); - } - return out->gamma_size_error; -} - - -/** - * Get the a monitor's subpixel order - * - * @param out Instance of a data structure to fill with the information about the CRTC - * @param connector The connector - */ -static void -get_subpixel_order(libgamma_crtc_information_t *restrict out, const drmModeConnector *restrict connector) -{ -#define __select(value)\ - case DRM_MODE_SUBPIXEL_##value:\ - out->subpixel_order = LIBGAMMA_SUBPIXEL_ORDER_##value;\ - break - - switch (connector->subpixel) { - __select (UNKNOWN); - __select (HORIZONTAL_RGB); - __select (HORIZONTAL_BGR); - __select (VERTICAL_RGB); - __select (VERTICAL_BGR); - __select (NONE); - default: - out->subpixel_order_error = LIBGAMMA_SUBPIXEL_ORDER_NOT_RECOGNISED; - break; - } - -#undef __select -} - - -/** - * Get a connector's type - * - * @param out Instance of a data structure to fill with the information about the CRTC - * @param connector The connector - * @param connector_name_base Output for the basename of the connector - */ -static void -get_connector_type(libgamma_crtc_information_t *restrict out, const drmModeConnector *restrict connector, - const char **restrict connector_name_base) -{ -#define __select(type, name)\ - case DRM_MODE_CONNECTOR_##type:\ - out->connector_type = LIBGAMMA_CONNECTOR_TYPE_##type;\ - *connector_name_base = name;\ - break - - /* These may not have been included by <xf86drmMode.h>, - but they should be available. Becuase we define them - outself, it is best to test them last. */ -#ifndef DRM_MODE_CONNECTOR_VIRTUAL -# define DRM_MODE_CONNECTOR_VIRTUAL 15 -#endif -#ifndef DRM_MODE_CONNECTOR_DSI -# define DRM_MODE_CONNECTOR_DSI 16 -#endif - - /* Translate connector type from DRM to libgamma - and store connector basename */ - switch (connector->connector_type) { - __select (Unknown, "Unknown" ); - __select (VGA, "VGA" ); - __select (DVII, "DVI-I" ); - __select (DVID, "DVI-D" ); - __select (DVIA, "DVI-A" ); - __select (Composite, "Composite"); - __select (SVIDEO, "SVIDEO" ); - __select (LVDS, "LVDS" ); - __select (Component, "Component"); - __select (9PinDIN, "DIN" ); - __select (DisplayPort, "DP" ); - __select (HDMIA, "HDMI-A" ); - __select (HDMIB, "HDMI-B" ); - __select (TV, "TV" ); - __select (eDP, "eDP" ); - __select (VIRTUAL, "VIRTUAL" ); - __select (DSI, "DSI" ); - default: - out->connector_type_error = LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED; - out->connector_name_error = LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED; - break; - } - -#undef __select -} - - -/** - * Read information from the CRTC's conncetor - * - * @param crtc The state of the CRTC whose information should be read - * @param out Instance of a data structure to fill with the information about the CRTC - * @param connector The CRTC's connector - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Non-zero if at least on error occured - */ -static int -read_connector_data(libgamma_crtc_state_t *restrict crtc, libgamma_crtc_information_t *restrict out, - const drmModeConnector *restrict connector, int32_t fields) -{ - const char *connector_name_base = NULL; - libgamma_drm_card_data_t *restrict card; - uint32_t type; - size_t i, n, c; - - /* Get some information that does not require too much work */ - if (fields & (LIBGAMMA_CRTC_INFO_MACRO_ACTIVE | LIBGAMMA_CRTC_INFO_MACRO_CONNECTOR)) { - /* Get whether or not a monitor is plugged in */ - out->active = connector->connection == DRM_MODE_CONNECTED; - out->active_error = connector->connection == DRM_MODE_UNKNOWNCONNECTION ? LIBGAMMA_STATE_UNKNOWN : 0; - if (!out->active) { - if (fields & (LIBGAMMA_CRTC_INFO_MACRO_VIEWPORT | LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER)) - out->width_mm_error = out->height_mm_error = out->subpixel_order_error = LIBGAMMA_NOT_CONNECTED; - goto not_connected; - } - - /* Get viewport dimension */ - out->width_mm = connector->mmWidth; - out->height_mm = connector->mmHeight; - - /* Get subpixel order */ - get_subpixel_order(out, connector); - - not_connected: - - /* Get connector type */ - get_connector_type(out, connector, &connector_name_base); - } - - /* Get the connector's name */ - if ((fields & LIBGAMMA_CRTC_INFO_CONNECTOR_NAME) && !out->connector_name_error) { - card = crtc->partition->data; - type = connector->connector_type; - n = (size_t)card->res->count_connectors; - - /* Allocate memory for the name of the connector */ - out->connector_name = malloc((strlen(connector_name_base) + 12) * sizeof(char)); - if (!out->connector_name) - return (out->connector_name_error = errno); - - /* Get the number of connectors with the same type on the same graphics card */ - for (i = c = 0; i < n && card->connectors[i] != connector; i++) - if (card->connectors[i]->connector_type == type) - c++; - - /* Construct and store connect name that is unique to the graphics card */ - sprintf(out->connector_name, "%s-%" PRIu32, connector_name_base, (uint32_t)(c + 1)); - } - - /* Did something go wrong? */ - return out->subpixel_order_error | out->active_error | out->connector_name_error; -} - - -/** - * Get the extended display identification data for a monitor - * - * @param crtc The CRTC state - * @param out Instance of a data structure to fill with the information about the CRTC - * @param connector The CRTC's connector - * @reutnr Non-zero on error - */ -static int -get_edid(libgamma_crtc_state_t *restrict crtc, libgamma_crtc_information_t *restrict out, drmModeConnector *connector) -{ - libgamma_drm_card_data_t *restrict card = crtc->partition->data; - int prop_n = connector->count_props; - int prop_i; - drmModePropertyRes *restrict prop; - drmModePropertyBlobRes *restrict blob; - - /* Test all properies on the connector */ - for (prop_i = 0; prop_i < prop_n; prop_i++) { - /* Get output property */ - prop = drmModeGetProperty(card->fd, connector->props[prop_i]); - if (!prop) - continue; - /* Is this property the EDID? */ - if (!strcmp(prop->name, "EDID")) { - /* Get the property value */ - blob = drmModeGetPropertyBlob(card->fd, (uint32_t)connector->prop_values[prop_i]); - if (!blob) { - drmModeFreeProperty(prop); - return (out->edid_error = LIBGAMMA_PROPERTY_VALUE_QUERY_FAILED); - } - if (blob->data) { - /* Get and store the length of the EDID */ - out->edid_length = blob->length; - /* Allocate memory for a copy of the EDID that is under our memory control */ - out->edid = malloc(out->edid_length * sizeof(unsigned char)); - if (!out->edid) { - out->edid_error = errno; - } else { - /* Copy the EDID so we can free resources that got us here */ - memcpy(out->edid, blob->data, (size_t)out->edid_length * sizeof(char)); - } - /* Free the propriety value and the propery */ - drmModeFreePropertyBlob(blob); - drmModeFreeProperty(prop); - /* Were we successful? */ - return !out->edid; - } - /* Free the propriety value */ - drmModeFreePropertyBlob(blob); - } - /* Free the propriety */ - drmModeFreeProperty(prop); - } - /* If we get here, we did not find a EDID */ - return (out->edid_error = LIBGAMMA_EDID_NOT_FOUND); -} - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error; on error refer to the error reports in `this` - */ -int -libgamma_linux_drm_get_crtc_information(libgamma_crtc_information_t *restrict this, - libgamma_crtc_state_t *restrict crtc, int32_t fields) -{ -#define _E(FIELD) ((fields & FIELD) ? LIBGAMMA_CRTC_INFO_NOT_SUPPORTED : 0) - - int e = 0, require_connector, free_edid, error; - drmModeConnector *restrict connector; - - /* Wipe all error indicators */ - memset(this, 0, sizeof(libgamma_crtc_information_t)); - - /* We need to free the EDID after us if it is not explicitly requested */ - free_edid = !(fields & LIBGAMMA_CRTC_INFO_EDID); - - /* Figure out whether we require the connector to get all information we want */ - require_connector = fields & (LIBGAMMA_CRTC_INFO_MACRO_ACTIVE | LIBGAMMA_CRTC_INFO_MACRO_CONNECTOR); - - /* If we are not interested in the connector or monitor, jump */ - if (!require_connector) - goto cont; - /* Find connector. */ - connector = find_connector(crtc, &error); - if (!connector) { - /* Store reported error in affected fields */ - e |= this->width_mm_error = this->height_mm_error - = this->connector_type_error = this->subpixel_order_error - = this->active_error = this->connector_name_error - = this->edid_error = this->gamma_error - = this->width_mm_edid_error = this->height_mm_edid_error = error; - goto cont; - } - - /* Read connector data and monitor data, excluding EDID */ - e |= read_connector_data(crtc, this, connector, fields); - - /* If we do not want any EDID information, jump */ - if (!(fields & LIBGAMMA_CRTC_INFO_MACRO_EDID)) - goto cont; - /* If there is not monitor that report error in EDID related fields */ - if (this->active_error || !this->active) { - e |= this->edid_error = this->gamma_error - = this->width_mm_edid_error = this->height_mm_edid_error - = LIBGAMMA_NOT_CONNECTED; - goto cont; - } - /* Get EDID */ - e |= get_edid(crtc, this, connector); - if (!this->edid) { - this->gamma_error = this->width_mm_edid_error = this->height_mm_edid_error = this->edid_error; - goto cont; - } - /* Parse EDID */ - if (fields & (LIBGAMMA_CRTC_INFO_MACRO_EDID ^ LIBGAMMA_CRTC_INFO_EDID)) - e |= libgamma_parse_edid(this, fields); - -cont: - /* Get gamma ramp size */ - e |= (fields & LIBGAMMA_CRTC_INFO_GAMMA_SIZE) ? get_gamma_ramp_size(this, crtc) : 0; - /* Store gamma ramp depth */ - this->gamma_depth = 16; - /* DRM does not support quering gamma ramp support */ - e |= this->gamma_support_error = _E(LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT); - - /* Free the EDID after us */ - if (free_edid) { - free(this->edid); - this->edid = NULL; - } - - return e ? -1 : 0; - -#undef _E -} - - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_linux_drm_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t *restrict ramps) -{ - libgamma_drm_card_data_t *restrict card = this->partition->data; - int r; -#ifdef DEBUG - /* Gamma ramp sizes are identical but not fixed */ - if (ramps->red_size != ramps->green_size || ramps->red_size != ramps->blue_size) - return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; -#endif - /* Read current gamma ramps */ - r = drmModeCrtcGetGamma(card->fd, (uint32_t)(size_t)this->data, (uint32_t)ramps->red_size, - ramps->red, ramps->green, ramps->blue); - return r ? LIBGAMMA_GAMMA_RAMP_READ_FAILED : 0; -} - - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_linux_drm_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t ramps) -{ - libgamma_drm_card_data_t *restrict card = this->partition->data; - int r; -#ifdef DEBUG - /* Gamma ramp sizes are identical but not fixed */ - if (ramps.red_size != ramps.green_size || ramps.red_size != ramps.blue_size) - return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; -#endif - - /* Apply gamma ramps */ - r = drmModeCrtcSetGamma(card->fd, (uint32_t)(size_t)this->data, - (uint32_t)ramps.red_size, ramps.red, ramps.green, ramps.blue); - /* Check for errors */ - if (r) { - switch (errno) { - case EACCES: - case EAGAIN: - case EIO: - /* Permission denied errors must be ignored, because we do not - * have permission to do this while a display server is active. - * We are also checking for some other error codes just in case. */ - case EBUSY: - case EINPROGRESS: - /* It is hard to find documentation for DRM (in fact all of this is - * just based on the functions names and some testing,) perhaps we - * could get this if we are updating to fast. */ - break; - case EBADF: - case ENODEV: - case ENXIO: - /* XXX: I have not actually tested removing my graphics card or, - * monitor but I imagine either of these is what would happen. */ - return LIBGAMMA_GRAPHICS_CARD_REMOVED; - - default: - return LIBGAMMA_ERRNO_SET; - } - } - return 0; -} diff --git a/src/lib/gamma-linux-drm.h b/src/lib/gamma-linux-drm.h deleted file mode 100644 index 41a654d..0000000 --- a/src/lib/gamma-linux-drm.h +++ /dev/null @@ -1,139 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_GAMMA_LINUX_DRM_H -#define LIBGAMMA_GAMMA_LINUX_DRM_H - -#ifndef HAVE_LIBGAMMA_METHOD_LINUX_DRM -# error Including gamma-linux-drm.h without HAVE_LIBGAMMA_METHOD_LINUX_DRM -#endif - - -#include "libgamma-method.h" - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void libgamma_linux_drm_method_capabilities(libgamma_method_capabilities_t *restrict); - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_linux_drm_site_initialise(libgamma_site_state_t *restrict, char *restrict); - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void libgamma_linux_drm_site_destroy(libgamma_site_state_t *restrict) __attribute__((const)); - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_linux_drm_site_restore(libgamma_site_state_t *restrict); - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_linux_drm_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void libgamma_linux_drm_partition_destroy(libgamma_partition_state_t *restrict); - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_linux_drm_partition_restore(libgamma_partition_state_t *restrict); - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_linux_drm_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void libgamma_linux_drm_crtc_destroy(libgamma_crtc_state_t *restrict) __attribute__((const)); - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_linux_drm_crtc_restore(libgamma_crtc_state_t *restrict); - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error; on error refer to the error reports in `this` - */ -int libgamma_linux_drm_get_crtc_information(libgamma_crtc_information_t *restrict, libgamma_crtc_state_t *restrict, int32_t); - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_linux_drm_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_linux_drm_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t); - - -#endif diff --git a/src/lib/gamma-quartz-cg.c b/src/lib/gamma-quartz-cg.c deleted file mode 100644 index b0e3a29..0000000 --- a/src/lib/gamma-quartz-cg.c +++ /dev/null @@ -1,359 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef HAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS -# error Compiling gamma-quartz-cg.c without HAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS -#endif - -#include "gamma-quartz-cg.h" - -#include "libgamma-error.h" - -#ifdef FAKE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS -# include "fake-quartz-cg.h" -#else -# include <ApplicationServices/ApplicationServices.h> -# include <CoreGraphics/CGDirectDisplay.h> -# define close_fake_quartz_cg() /* For compatibility with "fake-quartz-cg.h" */ -#endif - -#include <stdlib.h> -#include <errno.h> - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void -libgamma_quartz_cg_method_capabilities(libgamma_method_capabilities_t* restrict this) -{ - /* Gamma ramps size and depth can be queried */ - this->crtc_information = LIBGAMMA_CRTC_INFO_GAMMA_SIZE | LIBGAMMA_CRTC_INFO_GAMMA_DEPTH; - /* Quartz/CoreGraphics does not support sites or partitions */ - this->default_site_known = 1; - this->multiple_sites = 0; - this->multiple_partitions = 0; - /* Quartz/CoreGraphics does support CRTC:s */ - this->multiple_crtcs = 1; - /* Partitions are not supported... */ - this->partitions_are_graphics_cards = 0; - /* CoreGraphics have support for system restore */ - this->site_restore = 1; - this->partition_restore = 1; - /* But not for individual CRTC:s */ - this->crtc_restore = 0; - /* Gamma ramp sizes are identifical but not fixed */ - this->identical_gamma_sizes = 1; - this->fixed_gamma_size = 0; - /* Gamma ramp depths are fixed */ - this->fixed_gamma_depth = 1; - /* Quartz/CoreGraphics is a real adjustment method that can be faked */ -#ifdef FAKE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS - /* It is considered real but fake if it is translated to X RandR */ - this->fake = 1; -# ifdef HAVE_LIBGAMMA_METHOD_X_RANDR - this->real = 1; -# else - this->real = 0; -# endif -#else - /* It is real and not fake if we are running on Mac OS X */ - this->fake = 0; - this->real = 1; -#endif - /* Gamma ramp adjustments are non-persistent */ - this->auto_restore = 1; -} - - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_quartz_cg_site_initialise(libgamma_site_state_t *restrict this, char *restrict site) -{ - this->partitions_available = 1; - return site ? 0 : LIBGAMMA_NO_SUCH_SITE; -} - - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void -libgamma_quartz_cg_site_destroy(libgamma_site_state_t *restrict this) -{ - (void) this; - close_fake_quartz_cg(); -} - - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_quartz_cg_site_restore(libgamma_site_state_t *restrict this) -{ - (void) this; - CGDisplayRestoreColorSyncSettings(); - return 0; -} - - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_quartz_cg_partition_initialise(libgamma_partition_state_t *restrict this, - libgamma_site_state_t *restrict site, size_t partition) -{ - CGDirectDisplayID *crtcs, *crtcs_old; - uint32_t cap = 4, n; - - (void) site; - - this->data = NULL; - - if (partition) - return LIBGAMMA_NO_SUCH_PARTITION; - - /* Allocate array of CRTC ID:s */ - crtcs = malloc((size_t)cap * sizeof(CGDirectDisplayID)); - if (!crtcs) - return LIBGAMMA_ERRNO_SET; - - /* It is not possible to ask CoreGraphics how many CRTC:s are - * available. We have to ask it to give us a ID:s of a number - * of CRTC:s and ask for more if we got as many as we asked for. */ - for (;;) { - /* Ask for CRTC ID:s */ - if (CGGetOnlineDisplayList(cap, crtcs, &n) != kCGErrorSuccess) - return free(crtcs), LIBGAMMA_LIST_CRTCS_FAILED; - /* If we did not get as many as we asked for then we have all */ - if (n < cap) - break; - /* Increase the number CRTC ID:s to ask for */ - if (cap > SIZE_MAX / 2) /* We could also test ~0, but it is still too many */ - return free(crtcs), LIBGAMMA_IMPOSSIBLE_AMOUNT; - cap <<= 1; - /* Grow the array of CRTC ID:s so that it can fit all we are asking for */ - crtcs = realloc(crtcs_old = crtcs, (size_t)cap * sizeof(CGDirectDisplayID)); - if (!crtcs) { - free(crtcs_old); - return LIBGAMMA_ERRNO_SET; - } - } - - /* Store CRTC ID:s and CRTC count */ - this->data = crtcs; - this->crtcs_available = (size_t)n; - return 0; -} - - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void -libgamma_quartz_cg_partition_destroy(libgamma_partition_state_t *restrict this) -{ - free(this->data); -} - - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_quartz_cg_partition_restore(libgamma_partition_state_t *restrict this) -{ - return libgamma_quartz_cg_site_restore(this->site); -} - - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_quartz_cg_crtc_initialise(libgamma_crtc_state_t *restrict this, - libgamma_partition_state_t *restrict partition, size_t crtc) -{ - (void) this; - return crtc < partition->crtcs_available ? 0 : LIBGAMMA_NO_SUCH_CRTC; -} - - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void -libgamma_quartz_cg_crtc_destroy(libgamma_crtc_state_t *restrict this) -{ - (void) this; -} - - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_quartz_cg_crtc_restore(libgamma_crtc_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read. - * @return Zero on success, -1 on error. On error refer to the error reports in `this` - */ -int -libgamma_quartz_cg_get_crtc_information(libgamma_crtc_information_t *restrict this, - libgamma_crtc_state_t *restrict crtc, int32_t fields) -{ -#define SUPPORTED_FIELDS (LIBGAMMA_CRTC_INFO_GAMMA_SIZE | LIBGAMMA_CRTC_INFO_GAMMA_DEPTH) -#define _E(FIELD) ((fields & FIELD) ? LIBGAMMA_CRTC_INFO_NOT_SUPPORTED : 0) - - CGDirectDisplayID *restrict crtcs; - size_t gamma_size; - - /* Quartz/CoreGraphics does not support EDID or monitor dimensions */ - this->edid_error = _E(LIBGAMMA_CRTC_INFO_EDID); - this->width_mm_error = _E(LIBGAMMA_CRTC_INFO_WIDTH_MM); - this->height_mm_error = _E(LIBGAMMA_CRTC_INFO_HEIGHT_MM); - this->width_mm_edid_error = _E(LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID); - this->height_mm_edid_error = _E(LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID); - /* Quartz/CoreGraphics does support gamma ramp size query - The gamma ramps are identical but not fixed, and the query can fail */ - this->gamma_size_error = 0; - if ((fields & LIBGAMMA_CRTC_INFO_GAMMA_SIZE)) { - crtcs = crtc->partition->data; - gamma_size = CGDisplayGammaTableCapacity(crtcs[crtc->crtc]); - this->red_gamma_size = this->green_gamma_size = this->blue_gamma_size = (size_t)gamma_size; - this->gamma_size_error = gamma_size < 2 ? LIBGAMMA_SINGLETON_GAMMA_RAMP : 0; - } - /* Quartz/CoreGraphics uses `float` ramps */ - this->gamma_depth = -1; - this->gamma_depth_error = 0; - /* Quartz/CoreGraphics does not support gamma ramp support queries */ - this->gamma_support_error = _E(LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT); - /* Quartz/CoreGraphics does not support EDID or connector information */ - this->subpixel_order_error = _E(LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER); - this->active_error = _E(LIBGAMMA_CRTC_INFO_ACTIVE); - this->connector_name_error = _E(LIBGAMMA_CRTC_INFO_CONNECTOR_NAME); - this->connector_type_error = _E(LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE); - this->gamma_error = _E(LIBGAMMA_CRTC_INFO_GAMMA); - - /* We failed if gamma ramp size query failed or if an unsupport field was queried */ - return (this->gamma_size_error || (fields & ~SUPPORTED_FIELDS)) ? -1 : 0; - -#undef _E -#undef SUPPORTED_FIELDS -} - - -/** - * Get the current gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_quartz_cg_crtc_get_gamma_rampsf(libgamma_crtc_state_t *restrict this, libgamma_gamma_rampsf_t *restrict ramps) -{ - CGDirectDisplayID *restrict crtcs = this->partition->data; - CGDirectDisplayID crtc_id = crtcs[this->crtc]; - uint32_t gamma_size_out; - CGError r; -#ifdef DEBUG - /* Gamma ramps sizes are identical but not fixed */ - if (ramps->red_size != ramps->green_size || ramps->red_size != ramps->blue_size) - return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; -#endif - /* Read current gamma ramps */ - r = CGGetDisplayTransferByTable(crtc_id, (uint32_t)ramps->red_size, ramps->red, ramps->green, ramps->blue, &gamma_size_out); - if (r != kCGErrorSuccess) - return LIBGAMMA_GAMMA_RAMP_READ_FAILED; - /* I hope that it will not actually ever change, - but it does return the the gamma ramp size despite - that it can be queried without querying for more */ - if (gamma_size_out != ramps->red_size) - return LIBGAMMA_GAMMA_RAMP_SIZE_CHANGED; - return 0; -} - - -/** - * Set the gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_quartz_cg_crtc_set_gamma_rampsf(libgamma_crtc_state_t *restrict this, libgamma_gamma_rampsf_t ramps) -{ - CGDirectDisplayID *restrict crtcs = this->partition->data; - CGDirectDisplayID crtc_id = crtcs[this->crtc]; - CGError r; -#ifdef DEBUG - /* Gamma ramps sizes are identical but not fixed */ - if (ramps.red_size != ramps.green_size || ramps.red_size != ramps.blue_size) - return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; -#endif - /* Apply gamma ramps */ - r = CGSetDisplayTransferByTable(crtc_id, (uint32_t)ramps.red_size, ramps.red, ramps.green, ramps.blue); - return r == kCGErrorSuccess ? 0 : LIBGAMMA_GAMMA_RAMP_WRITE_FAILED; -} diff --git a/src/lib/gamma-quartz-cg.h b/src/lib/gamma-quartz-cg.h deleted file mode 100644 index 211e055..0000000 --- a/src/lib/gamma-quartz-cg.h +++ /dev/null @@ -1,140 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_GAMMA_QUARTZ_CG_H -#define LIBGAMMA_GAMMA_QUARTZ_CG_H - -#ifndef HAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS -# error Including gamma-quartz-cg.h without HAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS -#endif - - -#include "libgamma-method.h" - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void libgamma_quartz_cg_method_capabilities(libgamma_method_capabilities_t *restrict); - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_quartz_cg_site_initialise(libgamma_site_state_t *restrict, char *restrict); - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void libgamma_quartz_cg_site_destroy(libgamma_site_state_t *restrict); - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_quartz_cg_site_restore(libgamma_site_state_t *restrict); - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_quartz_cg_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void libgamma_quartz_cg_partition_destroy(libgamma_partition_state_t *restrict); - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_quartz_cg_partition_restore(libgamma_partition_state_t *restrict); - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -__attribute__((pure)) -int libgamma_quartz_cg_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void libgamma_quartz_cg_crtc_destroy(libgamma_crtc_state_t *restrict) __attribute__((const)); - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_quartz_cg_crtc_restore(libgamma_crtc_state_t *restrict); - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error. On error refer to the error reports in `this` - */ -int libgamma_quartz_cg_get_crtc_information(libgamma_crtc_information_t *restrict, libgamma_crtc_state_t *restrict, int32_t); - -/** - * Get the current gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_quartz_cg_crtc_get_gamma_rampsf(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsf_t *restrict); - -/** - * Set the gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_quartz_cg_crtc_set_gamma_rampsf(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsf_t); - - -#endif diff --git a/src/lib/gamma-w32-gdi.c b/src/lib/gamma-w32-gdi.c deleted file mode 100644 index e93b53d..0000000 --- a/src/lib/gamma-w32-gdi.c +++ /dev/null @@ -1,360 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef HAVE_LIBGAMMA_METHOD_W32_GDI -# error Compiling gamma-w32-gdi.c without HAVE_LIBGAMMA_METHOD_W32_GDI -#endif - -#include "gamma-w32-gdi.h" - -#include "libgamma-error.h" - -#ifndef WINVER -# define WINVER 0x0500 -#endif -#ifdef FAKE_LIBGAMMA_METHOD_W32_GDI -# include "fake-w32-gdi.h" -#else -# include <windows.h> -# include <wingdi.h> -#endif - -#include <errno.h> - - -/** - * The gamma ramp size that devices will always have in Windows GDI - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd372194(v=vs.85).aspx - */ -#define GAMMA_RAMP_SIZE 256 - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void -libgamma_w32_gdi_method_capabilities(libgamma_method_capabilities_t *restrict this) -{ - /* Gamma ramps size, depth and support can be queried */ - this->crtc_information = LIBGAMMA_CRTC_INFO_GAMMA_SIZE | LIBGAMMA_CRTC_INFO_GAMMA_DEPTH; - /* Windows GDI does not support sites or partitions */ - this->default_site_known = 1; - this->multiple_sites = 0; - this->multiple_partitions = 0; - /* Windows GDI does support CRTC:s */ - this->multiple_crtcs = 1; - /* Partitions are not supported... */ - this->partitions_are_graphics_cards = 0; - /* Windows GDI does not have system restore capabilities */ - this->site_restore = 0; - this->partition_restore = 0; - this->crtc_restore = 0; - /* Ramps sizes are fixed and identical and ramp depth is too */ - this->identical_gamma_sizes = 1; - this->fixed_gamma_size = 1; - this->fixed_gamma_depth = 1; - /* Windows GDI is a real adjustment method that can be faked */ -#ifdef FAKE_LIBGAMMA_METHOD_W32_GDI - /* It is considered real but fake if it is translated to X RandR */ - this->fake = 1; -# ifdef HAVE_LIBGAMMA_METHOD_X_RANDR - this->real = 1; -# else - this->real = 0; -# endif -#else - /* It is real and not fake if we are running on Windows */ - this->fake = 0; - this->real = 1; -#endif - /* Gamma ramp adjustments are persistent */ - this->auto_restore = 0; -} - - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_w32_gdi_site_initialise(libgamma_site_state_t *restrict this, char* restrict site) -{ - this->partitions_available = 1; - return !site ? 0 : LIBGAMMA_NO_SUCH_SITE; -} - - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void -libgamma_w32_gdi_site_destroy(libgamma_site_state_t *restrict this) -{ - (void) this; -} - - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_w32_gdi_site_restore(libgamma_site_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_w32_gdi_partition_initialise(libgamma_partition_state_t *restrict this, - libgamma_site_state_t *restrict site, size_t partition) -{ - DWORD n = 0; - DISPLAY_DEVICE display; - - (void) site; - - if (partition) - return LIBGAMMA_NO_SUCH_PARTITION; - - /* Count CRTC:s by iteration over all possible identifiers - until we reach on that does not exist */ - display.cb = sizeof(DISPLAY_DEVICE); - while (EnumDisplayDevices(NULL, n, &display, 0)) - if (n++ == UINT32_MAX) - return LIBGAMMA_IMPOSSIBLE_AMOUNT; - this->crtcs_available = (size_t)n; - return 0; -} - - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void -libgamma_w32_gdi_partition_destroy(libgamma_partition_state_t *restrict this) -{ - (void) this; -} - - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_w32_gdi_partition_restore(libgamma_partition_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_w32_gdi_crtc_initialise(libgamma_crtc_state_t *restrict this, - libgamma_partition_state_t *restrict partition, size_t crtc) -{ - DISPLAY_DEVICE display; - HDC context; - - (void) partition; - - this->data = NULL; - - /* Windows's API mandates this... */ - display.cb = sizeof(DISPLAY_DEVICE); - /* Get identifier for selected CRTC */ - if (!EnumDisplayDevices(NULL, (DWORD)crtc, &display, 0)) - return LIBGAMMA_NO_SUCH_CRTC; - /* Check that the connector is enabled, - * newer versions of Windows will always pass. - * (According to w32's documentation, but that - * that is a load of crap) */ - if (!(display.StateFlags & DISPLAY_DEVICE_ACTIVE)) - return LIBGAMMA_CONNECTOR_DISABLED; - /* Acquire CRTC connection. */ - context = CreateDC(TEXT("DISPLAY"), display.DeviceName, NULL, NULL); - if (!context) - return LIBGAMMA_OPEN_CRTC_FAILED; - this->data = context; - return 0; -} - - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void -libgamma_w32_gdi_crtc_destroy(libgamma_crtc_state_t *restrict this) -{ - if (this->data) - ReleaseDC(NULL, this->data); -} - - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_w32_gdi_crtc_restore(libgamma_crtc_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error. On error refer to the error reports in `this` - */ -int -libgamma_w32_gdi_get_crtc_information(libgamma_crtc_information_t *restrict this, - libgamma_crtc_state_t *restrict crtc, int32_t fields) -{ -#define KNOWN_FIELDS (LIBGAMMA_CRTC_INFO_GAMMA_SIZE | LIBGAMMA_CRTC_INFO_GAMMA_DEPTH) -#define _E(FIELD) ((fields & FIELD) ? LIBGAMMA_CRTC_INFO_NOT_SUPPORTED : 0) - - (void) crtc; - - /* Windows GDI does not support EDID or monitor dimensions */ - this->edid_error = _E(LIBGAMMA_CRTC_INFO_EDID); - this->width_mm_error = _E(LIBGAMMA_CRTC_INFO_WIDTH_MM); - this->height_mm_error = _E(LIBGAMMA_CRTC_INFO_HEIGHT_MM); - this->width_mm_edid_error = _E(LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID); - this->height_mm_edid_error = _E(LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID); - /* Windows GDI have fixed gamma ramp sizes */ - this->red_gamma_size = GAMMA_RAMP_SIZE; - this->green_gamma_size = GAMMA_RAMP_SIZE; - this->blue_gamma_size = GAMMA_RAMP_SIZE; - this->gamma_size_error = 0; - /* Windows GDI have fixed gamma ramp depth */ - this->gamma_depth = 16; - this->gamma_depth_error = 0; - /* It is possible to query Windows GDI whether the device - have gamma ramp support. It cannot fail. However, I think - the result is incorrect if multiple monitors are active, - so we cannot include this. */ - /* - if ((fields & LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT)) - this->gamma_support = GetDeviceCaps(crtc->data, COLORMGMTCAPS) == CM_GAMMA_RAMP; - this->gamma_support_error = 0; - */ - this->gamma_support_error = _E(LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT); - /* Windows GDI does not support EDID or connector information */ - this->subpixel_order_error = _E(LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER); - this->active_error = _E(LIBGAMMA_CRTC_INFO_ACTIVE); - this->connector_name_error = _E(LIBGAMMA_CRTC_INFO_CONNECTOR_NAME); - this->connector_type_error = _E(LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE); - this->gamma_error = _E(LIBGAMMA_CRTC_INFO_GAMMA); - - /* There was a failure if and only if unsupport field was requested. */ - return (fields & ~KNOWN_FIELDS) ? -1 : 0; - -#undef _E -#undef KNOWN_FIELDS -} - - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_w32_gdi_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t *restrict ramps) -{ -#ifdef DEBUG - /* Windows GDI have fixed gamma ramp sizes */ - if (ramps-> red_size != GAMMA_RAMP_SIZE || - ramps->green_size != GAMMA_RAMP_SIZE || - ramps-> blue_size != GAMMA_RAMP_SIZE) - return LIBGAMMA_WRONG_GAMMA_RAMP_SIZE; -#endif - /* Read current gamma ramps */ - if (!GetDeviceGammaRamp(this->data, ramps->red)) - return LIBGAMMA_GAMMA_RAMP_READ_FAILED; - return 0; -} - - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_w32_gdi_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t ramps) -{ -#ifdef DEBUG - /* Windows GDI have fixed gamma ramp sizes */ - if (ramps. red_size != GAMMA_RAMP_SIZE || - ramps.green_size != GAMMA_RAMP_SIZE || - ramps. blue_size != GAMMA_RAMP_SIZE) - return LIBGAMMA_WRONG_GAMMA_RAMP_SIZE; -#endif - /* Apply gamma ramps */ - if (!SetDeviceGammaRamp(this->data, ramps.red)) - return LIBGAMMA_GAMMA_RAMP_WRITE_FAILED; - return 0; -} diff --git a/src/lib/gamma-w32-gdi.h b/src/lib/gamma-w32-gdi.h deleted file mode 100644 index d139f16..0000000 --- a/src/lib/gamma-w32-gdi.h +++ /dev/null @@ -1,139 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_GAMMA_W32_GDI_H -#define LIBGAMMA_GAMMA_W32_GDI_H - -#ifndef HAVE_LIBGAMMA_METHOD_W32_GDI -# error Including gamma-w32-gdi.h without HAVE_LIBGAMMA_METHOD_W32_GDI -#endif - - -#include "libgamma-method.h" - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void libgamma_w32_gdi_method_capabilities(libgamma_method_capabilities_t *restrict); - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_w32_gdi_site_initialise(libgamma_site_state_t *restrict, char *restrict); - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void libgamma_w32_gdi_site_destroy(libgamma_site_state_t *restrict) __attribute__((const)); - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_w32_gdi_site_restore(libgamma_site_state_t *restrict); - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_w32_gdi_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void libgamma_w32_gdi_partition_destroy(libgamma_partition_state_t *restrict) __attribute__((const)); - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_w32_gdi_partition_restore(libgamma_partition_state_t *restrict); - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_w32_gdi_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void libgamma_w32_gdi_crtc_destroy(libgamma_crtc_state_t *restrict); - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_w32_gdi_crtc_restore(libgamma_crtc_state_t *restrict); - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error. On error refer to the error reports in `this` - */ -int libgamma_w32_gdi_get_crtc_information(libgamma_crtc_information_t *restrict, libgamma_crtc_state_t *restrict, int32_t); - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_w32_gdi_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_w32_gdi_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t); - - -#endif diff --git a/src/lib/gamma-x-randr.c b/src/lib/gamma-x-randr.c deleted file mode 100644 index fce65d1..0000000 --- a/src/lib/gamma-x-randr.c +++ /dev/null @@ -1,985 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef HAVE_LIBGAMMA_METHOD_X_RANDR -# error Compiling gamma-x-randr.c without HAVE_LIBGAMMA_METHOD_X_RANDR -#endif - -#include "gamma-x-randr.h" - -#include "libgamma-error.h" -#include "edid.h" - -#include <stdlib.h> -#include <errno.h> -#include <string.h> -#include <stdint.h> -#ifdef DEBUG -# include <stdio.h> -#endif - -#include <xcb/xcb.h> -#include <xcb/randr.h> - - -/** - * The major version of RandR the library expects - */ -#define RANDR_VERSION_MAJOR 1 - -/** - * The minor version of RandR the library expects - */ -#define RANDR_VERSION_MINOR 3 - - - -/** - * Data structure for partition data - */ -typedef struct libgamma_x_randr_partition_data { - /** - * Mapping from CRTC indices to CRTC identifiers - */ - xcb_randr_crtc_t *crtcs; - - /** - * Mapping from output indices to output identifiers - */ - xcb_randr_output_t *outputs; - - /** - * The number of outputs available - */ - size_t outputs_count; - - /** - * Mapping from CRTC indices to output indices. - * CRTC's without an output (should be impossible) - * have the value `SIZE_MAX` which is impossible - * for an existing mapping - */ - size_t *crtc_to_output; - - /** - * Screen configuration timestamp - */ - xcb_timestamp_t config_timestamp; - -} libgamma_x_randr_partition_data_t; - - - -/** - * Translate an xcb error into a libgamma error - * - * @param error_code The xcb error - * @param default_error The libgamma error to use if the xcb error is not recognised - * @return The libgamma error - */ -static int -translate_error_(int error_code, int default_error) -{ - switch (error_code) { - case XCB_CONN_ERROR: return errno = ECONNABORTED, LIBGAMMA_ERRNO_SET; - case XCB_CONN_CLOSED_EXT_NOTSUPPORTED: return errno = ENOPROTOOPT, LIBGAMMA_ERRNO_SET; - case XCB_CONN_CLOSED_MEM_INSUFFICIENT: return errno = ENOMEM, LIBGAMMA_ERRNO_SET; - case XCB_CONN_CLOSED_REQ_LEN_EXCEED: return errno = EMSGSIZE, LIBGAMMA_ERRNO_SET; - case XCB_CONN_CLOSED_PARSE_ERR: return LIBGAMMA_NO_SUCH_SITE; - case XCB_CONN_CLOSED_INVALID_SCREEN: return LIBGAMMA_NO_SUCH_PARTITION; - case XCB_CONN_CLOSED_FDPASSING_FAILED: return errno = EIO, LIBGAMMA_ERRNO_SET; - default: - return default_error; - } -} - - -/** - * Translate an xcb error into a libgamma error - * - * @param error_code The xcb error - * @param default_error The libgamma error to use if the xcb error is not recognised - * @param return_errno Whether an `errno` value may be returned - * @return The libgamma error - */ -static int -translate_error(int error_code, int default_error, int return_errno) -{ - int r = translate_error_(error_code, default_error); - return (return_errno && r > 0) ? errno : r; -} - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void -libgamma_x_randr_method_capabilities(libgamma_method_capabilities_t *restrict this) -{ - char *display = getenv("DISPLAY"); - /* Support for all information except active status and gamma ramp support. - Active status can be queried but it is not guaranteed produces an up to date result. */ - this->crtc_information = LIBGAMMA_CRTC_INFO_MACRO_EDID - | LIBGAMMA_CRTC_INFO_MACRO_VIEWPORT - | LIBGAMMA_CRTC_INFO_MACRO_RAMP - | LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER - | LIBGAMMA_CRTC_INFO_MACRO_CONNECTOR; - /* X RandR supports multiple sites, partitions and CRTC:s */ - this->default_site_known = display && *display; - this->multiple_sites = 1; - this->multiple_partitions = 1; - this->multiple_crtcs = 1; - /* Partitions are screens and not graphics cards in X */ - this->partitions_are_graphics_cards = 0; - /* X does not have system restore capabilities */ - this->site_restore = 0; - this->partition_restore = 0; - this->crtc_restore = 0; - /* Gamma ramp sizes are identical but not fixed */ - this->identical_gamma_sizes = 1; - this->fixed_gamma_size = 0; - /* Gamma ramp depths are fixed */ - this->fixed_gamma_depth = 1; - /* X RandR is a real non-faked adjustment method */ - this->real = 1; - this->fake = 0; - /* Gamma ramp adjustments are persistent */ - this->auto_restore = 0; -} - - -/* xcb violates the rule to never return struct:s */ -#ifdef __GNUC__ -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Waggregate-return" -#endif - - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_randr_site_initialise(libgamma_site_state_t *restrict this, char *restrict site) -{ - xcb_generic_error_t *error = NULL; - xcb_connection_t *restrict connection; - xcb_randr_query_version_cookie_t cookie; - xcb_randr_query_version_reply_t *restrict reply; - const xcb_setup_t *restrict setup; - xcb_screen_iterator_t iter; - - /* Connect to the display server */ - this->data = connection = xcb_connect(site, NULL); - if (!connection || xcb_connection_has_error(connection)) - return LIBGAMMA_OPEN_SITE_FAILED; - - /* Query the version of the X RandR extension protocol */ - cookie = xcb_randr_query_version(connection, RANDR_VERSION_MAJOR, RANDR_VERSION_MINOR); - reply = xcb_randr_query_version_reply(connection, cookie, &error); - - /* Check for version query failure */ - if (error || !reply) { - /* Release resources */ - free(reply); - /* If `xcb_connect` failed, both `error` and `reply` will be `NULL`. - XXX: Can both be `NULL` for any other reason? */ - if (!error && !reply) - return LIBGAMMA_OPEN_SITE_FAILED; - xcb_disconnect(connection); - /* Translate and report error. */ - if (error != NULL) - return translate_error(error->error_code, LIBGAMMA_PROTOCOL_VERSION_QUERY_FAILED, 0); - return LIBGAMMA_PROTOCOL_VERSION_QUERY_FAILED; - } - - /* Check protocol compatibility, - we require 1.3 but 2.x may not be backwards compatible */ - if (reply->major_version != RANDR_VERSION_MAJOR || reply->minor_version < RANDR_VERSION_MINOR) { -#ifdef DEBUG - /* Print used protocol */ - fprintf(stderr, "libgamma: RandR protocol version: %u.%u", reply->major_version, reply->minor_version); -#endif - /* Release resources */ - free(reply); - xcb_disconnect(connection); - /* Report error */ - return LIBGAMMA_PROTOCOL_VERSION_NOT_SUPPORTED; - } - - /* We do not longer need to know the version of the protocol */ - free(reply); - - /* Get available screens */ - setup = xcb_get_setup(connection); - if (!setup) { - xcb_disconnect(connection); - return LIBGAMMA_LIST_PARTITIONS_FAILED; - } - iter = xcb_setup_roots_iterator(setup); - /* Get the number of available screens */ - this->partitions_available = (size_t)iter.rem; - - /* Sanity check the number of available screens. */ - return iter.rem >= ? 0 : LIBGAMMA_NEGATIVE_PARTITION_COUNT; -} - - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void -libgamma_x_randr_site_destroy(libgamma_site_state_t *restrict this) -{ - xcb_disconnect((xcb_connection_t *)this->data); -} - - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_randr_site_restore(libgamma_site_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - -/** - * Duplicate a memory area - * - * @param ptr The memory area - * @param bytes The size, in bytes, of the memory area - * @return A duplication of the memory, `NULL` if zero-length or on error - */ -static inline void * -memdup(void *restrict ptr, size_t bytes) -{ - char *restrict rc; - if (!bytes) - return NULL; - rc = malloc(bytes); - if (!rc) - return NULL; - memcpy(rc, ptr, bytes); - return rc; -} - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to. - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_randr_partition_initialise(libgamma_partition_state_t *restrict this, - libgamma_site_state_t *restrict site, size_t partition) -{ - int fail_rc = LIBGAMMA_ERRNO_SET; - xcb_connection_t *restrict connection = site->data; - xcb_screen_t *restrict screen = NULL; - xcb_generic_error_t *error = NULL; - const xcb_setup_t *restrict setup; - xcb_screen_iterator_t iter; - xcb_randr_get_screen_resources_current_cookie_t cookie; - xcb_randr_get_screen_resources_current_reply_t *restrict reply; - xcb_randr_crtc_t *restrict crtcs; - xcb_randr_output_t *restrict outputs; - libgamma_x_randr_partition_data_t *restrict data; - xcb_randr_get_output_info_cookie_t out_cookie; - xcb_randr_get_output_info_reply_t *out_reply; - size_t i; - uint16_t j; - - /* Get screen list */ - setup = xcb_get_setup(connection); - if (!setup) - return LIBGAMMA_LIST_PARTITIONS_FAILED; - iter = xcb_setup_roots_iterator(setup); - - /* Get the screen */ - for (i = 0; iter.rem > 0; i++, xcb_screen_next(&iter)) - if (i == partition) { - screen = iter.data; - break; - } - /* Report failure if we did not find the screen */ - if (!iter.rem) - return LIBGAMMA_NO_SUCH_PARTITION; - - /* Check that the screen is not `NULL`. - * (Do not think this can happen, but why not.) */ - if (!screen) - return LIBGAMMA_NULL_PARTITION; - - /* Get the current resources of the screen */ - cookie = xcb_randr_get_screen_resources_current(connection, screen->root); - reply = xcb_randr_get_screen_resources_current_reply(connection, cookie, &error); - if (error) - return translate_error(error->error_code, LIBGAMMA_LIST_CRTCS_FAILED, 0); - - /* Get the number of available CRTC:s */ - this->crtcs_available = reply->num_crtcs; - /* Get the CRTC and output lists */ - crtcs = xcb_randr_get_screen_resources_current_crtcs(reply); - outputs = xcb_randr_get_screen_resources_current_outputs(reply); - if (!crtcs || !outputs) { - free(reply); - return LIBGAMMA_REPLY_VALUE_EXTRACTION_FAILED; - } - - /* Allocate adjustment method dependent data memory area. - We use `calloc` because we want `data`'s pointers to be `NULL` if not allocated at `fail`. */ - data = calloc(1, sizeof(libgamma_x_randr_partition_data_t)); - if (!data) - goto fail; - - /* Copy the CRTC:s, just so we do not have to keep the reply in memory */ - data->crtcs = memdup(crtcs, (size_t)(reply->num_crtcs) * sizeof(xcb_randr_crtc_t)); - if (!data->crtcs && reply->num_crtcs > 0) - goto fail; - - /* Copy the outputs as well */ - data->outputs = memdup(outputs, (size_t)reply->num_outputs * sizeof(xcb_randr_output_t)); - if (!data->outputs && reply->num_outputs > 0) - goto fail; - - /* Get the number of available outputs */ - data->outputs_count = (size_t)reply->num_outputs; - - /* Create mapping table from CRTC indices to output indicies. (injection) */ - data->crtc_to_output = malloc((size_t)reply->num_crtcs * sizeof(size_t)); - if (!data->crtc) - goto fail; - /* All CRTC:s should be mapped, but incase they are not, all unmapped CRTC:s should have - an invalid target, namely `SIZE_MAX`, which is 1 more than the theoretical limit */ - for (i = 0; i < (size_t)reply->num_crtcs; i++) - data->crtc_to_output[i] = SIZE_MAX; - /* Fill the table */ - for (i = 0; i < (size_t)reply->num_outputs; i++) { - /* Query output (target) information */ - out_cookie = xcb_randr_get_output_info(connection, outputs[i], reply->config_timestamp); - out_reply = xcb_randr_get_output_info_reply(connection, out_cookie, &error); - if (error) { - fail_rc = translate_error(error->error_code, LIBGAMMA_OUTPUT_INFORMATION_QUERY_FAILED, 0); - goto fail; - } - - /* Find CRTC (source) */ - for (j = 0; j < reply->num_crtcs; j++) { - if (crtcs[j] == out_reply->crtc) { - data->crtc_to_output[j] = i; - break; - } - } - - /* Release output information */ - free(out_reply); - } - - /* Store the configuration timestamp */ - data->config_timestamp = reply->config_timestamp; - /* Store the adjustment method dependent data */ - this->data = data; - /* Release resources and return successfully */ - free(reply); - return 0; - -fail: - /* Release resources and return with an error */ - if (data) { - free(data->crtcs); - free(data->outputs); - free(data->crtc_to_output); - free(data); - } - free(reply); - return fail_rc; -} - - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void -libgamma_x_randr_partition_destroy(libgamma_partition_state_t *restrict this) -{ - libgamma_x_randr_partition_data_t *restrict data = this->data; - free(data->crtcs); - free(data->outputs); - free(data->crtc_to_output); - free(data); -} - - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_randr_partition_restore(libgamma_partition_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to. - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_randr_crtc_initialise(libgamma_crtc_state_t *restrict this, - libgamma_partition_state_t *restrict partition, size_t crtc) -{ - libgamma_x_randr_partition_data_t *restrict screen_data = partition->data; - xcb_randr_crtc_t *restrict crtc_ids = screen_data->crtcs; - this->data = crtc_ids + crtc; - return crtc < partition->crtcs_available ? 0 : LIBGAMMA_NO_SUCH_CRTC; -} - - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void -libgamma_x_randr_crtc_destroy(libgamma_crtc_state_t *restrict this) -{ - (void) this; -} - - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_randr_crtc_restore(libgamma_crtc_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - - -/** - * Get the gamma ramp size of a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @return Non-zero on error - */ -static int -get_gamma_ramp_size(libgamma_crtc_information_t *restrict out, libgamma_crtc_state_t *restrict crtc) -{ - xcb_connection_t *restrict connection = crtc->partition->site->data; - xcb_randr_crtc_t *restrict crtc_id = crtc->data; - xcb_randr_get_crtc_gamma_size_cookie_t cookie; - xcb_randr_get_crtc_gamma_size_reply_t *restrict reply; - xcb_generic_error_t *error; - - /* Query gamma ramp size */ - out->gamma_size_error = 0; - cookie = xcb_randr_get_crtc_gamma_size(connection, *crtc_id); - reply = xcb_randr_get_crtc_gamma_size_reply(connection, cookie, &error); - if (error) { - out->gamma_size_error = translate_error(error->error_code, LIBGAMMA_GAMMA_RAMPS_SIZE_QUERY_FAILED, 1); - return out->gamma_size_error; - } - /* Sanity check gamma ramp size */ - if (reply->size < 2) - out->gamma_size_error = LIBGAMMA_SINGLETON_GAMMA_RAMP; - /* Store gamma ramp size */ - out->red_gamma_size = out->green_gamma_size = out->blue_gamma_size = reply->size; - /* Release resources and return successfulnes */ - free(reply); - return out->gamma_size_error; -} - - -/** - * Read information from the CRTC's output - * - * @param out Instance of a data structure to fill with the information about the CRTC - * @param output The CRTC's output information - * @return Non-zero if at least on error occured - */ -static int -read_output_data(libgamma_crtc_information_t *restrict out, xcb_randr_get_output_info_reply_t *restrict output) -{ -#define __select(value)\ - case XCB_RENDER_SUB_PIXEL_##value: out->subpixel_order = LIBGAMMA_SUBPIXEL_ORDER_##value; break - - switch (output->connection) { - case XCB_RANDR_CONNECTION_CONNECTED: - /* We have a monitor connected, report that and store information that is provided to us */ - out->active = 1; - out->width_mm = output->mm_width; - out->height_mm = output->mm_height; - switch (output->subpixel_order) { - __select (UNKNOWN); - __select (HORIZONTAL_RGB); - __select (HORIZONTAL_BGR); - __select (VERTICAL_RGB); - __select (VERTICAL_BGR); - __select (NONE); - default: - out->subpixel_order_error = LIBGAMMA_SUBPIXEL_ORDER_NOT_RECOGNISED; - break; - } - return 0; - - case XCB_RANDR_CONNECTION_UNKNOWN: - /* If we do know whether a monitor is connected report that and assume it is not */ - out->active_error = LIBGAMMA_STATE_UNKNOWN; - /* fall through */ - default: - /* If no monitor is connected, report that on fails that require it */ - out->width_mm_error = LIBGAMMA_NOT_CONNECTED; - out->height_mm_error = LIBGAMMA_NOT_CONNECTED; - out->subpixel_order_error = LIBGAMMA_NOT_CONNECTED; - /* And store that we are not connected */ - out->active = 0; - /* This fuction only failed if we could not figure out whether a monitor is connected */ - return output->connection != XCB_RANDR_CONNECTION_UNKNOWN ? 0 : -1; - } - -#undef __select -} - - -/** - * Determine the connector type from the connector name - * - * @param this The CRTC information to use and extend - * @param Non-zero on error - */ -static int -get_connector_type(libgamma_crtc_information_t *restrict this) -{ - /* Since we require the name of the output of get the type of the connected, - copy any reported error on the output's name to the connector's type, - and report failure if there was an error */ - if ((this->connector_type_error = this->connector_name_error)) - return -1; - -#define __select(name, type)\ - do {\ - if (strstr(this->connector_name, name "-") == this->connector_name) {\ - this->connector_type = LIBGAMMA_CONNECTOR_TYPE_##type\ - return 0;\ - }\ - } while (0) - - /* Check begin on the name of the output to find out what type the connector is of */ - __select ("None", Unknown); - __select ("VGA", VGA); - __select ("DVI-I", DVII); - __select ("DVI-D", DVID); - __select ("DVI-A", DVIA); - __select ("DVI", DVI); - __select ("Composite", Composite); - __select ("S-Video", SVIDEO); - __select ("Component", Component); - __select ("LFP", LFP); - __select ("Proprietary", Unknown); - __select ("HDMI", HDMI); - __select ("DisplayPort", DisplayPort); - -#undef __select - - /* If there was no matching output name pattern report that and exit with an error */ - this->connector_name_error = LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED; - return -1; -} - - -/** - * Get the output name of a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param output The CRTC's output's information - * @return Non-zero on error - */ -static int -get_output_name(libgamma_crtc_information_t *restrict out, xcb_randr_get_output_info_reply_t *restrict output) -{ - char *restrict store; - uint8_t *restrict name; - uint16_t length; - size_t i; - - /* Get the name of the output and the length of that name */ - name = xcb_randr_get_output_info_name(output); - length = output->name_len; /* There is no NUL-termination */ - if (!name) - return out->connector_name_error = LIBGAMMA_REPLY_VALUE_EXTRACTION_FAILED; - - /* Allocate a memory area for a NUL-terminated copy of the name */ - store = out->connector_name = malloc(((size_t)length + 1) * sizeof(char)); - if (!store) { - out->connector_name_error = errno; - return -1; - } - - /* char is guaranteed to be (u)int_least8_t, but it is only guaranteed to be (u)int8_t - * on POSIX, so to be truly portable we will not assume that char is (u)int8_t */ - for (i = 0; i < (size_t)length; i++) - store[i] = (char)name[i]; - store[length] = '\0'; - - return 0; -} - - -/** - * Get the Extended Display Information Data of the monitor connected to the connector of a CRTC - * - * @param out Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param output The CRTC's output - * @return Non-zero on error - */ -static int -get_edid(libgamma_crtc_information_t *restrict out, libgamma_crtc_state_t *restrict crtc, xcb_randr_output_t output) -{ - xcb_connection_t *restrict connection = crtc->partition->site->data; - xcb_randr_list_output_properties_cookie_t prop_cookie; - xcb_randr_list_output_properties_reply_t *restrict prop_reply; - xcb_atom_t *atoms; - xcb_atom_t *atoms_end; - xcb_generic_error_t *error; - xcb_get_atom_name_cookie_t atom_name_cookie; - xcb_get_atom_name_reply_t *restrict atom_name_reply; - char *restrict atom_name; - int atom_name_len; - xcb_randr_get_output_property_cookie_t atom_cookie; - xcb_randr_get_output_property_reply_t *restrict atom_reply; - unsigned char* restrict atom_data; - int length; - - /* Acquire a list of all properties of the output */ - prop_cookie = xcb_randr_list_output_properties(connection, output); - prop_reply = xcb_randr_list_output_properties_reply(connection, prop_cookie, &error); - if (error) - return out->edid_error = translate_error(error->error_code, LIBGAMMA_LIST_PROPERTIES_FAILED, 1); - - /* Extract the properties form the data structure that holds them, */ - atoms = xcb_randr_list_output_properties_atoms(prop_reply); - /* and get the last one so that we can iterate over them nicely */ - atoms_end = atoms + xcb_randr_list_output_properties_atoms_length(prop_reply); - - if (!atoms) { - free(prop_reply); - return out->edid_error = LIBGAMMA_REPLY_VALUE_EXTRACTION_FAILED; - } - - /* For each property */ - for (; atoms != atoms_end; atoms++) { - /* Acquire the atom name */ - atom_name_cookie = xcb_get_atom_name(connection, *atoms); - atom_name_reply = xcb_get_atom_name_reply(connection, atom_name_cookie, &error); - if (error) - continue; - - /* Extract the atom name from the data structure that holds it */ - atom_name = xcb_get_atom_name_name(atom_name_reply); - /* As well as the length of the name; it is not NUL-termianted */ - atom_name_len = xcb_get_atom_name_name_length(atom_name_reply); - - if (/* Check for errors */ - !atom_name || /* atom_name_len < 1 || */ - /* Check that the length is the expected length for the EDID property */ - atom_name_len != 4 || - /* Check that the property is the EDID property */ - atom_name[0] != 'E' || atom_name[1] != 'D' || atom_name[2] != 'I' || atom_name[3] != 'D') { - free(atom_name_reply); - continue; - } - - /* Acquire the property's value, we know that it is either 128 or 256 byte long */ - atom_cookie = xcb_randr_get_output_property(connection, output, *atoms, XCB_GET_PROPERTY_TYPE_ANY, 0, 256, 0, 0); - atom_reply = xcb_randr_get_output_property_reply(connection, atom_cookie, &error); - /* (*) EDID version 1.0 through 1.4 define it as 128 bytes long, - * but version 2.0 define it as 256 bytes long. However, - * version 2.0 is rare(?) and has been deprecated and replaced - * by version 1.3 (I guess that is with a new version epoch, - * but I do not know.) */ - if (error) { - free(atom_name_reply); - free(prop_reply); - return out->edid_error = LIBGAMMA_PROPERTY_VALUE_QUERY_FAILED; - } - - /* Extract the property's value */ - atom_data = xcb_randr_get_output_property_data(atom_reply); - /* and its actual length */ - length = xcb_randr_get_output_property_data_length(atom_reply); - if (!atom_data || length < 1) { - free(atom_reply); - free(atom_name_reply); - free(prop_reply); - return out->edid_error = LIBGAMMA_REPLY_VALUE_EXTRACTION_FAILED; - } - - /* Store the EDID */ - out->edid_length = (size_t)length; - out->edid = malloc((size_t)length * sizeof(unsigned char)); - if (!out->edid) - out->edid_error = errno; - else - memcpy(out->edid, atom_data, (size_t)length * sizeof(unsigned char)); - - /* Release resouces */ - free(atom_reply); - free(atom_name_reply); - free(prop_reply); - - return out->edid_error; - } - - return out->edid_error = LIBGAMMA_EDID_NOT_FOUND; -} - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error; on error refer to the error reports in `this` - */ -int -libgamma_x_randr_get_crtc_information(libgamma_crtc_information_t *restrict this, - libgamma_crtc_state_t *restrict crtc, int32_t fields) -{ -#define _E(FIELD) ((fields & FIELD) ? LIBGAMMA_CRTC_INFO_NOT_SUPPORTED : 0) - - int e = 0; - xcb_randr_get_output_info_reply_t *restrict output_info = NULL; - xcb_randr_output_t output; - int free_edid, free_name; - xcb_connection_t *restrict connection; - libgamma_x_randr_partition_data_t *restrict screen_data; - size_t output_index; - xcb_randr_get_output_info_cookie_t cookie; - xcb_generic_error_t *error; - - /* Wipe all error indicators */ - memset(this, 0, sizeof(libgamma_crtc_information_t)); - - /* We need to free the EDID after us if it is not explicitly requested */ - free_edid = !(fields & LIBGAMMA_CRTC_INFO_EDID); - - /* We need to free the output's name after us if it is not explicitly requested */ - free_name = !(fields & LIBGAMMA_CRTC_INFO_CONNECTOR_NAME); - - /* Jump if the output information is not required */ - if (!(fields & (LIBGAMMA_CRTC_INFO_MACRO_ACTIVE | LIBGAMMA_CRTC_INFO_MACRO_CONNECTOR))) - goto cont; - - /* Get connector and connector information */ - connection = crtc->partition->site->data; - screen_data = crtc->partition->data; - output_index = screen_data->crtc_to_output[crtc->crtc]; - /* `SIZE_MAX` is used for CRTC:s that misses mapping to its output (should not happen), - * because `SIZE_MAX - 1` is the highest theoretical possible value */ - if (output_index == SIZE_MAX) { - e |= this->edid_error = this->gamma_error = this->width_mm_edid_error - = this->height_mm_edid_error = this->connector_type_error - = this->connector_name_error = this->subpixel_order_error - = this->width_mm_error = this->height_mm_error - = this->active_error = LIBGAMMA_CONNECTOR_UNKNOWN; - goto cont; - } - /* Get the output */ - output = screen_data->outputs[output_index]; - /* Query output information */ - cookie = xcb_randr_get_output_info(connection, output, screen_data->config_timestamp); - output_info = xcb_randr_get_output_info_reply(connection, cookie, &error); - if (error) { - e |= this->edid_error = this->gamma_error = this->width_mm_edid_error - = this->height_mm_edid_error = this->connector_type_error - = this->connector_name_error = this->subpixel_order_error - = this->width_mm_error = this->height_mm_error - = this->active_error = LIBGAMMA_OUTPUT_INFORMATION_QUERY_FAILED; - goto cont; - } - - /* Get connector name */ - e |= get_output_name(this, output_info); - /* Get connector type */ - if (fields & LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE) - e |= get_connector_type(this); - /* Get additional output data, excluding EDID */ - e |= read_output_data(this, output_info); - if (fields & LIBGAMMA_CRTC_INFO_MACRO_VIEWPORT) - e |= this->width_mm_error | this->height_mm_error; - e |= (fields & LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER) ? this->subpixel_order_error : 0; - - /* If we do not want any EDID information, jump */ - if (!(fields & LIBGAMMA_CRTC_INFO_MACRO_EDID)) - goto cont; - /* If there is not monitor that report error in EDID related fields */ - if (!this->active) { - e |= this->edid_error = this->gamma_error = this->width_mm_edid_error - = this->height_mm_edid_error = LIBGAMMA_NOT_CONNECTED; - goto cont; - } - /* Get EDID */ - e |= get_edid(this, crtc, output); - if (!this->edid) { - this->gamma_error = this->width_mm_edid_error = this->height_mm_edid_error = this->edid_error; - goto cont; - } - /* Parse EDID */ - if ((fields & (LIBGAMMA_CRTC_INFO_MACRO_EDID ^ LIBGAMMA_CRTC_INFO_EDID))) - e |= libgamma_parse_edid(this, fields); - -cont: - /* Get gamma ramp size */ - e |= (fields & LIBGAMMA_CRTC_INFO_GAMMA_SIZE) ? get_gamma_ramp_size(this, crtc) : 0; - /* Store gamma ramp depth. */ - this->gamma_depth = 16; - /* X RandR does not support quering gamma ramp support. */ - e |= this->gamma_support_error = _E(LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT); - - /* Free the EDID after us */ - if (free_edid) { - free(this->edid); - this->edid = NULL; - } - /* Free the output name after us */ - if (free_name) { - free(this->connector_name); - this->connector_name = NULL; - } - - free(output_info); - return e ? -1 : 0; - -#undef _E -} - - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_randr_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t *restrict ramps) -{ - xcb_connection_t *restrict connection = this->partition->site->data; - xcb_randr_get_crtc_gamma_cookie_t cookie; - xcb_randr_get_crtc_gamma_reply_t *restrict reply; - xcb_generic_error_t *error; - uint16_t *restrict red; - uint16_t *restrict green; - uint16_t *restrict blue; - -#ifdef DEBUG - /* Gamma ramp sizes are identical but not fixed */ - if (ramps->red_size != ramps->green_size || ramps->red_size != ramps->blue_size) - return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; -#endif - - /* Read current gamma ramps */ - cookie = xcb_randr_get_crtc_gamma(connection, *(xcb_randr_crtc_t *)this->data); - reply = xcb_randr_get_crtc_gamma_reply(connection, cookie, &error); - - /* Check for errors */ - if (error) - return translate_error(error->error_code, LIBGAMMA_GAMMA_RAMP_READ_FAILED, 0); - - /* Get current gamma ramps from response */ - red = xcb_randr_get_crtc_gamma_red(reply); - green = xcb_randr_get_crtc_gamma_green(reply); - blue = xcb_randr_get_crtc_gamma_blue(reply); - /* Copy over the gamma ramps to our memory */ - memcpy(ramps->red, red, ramps->red_size * sizeof(uint16_t)); - memcpy(ramps->green, green, ramps->green_size * sizeof(uint16_t)); - memcpy(ramps->blue, blue, ramps->blue_size * sizeof(uint16_t)); - - free(reply); - return 0; -} - - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_randr_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t ramps) -{ - xcb_connection_t *restrict connection = this->partition->site->data; - xcb_void_cookie_t cookie; - xcb_generic_error_t *restrict error; -#ifdef DEBUG - /* Gamma ramp sizes are identical but not fixed */ - if (ramps.red_size != ramps.green_size || ramps.red_size != ramps.blue_size) - return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; -#endif - - /* Apply gamma ramps */ - cookie = xcb_randr_set_crtc_gamma_checked(connection, *(xcb_randr_crtc_t*)this->data, - (uint16_t)ramps.red_size, ramps.red, ramps.green, ramps.blue); - /* Check for errors */ - error = xcb_request_check(connection, cookie); - if (error) - return translate_error(error->error_code, LIBGAMMA_GAMMA_RAMP_WRITE_FAILED, 0); - return 0; -} - - -#ifdef __GNUC__ -# pragma GCC diagnostic pop -#endif diff --git a/src/lib/gamma-x-randr.h b/src/lib/gamma-x-randr.h deleted file mode 100644 index 59c64bc..0000000 --- a/src/lib/gamma-x-randr.h +++ /dev/null @@ -1,139 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_GAMMA_X_RANDR_H -#define LIBGAMMA_GAMMA_X_RANDR_H - -#ifndef HAVE_LIBGAMMA_METHOD_X_RANDR -# error Including gamma-x-randr.h without HAVE_LIBGAMMA_METHOD_X_RANDR -#endif - - -#include "libgamma-method.h" - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void libgamma_x_randr_method_capabilities(libgamma_method_capabilities_t *restrict); - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_randr_site_initialise(libgamma_site_state_t *restrict, char *restrict); - -/** - * Release all resources held by a site state. - * - * @param this The site state. - */ -void libgamma_x_randr_site_destroy(libgamma_site_state_t *restrict); - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_randr_site_restore(libgamma_site_state_t *restrict); - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_randr_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void libgamma_x_randr_partition_destroy(libgamma_partition_state_t *restrict); - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_randr_partition_restore(libgamma_partition_state_t *restrict); - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_randr_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void libgamma_x_randr_crtc_destroy(libgamma_crtc_state_t *restrict) __attribute__((const)); - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_randr_crtc_restore(libgamma_crtc_state_t *restrict); - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error; on error refer to the error reports in `this` - */ -int libgamma_x_randr_get_crtc_information(libgamma_crtc_information_t *restrict, libgamma_crtc_state_t *restrict, int32_t); - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_randr_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_randr_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t); - - -#endif diff --git a/src/lib/gamma-x-vidmode.c b/src/lib/gamma-x-vidmode.c deleted file mode 100644 index bae9d31..0000000 --- a/src/lib/gamma-x-vidmode.c +++ /dev/null @@ -1,312 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef HAVE_LIBGAMMA_METHOD_X_VIDMODE -# error Compiling gamma-x-vidmode.c without HAVE_LIBGAMMA_METHOD_X_VIDMODE -#endif - -#include "gamma-x-vidmode.h" - -#include "libgamma-error.h" - -#include <X11/Xlib.h> -#include <X11/extensions/xf86vmode.h> - -#include <stdlib.h> -#include <errno.h> - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void -libgamma_x_vidmode_method_capabilities(libgamma_method_capabilities_t *restrict this) -{ - char *restrict display = getenv("DISPLAY"); - /* Gamma ramps size and depth can be queried */ - this->crtc_information = LIBGAMMA_CRTC_INFO_GAMMA_SIZE | LIBGAMMA_CRTC_INFO_GAMMA_DEPTH; - /* X VidMode supports multiple sites and partitions but not CRTC:s */ - this->default_site_known = (display && *display) ? 1 : 0; - this->multiple_sites = 1; - this->multiple_partitions = 1; - this->multiple_crtcs = 0; - /* Partitions are screens and not graphics cards in X */ - this->partitions_are_graphics_cards = 0; - /* X does not have system restore capabilities */ - this->site_restore = 0; - this->partition_restore = 0; - this->crtc_restore = 0; - /* Gamma ramp sizes are identical but not fixed */ - this->identical_gamma_sizes = 1; - this->fixed_gamma_size = 0; - /* Gamma ramp depths are fixed */ - this->fixed_gamma_depth = 1; - /* X VidMode is a real non-faked adjustment method */ - this->real = 1; - this->fake = 0; - /* Gamma ramp adjustments are persistent */ - this->auto_restore = 0; -} - - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_vidmode_site_initialise(libgamma_site_state_t *restrict this, char *restrict site) -{ - /* Connect to the display */ - Display *restrict connection; - int _major, _minor, screens; - this->data = connection = XOpenDisplay(site); - if (!this->data) - return LIBGAMMA_OPEN_SITE_FAILED; - /* Query X VidMode extension protocol version */ - if (!XF86VidModeQueryVersion(connection, &_major, &_minor)) { - XCloseDisplay(connection); - return LIBGAMMA_PROTOCOL_VERSION_QUERY_FAILED; - } - /* Query the number of available screens */ - screens = ScreenCount(connection); - if (screens < 0) { - XCloseDisplay(connection); - return LIBGAMMA_NEGATIVE_PARTITION_COUNT; - } - this->partitions_available = (size_t)screens; - return 0; -} - - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void -libgamma_x_vidmode_site_destroy(libgamma_site_state_t *restrict this) -{ - XCloseDisplay((Display *)this->data); -} - - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_vidmode_site_restore(libgamma_site_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_vidmode_partition_initialise(libgamma_partition_state_t *restrict this, - libgamma_site_state_t *restrict site, size_t partition) -{ - this->crtcs_available = 1; - return partition < site->partitions_available ? 0 : LIBGAMMA_NO_SUCH_PARTITION; -} - - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void -libgamma_x_vidmode_partition_destroy(libgamma_partition_state_t *restrict this) -{ - (void) this; -} - - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_vidmode_partition_restore(libgamma_partition_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_vidmode_crtc_initialise(libgamma_crtc_state_t *restrict this, - libgamma_partition_state_t *restrict partition, size_t crtc) -{ - (void) this; - (void) partition; - return !crtc ? 0 : LIBGAMMA_NO_SUCH_CRTC; -} - - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void -libgamma_x_vidmode_crtc_destroy(libgamma_crtc_state_t *restrict this) -{ - (void) this; -} - - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_vidmode_crtc_restore(libgamma_crtc_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error; oOn error refer to the error reports in `this` - */ -int -libgamma_x_vidmode_get_crtc_information(libgamma_crtc_information_t *restrict this, - libgamma_crtc_state_t *restrict crtc, int32_t fields) -{ -#define _E(FIELD) ((fields & FIELD) ? LIBGAMMA_CRTC_INFO_NOT_SUPPORTED : 0) - - Display *restrict connection; - int stops = 0; - - /* X VidMode does not support EDID or monitor dimensions */ - this->edid_error = _E(LIBGAMMA_CRTC_INFO_EDID); - this->width_mm_error = _E(LIBGAMMA_CRTC_INFO_WIDTH_MM); - this->height_mm_error = _E(LIBGAMMA_CRTC_INFO_HEIGHT_MM); - this->width_mm_edid_error = _E(LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID); - this->height_mm_edid_error = _E(LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID); - this->gamma_size_error = 0; - /* X VidMode does support gamma ramp size query. The gamma - ramps are identical but not fixed, and the query can fail. */ - if ((fields & LIBGAMMA_CRTC_INFO_GAMMA_SIZE)) { - connection = crtc->partition->site->data; - if (!XF86VidModeGetGammaRampSize(connection, (int)crtc->partition->partition, &stops)) - this->gamma_size_error = LIBGAMMA_GAMMA_RAMPS_SIZE_QUERY_FAILED; - else if (stops < 2) - this->gamma_size_error = LIBGAMMA_SINGLETON_GAMMA_RAMP; - this->red_gamma_size = this->green_gamma_size = this->blue_gamma_size = (size_t)stops; - } - /* X VidMode uses 16-bit integer ramps */ - this->gamma_depth = 16; - this->gamma_depth_error = 0; - /* X VidMode does not support gamma ramp support queries */ - this->gamma_support_error = _E(LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT); - /* X VidMode does not support EDID or connector information */ - this->subpixel_order_error = _E(LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER); - this->active_error = _E(LIBGAMMA_CRTC_INFO_ACTIVE); - this->connector_name_error = _E(LIBGAMMA_CRTC_INFO_CONNECTOR_NAME); - this->connector_type_error = _E(LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE); - this->gamma_error = _E(LIBGAMMA_CRTC_INFO_GAMMA); - - /* We failed if gamma ramp size query failed or if an unsupport field was queried. */ - return (this->gamma_size_error || (fields & ~(LIBGAMMA_CRTC_INFO_GAMMA_DEPTH | LIBGAMMA_CRTC_INFO_GAMMA_SIZE))) ? -1 : 0; - -#undef _E -} - - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_vidmode_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t *restrict ramps) -{ -#ifdef DEBUG - /* Gamma ramp sizes are identical but not fixed */ - if (ramps->red_size != ramps->green_size || ramps->red_size != ramps->blue_size) - return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; -#endif - /* Read current gamma ramps */ - if (!XF86VidModeGetGammaRamp((Display *)this->partition->site->data, (int)this->partition->partition, - (int)ramps->red_size, ramps->red, ramps->green, ramps->blue)) - return LIBGAMMA_GAMMA_RAMP_READ_FAILED; - return 0; -} - - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_vidmode_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t ramps) -{ -#ifdef DEBUG - /* Gamma ramp sizes are identical but not fixed */ - if (ramps.red_size != ramps.green_size || ramps.red_size != ramps.blue_size) - return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; -#endif - /* Apply gamma ramps */ - if (!XF86VidModeSetGammaRamp((Display *)this->partition->site->data, (int)this->partition->partition, - (int)ramps.red_size, ramps.red, ramps.green, ramps.blue)) - return LIBGAMMA_GAMMA_RAMP_WRITE_FAILED; - return 0; -} diff --git a/src/lib/gamma-x-vidmode.h b/src/lib/gamma-x-vidmode.h deleted file mode 100644 index 618d5c4..0000000 --- a/src/lib/gamma-x-vidmode.h +++ /dev/null @@ -1,140 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_GAMMA_X_VIDMODE_H -#define LIBGAMMA_GAMMA_X_VIDMODE_H - -#ifndef HAVE_LIBGAMMA_METHOD_X_VIDMODE -# error Including gamma-x-vidmode.h without HAVE_LIBGAMMA_METHOD_X_VIDMODE -#endif - - -#include "libgamma-method.h" - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void libgamma_x_vidmode_method_capabilities(libgamma_method_capabilities_t *restrict); - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_vidmode_site_initialise(libgamma_site_state_t *restrict, char *restrict); - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void libgamma_x_vidmode_site_destroy(libgamma_site_state_t *restrict); - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_vidmode_site_restore(libgamma_site_state_t *restrict); - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_vidmode_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void libgamma_x_vidmode_partition_destroy(libgamma_partition_state_t *restrict) __attribute__((const)); - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_vidmode_partition_restore(libgamma_partition_state_t *restrict); - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -__attribute__((const)) -int libgamma_x_vidmode_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void libgamma_x_vidmode_crtc_destroy(libgamma_crtc_state_t *restrict) __attribute__((const)); - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_vidmode_crtc_restore(libgamma_crtc_state_t *restrict); - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error; on error refer to the error reports in `this` - */ -int libgamma_x_vidmode_get_crtc_information(libgamma_crtc_information_t *restrict, libgamma_crtc_state_t *restrict, int32_t); - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_vidmode_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_vidmode_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t); - - -#endif diff --git a/src/lib/libgamma-error.c.gpp b/src/lib/libgamma-error.c.gpp deleted file mode 100644 index d43f336..0000000 --- a/src/lib/libgamma-error.c.gpp +++ /dev/null @@ -1,178 +0,0 @@ -/* -*- c -*- */ -/* See LICENSE file for copyright and license details. */ -#include "libgamma-error.h" - - -#include <stddef.h> -#include <sys/types.h> -#include <string.h> -#include <stdio.h> -#include <errno.h> - - - -$>set -u -$>cd src/extract -$>export PATH=".:${PATH}" - - - -/** - * Group that the user needs to be a member of if - * `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned - */ -#ifndef __WIN32__ -__thread gid_t libgamma_group_gid = 0; -gid_t -libgamma_group_gid_get(void) -{ - return libgamma_group_gid; -} -void -libgamma_group_gid_set(gid_t value) -{ - libgamma_group_gid = value; -} -#else -short libgamma_group_gid = 0; -#endif - -/** - * Group that the user needs to be a member of if - * `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned, - * `NULL` if the name of the group `libgamma_group_gid` - * cannot be determined - */ -#ifndef __WIN32__ -__thread const char *libgamma_group_name = NULL; -const char * -libgamma_group_name_get(void) { - return libgamma_group_name; -} -void -libgamma_group_name_set(const char *value) -{ - libgamma_group_name = value; -} -#else -const char *libgamma_group_name = NULL; -#endif - - - -/** - * Prints an error to stderr in a `perror` fashion, - * however this function will not translate the `libgamma` - * errors into human-readable strings, it will simply - * print the name of the error. If the value `error_code` - * is the value of `LIBGAMMA_ERRNO_SET`, `perror` will be - * used to print the current error stored in `errno`. - * If `error_code` is non-negative (an `errno` value`), that - * value will be stored in `errno` and `perror` will be - * used to print it. Additionally, if the `error_code` is - * the value of `LIBGAMMA_DEVICE_REQUIRE_GROUP` the - * required group will be printed with its numerical value - * and, if known, its name. - * - * @param name The text to add at the beginning - * @param value The error code, may be an `errno` value - */ -void -libgamma_perror(const char *name, int error_code) -{ - const char *error; - long int gid; - - if (error_code >= 0) { - /* Print the stored errno value */ - errno = error_code; - perror(name); - } else if (error_code == LIBGAMMA_ERRNO_SET) { - /* Print errno */ - perror(name); - } else if (error_code == LIBGAMMA_DEVICE_REQUIRE_GROUP) { - /* Print the error name and the required group membership */ - error = libgamma_name_of_error(error_code); - gid = (long int)libgamma_group_gid; - if (!libgamma_group_name) { - /* Group name unknown */ - if (name && *name) - fprintf(stderr, "%s: %s: %ld\n", name, error, gid); - else - fprintf(stderr, "%s: %ld\n", error, gid); - } else { - /* Group name known, ID is second class */ - if (name && *name) - fprintf(stderr, "%s: %s: %s (%ld)\n", name, error, libgamma_group_name, gid); - else - fprintf(stderr, "%s: %s (%ld)\n", error, libgamma_group_name, gid); - } - } else if (error_code < LIBGAMMA_ERROR_MIN) { - /* If the error code does not exist, print "(?)" */ - if (name && *name) - fprintf(stderr, "%s: (?)\n", name); - else - fprintf(stderr, "(?)\n"); - } else { - /* Print the name of the error */ - if (name && *name) - fprintf(stderr, "%s: %s\n", name, libgamma_name_of_error(error_code)); - else - fprintf(stderr, "%s\n", libgamma_name_of_error(error_code)); - } -} - - -/** - * Returns the name of the definition associated with a `libgamma` error code - * - * @param value The error code - * @return The name of the definition associated with the error code, - * `NULL` if the error code does not exist; the return string - * should not be `free`:d - */ -const char * -libgamma_name_of_error(int value) -{ - /* Map from error codes to error names. - The output of $(libgamma-error-extract --list) - is sorted by error code in decreasing order */ - static const char* error_names[] = { -$>for error in $(libgamma-error-extract --list); do - "${error}", -$>done - }; - - /* Return `NULL` if the error code is invalid */ - if (value < LIBGAMMA_ERROR_MIN || value >= 0) - return NULL; - - /* Convert error code from {-1, -2, -3, ...} to {0, 1, 2, ...} - * and look up the error's name and return it */ - return error_names[-value - 1]; -} - - -/** - * Return the value of a `libgamma` error definition refered to by name - * - * @param name The name of the definition associated with the error code - * @return The error code, zero if the name does is `NULL` - * or does not refer to a `libgamma` error - */ -int -libgamma_value_of_error(const char *name) -{ - /* Return 0 (not a valid error code) if the error name is `NULL` */ - if (!name) - return 0; - - /* Test error names against `name` and return the value of the match error */ -$>for error in $(libgamma-error-extract --list); do - if (!strcmp(name, "${error}")) - return ${error}; -$>done - - /* Return 0 (not a valid error code) if the error name is unknown */ - return 0; -} diff --git a/src/lib/libgamma-error.h b/src/lib/libgamma-error.h deleted file mode 100644 index a026e79..0000000 --- a/src/lib/libgamma-error.h +++ /dev/null @@ -1,379 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_ERROR_H -#define LIBGAMMA_ERROR_H - -#if !defined(LIBGAMMA_CONFIG_H) && !defined(DEBUG) -# error libgamma-error.h should not be included directly, include libgamma.h instead -#endif - - -#include <sys/types.h> - -#ifndef __GNUC__ -# define __attribute__(x) -#endif - - -/** - * Group that the user needs to be a member of if - * `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned - */ -#ifndef __WIN32__ -extern __thread gid_t libgamma_group_gid; -gid_t libgamma_group_gid_get(void) __attribute__((pure)); -void libgamma_group_gid_set(gid_t value); -#else -extern short libgamma_group_gid; -#endif - -/** - * Group that the user needs to be a member of if - * `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned, - * `NULL` if the name of the group `libgamma_group_gid` - * cannot be determined - */ -#ifndef __WIN32__ -extern __thread const char *libgamma_group_name; -const char *libgamma_group_name_get(void) __attribute__((pure)); -void libgamma_group_name_set(const char *value); -#else -extern const char *libgamma_group_name; -#endif - - -/** - * `errno` has be set with a standard error number - * to indicate the what has gone wrong - */ -#define LIBGAMMA_ERRNO_SET (-1) - -/** - * The selected adjustment method does not exist - * or has been excluded at compile-time - */ -#define LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD (-2) - -/** - * The selected site does not exist - */ -#define LIBGAMMA_NO_SUCH_SITE (-3) - -/** - * The selected partition does not exist - */ -#define LIBGAMMA_NO_SUCH_PARTITION (-4) - -/** - * The selected CRTC does not exist - */ -#define LIBGAMMA_NO_SUCH_CRTC (-5) - -/** - * Counter overflowed when counting the number - * of available items - */ -#define LIBGAMMA_IMPOSSIBLE_AMOUNT (-6) - -/** - * The selected connector is disabled, it does - * not have a CRTC - */ -#define LIBGAMMA_CONNECTOR_DISABLED (-7) - -/** - * The selected CRTC could not be opened, - * reason unknown - */ -#define LIBGAMMA_OPEN_CRTC_FAILED (-8) - -/** - * The CRTC information field is not supported - * by the adjustment method - */ -#define LIBGAMMA_CRTC_INFO_NOT_SUPPORTED (-9) - -/** - * Failed to read the current gamma ramps for - * the selected CRTC, reason unknown - */ -#define LIBGAMMA_GAMMA_RAMP_READ_FAILED (-10) - -/** - * Failed to write the current gamma ramps for - * the selected CRTC, reason unknown - */ -#define LIBGAMMA_GAMMA_RAMP_WRITE_FAILED (-11) - -/** - * The specified ramp sizes does not match the - * ramps sizes returned by the adjustment methods - * in response to the query/command - */ -#define LIBGAMMA_GAMMA_RAMP_SIZE_CHANGED (-12) - -/** - * The specified ramp sizes are not identical - * which is required by the adjustment method - * - * (Only returned in debug mode) - */ -#define LIBGAMMA_MIXED_GAMMA_RAMP_SIZE (-13) - -/** - * The specified ramp sizes are not supported - * by the adjustment method - * - * (Only returned in debug mode) - */ -#define LIBGAMMA_WRONG_GAMMA_RAMP_SIZE (-14) - -/** - * The adjustment method reported that the gamma - * ramps size is 1, or perhaps even zero or negative - */ -#define LIBGAMMA_SINGLETON_GAMMA_RAMP (-15) - -/** - * The adjustment method failed to list - * available CRTC:s, reason unknown - */ -#define LIBGAMMA_LIST_CRTCS_FAILED (-16) - -/** - * Failed to acquire mode resources from the - * adjustment method - */ -#define LIBGAMMA_ACQUIRING_MODE_RESOURCES_FAILED (-17) - -/** - * The adjustment method reported that a negative - * number of partitions exists in the site - */ -#define LIBGAMMA_NEGATIVE_PARTITION_COUNT (-18) - -/** - * The adjustment method reported that a negative - * number of CRTC:s exists in the partition - */ -#define LIBGAMMA_NEGATIVE_CRTC_COUNT (-19) - -/** - * Device cannot be access becauses of - * insufficient permissions - */ -#define LIBGAMMA_DEVICE_RESTRICTED (-20) - -/** - * Device cannot be access, reason unknown - */ -#define LIBGAMMA_DEVICE_ACCESS_FAILED (-21) - -/** - * Device cannot be access, membership of the - * `libgamma_group_gid` (named by `libgamma_group_name` - * (can be `NULL`, if so `errno` may have been set - * to tell why)) is required - */ -#define LIBGAMMA_DEVICE_REQUIRE_GROUP (-22) - -/** - * The graphics card appear to have been removed - */ -#define LIBGAMMA_GRAPHICS_CARD_REMOVED (-23) - -/** - * The state of the requested information is unknown - */ -#define LIBGAMMA_STATE_UNKNOWN (-24) - -/** - * Failed to determine which connector the - * CRTC belongs to - */ -#define LIBGAMMA_CONNECTOR_UNKNOWN (-25) - -/** - * The detected connector type is not listed - * in this library and has to be updated - */ -#define LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED (-26) - -/** - * The detected subpixel order is not listed - * in this library and has to be updated - */ -#define LIBGAMMA_SUBPIXEL_ORDER_NOT_RECOGNISED (-27) - -/** - * The length of the EDID does not match that - * of any supported EDID structure revision - */ -#define LIBGAMMA_EDID_LENGTH_UNSUPPORTED (-28) - -/** - * The magic number in the EDID does not match - * that of any supported EDID structure revision - */ -#define LIBGAMMA_EDID_WRONG_MAGIC_NUMBER (-29) - -/** - * The EDID structure revision used by the - * monitor is not supported - */ -#define LIBGAMMA_EDID_REVISION_UNSUPPORTED (-30) - -/** - * The gamma characteristics field in the EDID - * is left unspecified - * - * (This could be considered a non-error) - */ -#define LIBGAMMA_GAMMA_NOT_SPECIFIED (-31) - -/** - * The checksum in the EDID is incorrect, all - * request information has been provided - * by you cannot count on it - */ -#define LIBGAMMA_EDID_CHECKSUM_ERROR (-32) - -/** - * Both of the errors `LIBGAMMA_GAMMA_NOT_SPECIFIED` - * and `LIBGAMMA_EDID_CHECKSUM_ERROR` have occurred - */ -#define LIBGAMMA_GAMMA_NOT_SPECIFIED_AND_EDID_CHECKSUM_ERROR (-33) - -/** - * Failed to query the gamma ramps size from the - * adjustment method, reason unknown - */ -#define LIBGAMMA_GAMMA_RAMPS_SIZE_QUERY_FAILED (-34) - -/** - * The selected partition could not be opened, - * reason unknown - */ -#define LIBGAMMA_OPEN_PARTITION_FAILED (-35) - -/** - * The selected site could not be opened, - * reason unknown - */ -#define LIBGAMMA_OPEN_SITE_FAILED (-36) - -/** - * Failed to query the adjustment method for - * its protocol version, reason unknown - */ -#define LIBGAMMA_PROTOCOL_VERSION_QUERY_FAILED (-37) - -/** - * The adjustment method's version of its - * protocol is not supported - */ -#define LIBGAMMA_PROTOCOL_VERSION_NOT_SUPPORTED (-38) - -/** - * The adjustment method failed to list - * available partitions, reason unknown - */ -#define LIBGAMMA_LIST_PARTITIONS_FAILED (-39) - -/** - * Partition exists by index, but the partition - * at that index does not exist - */ -#define LIBGAMMA_NULL_PARTITION (-40) - -/** - * There is not monitor connected to the - * connector of the selected CRTC - */ -#define LIBGAMMA_NOT_CONNECTED (-41) - -/** - * Data extraction from a reply from the - * adjustment method failed, reason unknown - */ -#define LIBGAMMA_REPLY_VALUE_EXTRACTION_FAILED (-42) - -/** - * No EDID property was found on the output - */ -#define LIBGAMMA_EDID_NOT_FOUND (-43) - -/** - * Failed to list properties on the output, - * reason unknown - */ -#define LIBGAMMA_LIST_PROPERTIES_FAILED (-44) - -/** - * Failed to query a property's value from - * the output, reason unknown - */ -#define LIBGAMMA_PROPERTY_VALUE_QUERY_FAILED (-45) - -/** - * A request for information on an output - * failed, reason unknown - */ -#define LIBGAMMA_OUTPUT_INFORMATION_QUERY_FAILED (-46) - - - -/** - * The number of the libgamma error with the - * lowest number. If this is lower than the - * number your program thinks it should be sould - * update your program for new errors. - */ -#define LIBGAMMA_ERROR_MIN (-46) - - - -/** - * Prints an error to stderr in a `perror` fashion, - * however this function will not translate the `libgamma` - * errors into human-readable strings, it will simply - * print the name of the error. If the value `error_code` - * is the value of `LIBGAMMA_ERRNO_SET`, `perror` will be - * used to print the current error stored in `errno`. - * If `error_code` is non-negative (an `errno` value`), that - * value will be stored in `errno` and `perror` will be - * used to print it. Additionally, if the `error_code` is - * the value of `LIBGAMMA_DEVICE_REQUIRE_GROUP` the - * required group will be printed with its numerical value - * and, if known, its name. - * - * @param name The text to add at the beginning - * @param error_code The error code, may be an `errno` value - */ -void libgamma_perror(const char *, int); - -/** - * Returns the name of the definition associated with a `libgamma` error code - * - * @param value The error code - * @return The name of the definition associated with the error code, - * `NULL` if the error code does not exist. The return string - * should not be `free`:d. - */ -const char *libgamma_name_of_error(int) __attribute__((const)); - -/** - * Return the value of a `libgamma` error definition refered to by name - * - * @param name The name of the definition associated with the error code - * @return The error code, zero if the name is `NULL` - * or does not refer to a `libgamma` error - */ -int libgamma_value_of_error(const char *) __attribute__((const)); - - - -#ifndef __GNUC__ -# undef __attribute__ -#endif - -#endif diff --git a/src/lib/libgamma-facade.c.gpp b/src/lib/libgamma-facade.c.gpp deleted file mode 100644 index 9f90ed6..0000000 --- a/src/lib/libgamma-facade.c.gpp +++ /dev/null @@ -1,1097 +0,0 @@ -/* -*- c -*- */ -/* See LICENSE file for copyright and license details. */ -#include "libgamma-facade.h" - -#include "libgamma-error.h" -#include "libgamma-method.h" -#include "gamma-helper.h" - - -/* Initialise the general preprocessor */ -$>cd src/extract -$>export PATH=".:${PATH}" - -/* Some general preprocessor we will use frequently */ -$< -get-methods () -{ - ./libgamma-method-extract --list --method | cut -d _ -f 1,2 --complement -} -lowercase () -{ - echo "$*" | sed -e y/QWERTYUIOPASDFGHJKLZXCVBNM/qwertyuiopasdfghjklzxcvbnm/ | sed -e s:core_graphics:cg:g -} -$> - -/* Include all adjustment methods that are enabled at compile-time. */ -$>for method in $(get-methods); do -#ifdef HAVE_LIBGAMMA_METHOD_${method} -# include "gamma-$(lowercase $method | sed -e s:_:-:g).h" -# ifndef HAVE_LIBGAMMA_METHODS -# define HAVE_LIBGAMMA_METHODS -# endif -#endif -$>done - -#include <unistd.h> -#include <stddef.h> -#include <stdint.h> -#include <stdlib.h> -#include <string.h> -#include <errno.h> - - -/* Some things to reduce warnings when we do - * not have any adjustment methods enabled */ -#ifndef HAVE_LIBGAMMA_METHODS -# define HAVE_NO_LIBGAMMA_METHODS -# ifdef __GNUC__ -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wsuggest-attribute=const" -# endif -#endif - - - -#ifdef HAVE_LIBGAMMA_METHODS -# ifdef HAVE_LIBGAMMA_METHOD_LINUX_DRM -/** - * Test whether a file descriptor refers to a VT - * - * @param fd The file descriptor - * @return Whether the file descriptor refers to a VT - */ -static int -libgamma_is_vt_proper(int fd) -{ - char buf[32], digit0; - - /* Get TTY */ - if (ttyname_r(fd, buf, sizeof(buf) / sizeof(char))) - return 0; - - /* Validate TTY path */ - if (!strcmp(buf, "/dev/console")) - return 1; - if (strstr(buf, "/dev/tty") != buf) - return 0; - - /* Validate TTY name */ - digit0 = buf[strlen("/dev/tty")]; - return '1' <= digit0 && digit0 <= '9'; -} -# endif - - -/** - * Test the availability of an adjustment method - * - * @param method The adjustment method - * @param operation Allowed values: - * 0: Pass if the environment suggests it will work but is not fake - * 1: Pass if the environment suggests it will work - * 2: Pass if real and not fake - * 3: Pass if real - * 4: Always pass - * Other values invoke undefined behaviour - * @return Whether the test passed - */ -static int -libgamma_list_method_test(int method, int operation) -{ - libgamma_method_capabilities_t caps; - libgamma_method_capabilities(&caps, method); - - switch (operation) { - case 0: - /* Methods that the environment suggests will work, excluding fake */ - if (caps.fake) - return 0; - /* fall through */ - - case 1: - /* Methods that the environment suggests will work, including fake */ - if (!caps.real) - return 0; -#ifdef HAVE_LIBGAMMA_METHOD_LINUX_DRM - if (method == LIBGAMMA_METHOD_LINUX_DRM) { - return libgamma_is_vt_proper(STDIN_FILENO) || - libgamma_is_vt_proper(STDOUT_FILENO) || - libgamma_is_vt_proper(STDERR_FILENO); - } -#endif -#ifdef HAVE_LIBGAMMA_METHOD_DUMMY - if (method == LIBGAMMA_METHOD_DUMMY) - return 0; -#endif - return caps.default_site_known; - - case 2: - /* All real non-fake methods */ - return caps.real && !caps.fake; - - case 3: - /* All real methods */ - return caps.real; - - default: - /* All methods */ - return 1; - } -} -#endif - - -/** - * List available adjustment methods by their order of preference based on the environment - * - * @param methods Output array of methods, should be able to hold `LIBGAMMA_METHOD_COUNT` elements - * @param buf_size The number of elements that fits in `methods`, it should be `LIBGAMMA_METHOD_COUNT`, - * This is used to avoid writing outside the output buffer if this library adds new - * adjustment methods without the users of the library recompiling - * @param operation Allowed values: - * 0: Methods that the environment suggests will work, excluding fake - * 1: Methods that the environment suggests will work, including fake - * 2: All real non-fake methods - * 3: All real methods - * 4: All methods - * Other values invoke undefined behaviour - * @return The number of element that have been stored in `methods`, or should - * have been stored if the buffer was large enough. - */ -size_t -libgamma_list_methods(int *restrict methods, size_t buf_size, int operation) -{ -#ifdef HAVE_NO_LIBGAMMA_METHODS - (void) methods; - (void) buf_size; - (void) operation; - return 0; -#else - size_t n = 0; - -$>for method in $(get-methods); do -#ifdef HAVE_LIBGAMMA_METHOD_${method} - if (libgamma_list_method_test(LIBGAMMA_METHOD_${method}, operation) && n++ < buf_size) - methods[n - 1] = LIBGAMMA_METHOD_${method}; -#endif -$>done - - return n; -#endif -} - - -/** - * Check whether an adjustment method is available, non-existing (invalid) methods will be - * identified as not available under the rationale that the library may be out of date - * - * @param method The adjustment method - * @return Whether the adjustment method is available - */ -int -libgamma_is_method_available(int method) -{ -#ifdef HAVE_NO_LIBGAMMA_METHODS - (void) method; - return 0; -#else - switch (method) { -$>for method in $(get-methods); do -#ifdef HAVE_LIBGAMMA_METHOD_${method} - case LIBGAMMA_METHOD_${method}: -#endif -$>done - return 1; - - default: - return 0; - } -#endif -} - - -/** - * Call the adjustment method's implementation of the called function - * - * @param 1 The adjustment method, you may use `.` instead of `->` when resolving it - * @param 2 `return` if the function returns a value, `break` otherwise - * @param 3 The base name of the function to call, that is, the name of the function - * this is expended into without the libgamma namespace prefix - * @param * The function's parameters - */ -$<switch () -$>{ - /* Read out macro's parameters */ -$<method="${1//./->}" - ctrl=$2 - fun=$3 - shift 3 - params="$*" -$>params="${params// /, }" - - switch (${method}) { -$>for adjmethod in $(get-methods); do -#ifdef HAVE_LIBGAMMA_METHOD_${adjmethod} - case LIBGAMMA_METHOD_${adjmethod}: - /* Call the adjustment method's implementation, either - * return or break after it depending on macro parameter's */ -$> if [ $ctrl = return ]; then - return - libgamma_$(lowercase $adjmethod)_${fun}(${params}); -$> elif [ ! $ctrl = return ]; then - break; -$> fi -#endif -$>done - - default: - /* If the adjustment method does not exists, either return - * that error, or do nothing because the function this is - * expanded into does return errors */ -$>if [ $ctrl = return ]; then - return LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD; -$>else - /* Method does not exists/excluded at compile-time. - * We will assume that this is not done... */ - break; -$>fi - } -$>} - - -/** - * Return the capabilities of an adjustment method - * - * @param this The data structure to fill with the method's capabilities - * @param method The adjustment method (display server and protocol) - */ -void -libgamma_method_capabilities(libgamma_method_capabilities_t *restrict this, int method) -{ - memset(this, 0, sizeof(libgamma_method_capabilities_t)); -$>switch method break method_capabilities this -} - - -/** - * Return the default site for an adjustment method - * - * @param method The adjustment method (display server and protocol) - * @return The default site, `NULL` if it cannot be determined or - * if multiple sites are not supported by the adjustment - * method; this value should not be `free`:d - */ -char * -libgamma_method_default_site(int method) -{ - const char *restrict var = libgamma_method_default_site_variable(method); - char *restrict env; - - /* Return `NULL` there is not variable to read */ - if (!var) - return NULL; - - /* Read the variable */ - env = getenv(var); - /* Return `NULL` if it does not exist (or is empty) */ - if (!env || !*env) - return NULL; - - /* Return the variable's value */ - return env; -} - - -/** - * Return the default variable that determines - * the default site for an adjustment method - * - * @param method The adjustment method (display server and protocol) - * @return The environ variables that is used to determine the - * default site. `NULL` if there is none, that is, if - * the method does not support multiple sites. - * This value should not be `free`:d. - */ -const char * -libgamma_method_default_site_variable(int method) -{ - switch (method) { -#ifdef HAVE_LIBGAMMA_METHOD_X_RANDR - case LIBGAMMA_METHOD_X_RANDR: - return "DISPLAY"; -#endif -#ifdef HAVE_LIBGAMMA_METHOD_X_VIDMODE - case LIBGAMMA_METHOD_X_VIDMODE: - return "DISPLAY"; -#endif - default: - return NULL; - } -} - - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param method The adjustment method (display server and protocol) - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocated on the stack. Note however that it will - * not be `free`:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_site_initialise(libgamma_site_state_t *restrict this, int method, char *restrict site) -{ - this->method = method; - this->site = site; -$>switch method return site_initialise this site -} - - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void -libgamma_site_destroy(libgamma_site_state_t *restrict this) -{ -$>switch this.method break site_destroy this - free(this->site); -} - - -/** - * Release all resources held by a site state - * and free the site state pointer - * - * @param this The site state - */ -void -libgamma_site_free(libgamma_site_state_t *restrict this) -{ - libgamma_site_destroy(this); - free(this); -} - - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_site_restore(libgamma_site_state_t *restrict this) -{ -$>switch this.method return site_restore this -} - - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_partition_initialise(libgamma_partition_state_t *restrict this, libgamma_site_state_t *restrict site, size_t partition) -{ - this->site = site; - this->partition = partition; -$>switch site.method return partition_initialise this site partition -} - - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void -libgamma_partition_destroy(libgamma_partition_state_t *restrict this) -{ -$>switch this.site.method break partition_destroy this -} - - -/** - * Release all resources held by a partition state - * and free the partition state pointer - * - * @param this The partition state - */ -void -libgamma_partition_free(libgamma_partition_state_t *restrict this) -{ - libgamma_partition_destroy(this); - free(this); -} - - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_partition_restore(libgamma_partition_state_t *restrict this) -{ -$>switch this.site.method return partition_restore this -} - - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The index of the CRTC within the partition - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_crtc_initialise(libgamma_crtc_state_t *restrict this, libgamma_partition_state_t *restrict partition, size_t crtc) -{ - this->partition = partition; - this->crtc = crtc; -$>switch partition.site.method return crtc_initialise this partition crtc -} - - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void -libgamma_crtc_destroy(libgamma_crtc_state_t *restrict this) -{ -$>switch this.partition.site.method break crtc_destroy this -} - - -/** - * Release all resources held by a CRTC state - * and free the CRTC state pointer - * - * @param this The CRTC state - */ -void -libgamma_crtc_free(libgamma_crtc_state_t *restrict this) -{ - libgamma_crtc_destroy(this); - free(this); -} - - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_crtc_restore(libgamma_crtc_state_t *restrict this) -{ -$>switch this.partition.site.method return crtc_restore this -} - - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error; on error refer to the error reports in `this` - */ -int -libgamma_get_crtc_information(libgamma_crtc_information_t *restrict this, libgamma_crtc_state_t *restrict crtc, int32_t fields) -{ -#ifdef HAVE_NO_LIBGAMMA_METHODS - (void) fields; -#endif - this->edid = NULL; - this->connector_name = NULL; -$>switch crtc.partition.site.method return get_crtc_information this crtc fields -} - - -/** - * Release all resources in an information data structure for a CRTC - * - * @param this The CRTC information - */ -void -libgamma_crtc_information_destroy(libgamma_crtc_information_t *restrict this) -{ - free(this->edid); - free(this->connector_name); -} - - -/** - * Release all resources in an information data structure for a CRTC - * and free the data structure pointer - * - * @param this The CRTC information - */ -void -libgamma_crtc_information_free(libgamma_crtc_information_t *restrict this) -{ - libgamma_crtc_information_destroy(this); - free(this); -} - - -/** - * Convert a raw representation of an EDID to a hexadecimal representation - * - * @param 1 Casing name - * @param 2 The hexadecimal alphabet - * @param edid The EDID in raw representation - * @param length The length of `edid` - * @return The EDID in hexadecimal representation - * `NULL` on allocation error, `errno` will be set accordingly - */ -$>behex_edid () -$>{ -char * -libgamma_behex_edid_${1}(const unsigned char *restrict edid, size_t length) -{ - char *restrict out; - size_t i; - - /* Allocate memory area for the output string */ - out = malloc((length * 2 + 1) * sizeof(char)); - if (!out) - return NULL; - - /* Translate from raw octets to hexadecimal */ - for (i = 0; i < length; i++) { - out[i * 2 + 0] = "${2}"[(edid[i] >> 4) & 15]; - out[i * 2 + 1] = "${2}"[(edid[i] >> 0) & 15]; - } - /* NUL-terminate the output string */ - out[length * 2] = '\0'; - - return out; -} -$>} - - -/** - * Convert a raw representation of an EDID to a lowercase hexadecimal representation - * - * @param edid The EDID in raw representation - * @param length The length of `edid` - * @return The EDID in lowercase hexadecimal representation, - * `NULL` on allocation error, `errno` will be set accordingly - */ -$>behex_edid lowercase 0123456789abcdef - - -/** - * Convert a raw representation of an EDID to an uppercase hexadecimal representation - * - * @param edid The EDID in raw representation - * @param length The length of `edid` - * @return The EDID in uppercase hexadecimal representation, - * NULL` on allocation error, `errno` will be set accordingly - */ -$>behex_edid uppercase 0123456789ABCDEF - - -/** - * Convert an hexadecimal representation of an EDID to a raw representation - * - * @param edid The EDID in hexadecimal representation - * @return The EDID in raw representation, it will be half the length - * of `edid` (the input value); `NULL` on allocation error or - * if the EDID is malformated, `errno` will be set accordingly - */ -unsigned char * -libgamma_unhex_edid(const char *restrict edid) -{ -#define not_range(lower, V, upper) (V < lower || upper < V) -#define is_not_hex(V) (not_range('0', V, '9') && not_range('a', V, 'f') && not_range('A', V, 'F')) - - unsigned char *restrict out; - size_t i, n = strlen(edid); - char a, b; - - /* Check that the length of the strings is even, - * otherwise it cannot represent bytes */ - if (n & 1) { - errno = EINVAL; - return NULL; - } - - /* Allocate memory area for output octet array */ - n /= 2 * sizeof(unsigned char); - out = malloc(n); - if (!out) - return NULL; - - /* Convert to raw octet array */ - for (i = 0; i < n; i++) { - /* Get the next character pair that, it represents an octet. */ - a = edid[i * 2 + 0]; - b = edid[i * 2 + 1]; - - /* Verify that the input is in hexadecimal */ - if (is_not_hex(a) || is_not_hex(b)) { - free(out); - errno = EINVAL; - return NULL; - } - - /* Convert each chararacter to raw format */ - a = (char)((a & 15) + (a > '9' ? 9 : 0)); - b = (char)((b & 15) + (b > '9' ? 9 : 0)); - - /* Combine the two characters into one octet */ - out[i] = (unsigned char)((a << 4) | b); - } - - return out; - -#undef is_hex -#undef not_range -} - - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t *restrict ramps) -{ - libgamma_gamma_ramps_any_t ramps_; - -#ifdef HAVE_NO_LIBGAMMA_METHODS - (void) ramps; -#endif - - switch (this->partition->site->method) { - /* Methods other than Quartz/CoreGraphics uses 16-bit integers */ -$>for method in $(get-methods | grep -v QUARTZ_CORE_GRAPHICS); do -#ifdef HAVE_LIBGAMMA_METHOD_${method} - case LIBGAMMA_METHOD_${method}: - return libgamma_$(lowercase $method)_crtc_get_gamma_ramps16(this, ramps); -#endif - - /* The Quartz/CoreGraphics method uses single precision float */ -$>done -#ifdef HAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS - case LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS: - ramps_.bits16 = *ramps; - return libgamma_translated_ramp_get(this, &ramps_, 16, -1, libgamma_crtc_get_gamma_rampsf); -#endif - - /* The selected method does not exist */ - default: - return LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD; - } -} - - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t ramps) -{ - libgamma_gamma_ramps_any_t ramps_; - -#ifdef HAVE_NO_LIBGAMMA_METHODS - (void) ramps; -#endif - - switch (this->partition->site->method) { - /* Methods other than Quartz/CoreGraphics uses 16-bit integers */ -$>for method in $(get-methods | grep -v QUARTZ_CORE_GRAPHICS); do -#ifdef HAVE_LIBGAMMA_METHOD_${method} - case LIBGAMMA_METHOD_${method}: - return libgamma_$(lowercase $method)_crtc_set_gamma_ramps16(this, ramps); -#endif -$>done - - /* The Quartz/CoreGraphics method uses single precision float */ -#ifdef HAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS - case LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS: - ramps_.bits16 = ramps; - return libgamma_translated_ramp_set(this, ramps_, 16, -1, libgamma_crtc_set_gamma_rampsf); -#endif - - /* The selected method does not exist. */ - default: - return LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD; - } -} - - - -/** - * Set or get the gamma ramps for a CRTC, non-16-bit gamma-depth version - * - * @param 1 Either `get` or `set`, for the action that the name of value implies - * @param 2 The `ramp*` pattern for the ramp structure and function to call - * @param 3 Either of `bit8`, `bit16`, `bit32`, `bit64`, `float_single`, `float_double`; - * rather self-explanatory - * @param 4 The number of bits in the gamma depth, -1 for single precision float, - * (`float`) and -2 for double percition float (`double`) - * @param this The CRTC state - * @param ramps The gamma ramps to apply, or - * the gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps () -$>{ -$<action=$1 - ramps=$2 - type=$3 - bits=$4 - p= - if [ $action = get ]; then - p='*' - fi -$> -int -libgamma_crtc_${action}_gamma_${ramps}(libgamma_crtc_state_t *restrict this, libgamma_gamma_${ramps}_t${p:+* restrict} ramps) -{ - libgamma_gamma_ramps_any_t ramps_; - switch (this->partition->site->method) { - /* The dummy method supports all ramp depths */ -#ifdef HAVE_LIBGAMMA_METHOD_DUMMY - case LIBGAMMA_METHOD_DUMMY: - return libgamma_dummy_crtc_${action}_gamma_${ramps}(this, ramps); -#endif - - /* The Quartz/CoreGraphics method uses single precision float */ -#ifdef HAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS - case LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS: -$>if [ $bits = -1 ]; then - /* Single precision float is used */ - return libgamma_quartz_cg_crtc_${action}_gamma_${ramps}(this, ramps); -$>else - /* Something else is used and we convert to Single precision float */ - ramps_.${type} = ${p}ramps; - return libgamma_translated_ramp_${action}(this, ${p:+&}ramps_, ${bits}, -1, libgamma_crtc_${action}_gamma_rampsf); -$>fi -#endif - - /* Other methods use 16-bit integers. */ - default: - ramps_.${type} = ${p}ramps; - return libgamma_translated_ramp_${action}(this, ${p:+&}ramps_, ${bits}, 16, libgamma_crtc_${action}_gamma_ramps16); - } -} -$>} - - - -/** - * Get the current gamma ramps for a CRTC, 8-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps get ramps8 bits8 8 - - -/** - * Set the gamma ramps for a CRTC, 32-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps set ramps8 bits8 8 - - - -/** - * Get the current gamma ramps for a CRTC, 32-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps get ramps32 bits32 32 - - -/** - * Set the gamma ramps for a CRTC, 32-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps set ramps32 bits32 32 - - - -/** - * Get the current gamma ramps for a CRTC, 64-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps get ramps64 bits64 64 - - -/** - * Set the gamma ramps for a CRTC, 64-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps set ramps64 bits64 64 - - -/** - * Get the current gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps get rampsf float_single -1 - - -/** - * Set the gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps set rampsf float_single -1 - - - -/** - * Get the current gamma ramps for a CRTC, `double` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps get rampsd float_double -2 - - -/** - * Set the gamma ramps for a CRTC, `double` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps set rampsd float_double -2 - - - -/** - * Set the gamma ramps for a CRTC - * - * Note that this will probably involve the library allocating temporary data - * - * @param 1 The data type for the ramp stop elements - * @param 2 The `ramp*` pattern for the ramp structure and function to call - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_gamma_ramps_f () -$>{ -int -libgamma_crtc_set_gamma_${2}_f(libgamma_crtc_state_t *restrict this, libgamma_gamma_${2}_fun *red_function, - libgamma_gamma_${2}_fun *green_function, libgamma_gamma_${2}_fun *blue_function) -{ - libgamma_crtc_information_t info; - libgamma_gamma_${2}_t ramps; - size_t i, n; - int e; - - /* Get the size of the gamma ramps */ - if (libgamma_get_crtc_information(&info, this, LIBGAMMA_CRTC_INFO_GAMMA_SIZE)) { - e = info.gamma_size_error; - if (e < 0) - return e; - errno = e; - return LIBGAMMA_ERRNO_SET; - } - - /* Copy the size of the gamma ramps and calculte the grand size */ - n = ramps. red_size = info. red_gamma_size; - n += ramps.green_size = info.green_gamma_size; - n += ramps. blue_size = info. blue_gamma_size; - - /* Allocate gamma ramps */ - ramps. red = malloc(n * sizeof(${1})); - ramps.green = &ramps. red[ramps. red_size]; - ramps. blue = &ramps.green[ramps.green_size]; - if (!ramps.red) - return LIBGAMMA_ERRNO_SET; - - /* Generate the gamma ramp for the red chennel */ - for (i = 0, n = ramps.red_size; i < n; i++) - ramps.red[i] = red_function((float)i / (float)(n - 1)); - - /* Generate the gamma ramp for the green chennel */ - for (i = 0, n = ramps.green_size; i < n; i++) - ramps.green[i] = green_function((float)i / (float)(n - 1)); - - /* Generate the gamma ramp for the blue chennel */ - for (i = 0, n = ramps.blue_size; i < n; i++) - ramps.blue[i] = blue_function((float)i / (float)(n - 1)); - - /* Apply the gamma ramps */ - e = libgamma_crtc_set_gamma_${2}(this, ramps); - free(ramps.red); - return e; -} -$>} - - -/** - * Set the gamma ramps for a CRTC, 8-bit gamma-depth function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_gamma_ramps_f uint8_t ramps8 - - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_gamma_ramps_f uint16_t ramps16 - - -/** - * Set the gamma ramps for a CRTC, 32-bit gamma-depth function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_gamma_ramps_f uint32_t ramps32 - - -/** - * Set the gamma ramps for a CRTC, 64-bit gamma-depth function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_gamma_ramps_f uint64_t ramps64 - - -/** - * Set the gamma ramps for a CRTC, `float` function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_gamma_ramps_f float rampsf - - -/** - * Set the gamma ramps for a CRTC, `double` function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_gamma_ramps_f double rampsd - - - -#ifdef HAVE_NO_LIBGAMMA_METHODS -# ifdef __GNUC__ -# pragma GCC diagnostic pop -# endif -#endif diff --git a/src/lib/libgamma-facade.h b/src/lib/libgamma-facade.h deleted file mode 100644 index 596094c..0000000 --- a/src/lib/libgamma-facade.h +++ /dev/null @@ -1,537 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_FACADE_H -#define LIBGAMMA_FACADE_H - -#if !defined(LIBGAMMA_CONFIG_H) && !defined(DEBUG) -# error libgamma-facade.h should not be included directly, include libgamma.h instead -#endif - - -#include "libgamma-method.h" - -#include <stddef.h> -#include <stdint.h> - -#ifndef __GNUC__ -# define __attribute__(x) -#endif - - - -/** - * Mapping function from [0, 1] float encoding value to [0, 2⁸ − 1] integer output value - * - * @param encoding [0, 1] float encoding value - * @return [0, 2⁸ − 1] integer output value - */ -typedef uint8_t libgamma_gamma_ramps8_fun(float); - -/** - * Mapping function from [0, 1] float encoding value to [0, 2¹⁶ − 1] integer output value - * - * @param encoding [0, 1] float encoding value - * @return [0, 2¹⁶ − 1] integer output value - */ -typedef uint16_t libgamma_gamma_ramps16_fun(float); - -/** - * Mapping function from [0, 1] float encoding value to [0, 2³² − 1] integer output value - * - * @param encoding [0, 1] float encoding value - * @return [0, 2³² − 1] integer output value - */ -typedef uint32_t libgamma_gamma_ramps32_fun(float); - -/** - * Mapping function from [0, 1] float encoding value to [0, 2⁶⁴ − 1] integer output value - * - * @param encoding [0, 1] float encoding value - * @return [0, 2⁶⁴ − 1] integer output value - */ -typedef uint64_t libgamma_gamma_ramps64_fun(float); - -/** - * Mapping function from [0, 1] float encoding value to [0, 1] float output value - * - * @param encoding [0, 1] float encoding value - * @return [0, 1] float output value - */ -typedef float libgamma_gamma_rampsf_fun(float); - -/** - * Mapping function from [0, 1] double precision float encoding - * value to [0, 1] double precision float output value - * - * @param encoding [0, 1] float encoding value - * @return [0, 1] float output value - */ -typedef double libgamma_gamma_rampsd_fun(double); - - - -/** - * List available adjustment methods by their order of preference based on the environment - * - * @param methods Output array of methods, should be able to hold `LIBGAMMA_METHOD_COUNT` elements - * @param buf_size The number of elements that fits in `methods`, it should be `LIBGAMMA_METHOD_COUNT`, - * This is used to avoid writing outside the output buffer if this library adds new - * adjustment methods without the users of the library recompiling - * @param operation Allowed values: - * 0: Methods that the environment suggests will work, excluding fake - * 1: Methods that the environment suggests will work, including fake - * 2: All real non-fake methods - * 3: All real methods - * 4: All methods - * Other values invoke undefined behaviour - * @return The number of element that have been stored in `methods`, or should - * have been stored if the buffer was large enough - */ -size_t libgamma_list_methods(int *restrict, size_t, int); - -/** - * Check whether an adjustment method is available, non-existing (invalid) methods will be - * identified as not available under the rationale that the library may be out of date - * - * @param method The adjustment method - * @return Whether the adjustment method is available - */ -int libgamma_is_method_available(int) __attribute__((const)); - -/** - * Return the capabilities of an adjustment method - * - * @param this The data structure to fill with the method's capabilities - * @param method The adjustment method (display server and protocol) - */ -void libgamma_method_capabilities(libgamma_method_capabilities_t *restrict, int); - -/** - * Return the default site for an adjustment method - * - * @param method The adjustment method (display server and protocol) - * @return The default site, `NULL` if it cannot be determined or - * if multiple sites are not supported by the adjustment - * method; this value should not be `free`:d - */ -char *libgamma_method_default_site(int); - -/** - * Return the default variable that determines - * the default site for an adjustment method - * - * @param method The adjustment method (display server and protocol) - * @return The environ variables that is used to determine the - * default site, `NULL` if there is none, that is, if - * the method does not support multiple sites; this - * value should not be `free`:d - */ -const char *libgamma_method_default_site_variable(int) __attribute__((const)); - - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param method The adjustment method (display server and protocol) - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be `free`:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_site_initialise(libgamma_site_state_t *restrict, int, char *restrict); - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void libgamma_site_destroy(libgamma_site_state_t *restrict); - -/** - * Release all resources held by a site state - * and free the site state pointer - * - * @param this The site state - */ -void libgamma_site_free(libgamma_site_state_t *restrict); - -/** - * Restore the gamma ramps all CRTC:s within a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_site_restore(libgamma_site_state_t *restrict); - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void libgamma_partition_destroy(libgamma_partition_state_t *restrict); - -/** - * Release all resources held by a partition state - * and free the partition state pointer - * - * @param this The partition state - */ -void libgamma_partition_free(libgamma_partition_state_t *restrict); - -/** - * Restore the gamma ramps all CRTC:s within a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_partition_restore(libgamma_partition_state_t *restrict); - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The index of the CRTC within the partition - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void libgamma_crtc_destroy(libgamma_crtc_state_t *restrict); - -/** - * Release all resources held by a CRTC state - * and free the CRTC state pointer - * - * @param this The CRTC state - */ -void libgamma_crtc_free(libgamma_crtc_state_t *restrict); - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_restore(libgamma_crtc_state_t *restrict); - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error; on error refer to the error reports in `this` - */ -int libgamma_get_crtc_information(libgamma_crtc_information_t *restrict, libgamma_crtc_state_t *restrict, int32_t); - -/** - * Release all resources in an information data structure for a CRTC - * - * @param this The CRTC information - */ -void libgamma_crtc_information_destroy(libgamma_crtc_information_t *restrict); - -/** - * Release all resources in an information data structure for a CRTC - * and free the data structure pointer - * - * @param this The CRTC information - */ -void libgamma_crtc_information_free(libgamma_crtc_information_t *restrict); - -/** - * Convert a raw representation of an EDID to a lowercase hexadecimal representation - * - * @param edid:const unsigned char* The EDID in raw representation - * @param length:size_t The length of `edid` - * @return :char* The EDID in lowercase hexadecimal representation, - * `NULL` on allocation error, `errno` will be set accordingly - */ -#define libgamma_behex_edid(edid, length) libgamma_behex_edid_lowercase((edid), (length)) - -/** - * Convert a raw representation of an EDID to a lowercase hexadecimal representation - * - * @param edid The EDID in raw representation - * @param length The length of `edid` - * @return The EDID in lowercase hexadecimal representation - * `NULL` on allocation error, `errno` will be set accordingly - */ -char *libgamma_behex_edid_lowercase(const unsigned char *restrict, size_t); - -/** - * Convert a raw representation of an EDID to an uppercase hexadecimal representation - * - * @param edid The EDID in raw representation - * @param length The length of `edid` - * @return The EDID in uppercase hexadecimal representation, - * NULL` on allocation error, `errno` will be set accordingly - */ -char *libgamma_behex_edid_uppercase(const unsigned char *restrict, size_t); - -/** - * Convert an hexadecimal representation of an EDID to a raw representation - * - * @param edid The EDID in hexadecimal representation - * @return The EDID in raw representation, it will be half the length - * of `edid` (the input value); `NULL` on allocation error or - * if the EDID is malformated, `errno` will be set accordingly - */ -unsigned char *libgamma_unhex_edid(const char *restrict); - - -/** - * Get the current gamma ramps for a CRTC, 8-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_get_gamma_ramps8(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps8_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 8-bit gamma-depth version. - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_set_gamma_ramps8(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps8_t); - - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t) __attribute__((hot)); - - -/** - * Get the current gamma ramps for a CRTC, 32-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_get_gamma_ramps32(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps32_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 32-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_set_gamma_ramps32(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps32_t); - - -/** - * Get the current gamma ramps for a CRTC, 64-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_get_gamma_ramps64(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps64_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 64-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_set_gamma_ramps64(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps64_t); - - -/** - * Set the gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_set_gamma_rampsf(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsf_t); - -/** - * Get the current gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_get_gamma_rampsf(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsf_t *restrict); - - -/** - * Get the current gamma ramps for a CRTC, `double` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_get_gamma_rampsd(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsd_t *restrict); - -/** - * Set the gamma ramps for a CRTC, `double` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_set_gamma_rampsd(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsd_t); - - -/** - * Set the gamma ramps for a CRTC, 8-bit gamma-depth function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -__attribute__((cold)) -int libgamma_crtc_set_gamma_ramps8_f(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps8_fun *, - libgamma_gamma_ramps8_fun *, libgamma_gamma_ramps8_fun *); - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -__attribute__((cold)) -int libgamma_crtc_set_gamma_ramps16_f(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_fun *, - libgamma_gamma_ramps16_fun *, libgamma_gamma_ramps16_fun *); - -/** - * Set the gamma ramps for a CRTC, 32-bit gamma-depth function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -__attribute__((cold)) -int libgamma_crtc_set_gamma_ramps32_f(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps32_fun *, - libgamma_gamma_ramps32_fun *, libgamma_gamma_ramps32_fun *); - -/** - * Set the gamma ramps for a CRTC, 64-bit gamma-depth function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -__attribute__((cold)); -int libgamma_crtc_set_gamma_ramps64_f(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps64_fun *, - libgamma_gamma_ramps64_fun *, libgamma_gamma_ramps64_fun *); - -/** - * Set the gamma ramps for a CRTC, `float` function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -__attribute__((cold)) -int libgamma_crtc_set_gamma_rampsf_f(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsf_fun *, - libgamma_gamma_rampsf_fun *, libgamma_gamma_rampsf_fun *); - -/** - * Set the gamma ramps for a CRTC, `double` function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -__attribute__((cold)) -int libgamma_crtc_set_gamma_rampsd_f(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsd_fun *, - libgamma_gamma_rampsd_fun *, libgamma_gamma_rampsd_fun *); - - -#ifndef __GNUC__ -# undef __attribute__ -#endif - -#endif diff --git a/src/lib/libgamma-method.c b/src/lib/libgamma-method.c deleted file mode 100644 index cf9a23b..0000000 --- a/src/lib/libgamma-method.c +++ /dev/null @@ -1,324 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include "libgamma-method.h" - - -#include <stddef.h> -#include <stdint.h> -#include <stdlib.h> - - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int -libgamma_gamma_ramps8_initialise(libgamma_gamma_ramps8_t *restrict this) -{ - size_t n = this->red_size + this->green_size + this->blue_size; - this->red = malloc(n * sizeof(uint8_t)); - this->green = &this-> red[this-> red_size]; - this->blue = &this->green[this->green_size]; - return this->red ? 0 : -1; -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps8_initialise` or otherwise - * initialises in the proper manner - * - * @param this The gamma ramps - */ -void -libgamma_gamma_ramps8_destroy(libgamma_gamma_ramps8_t *restrict this) -{ - free(this->red); -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps8_initialise` or otherwise - * initialises in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void -libgamma_gamma_ramps8_free(libgamma_gamma_ramps8_t *restrict this) -{ - free(this->red); - free(this); -} - - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int -libgamma_gamma_ramps16_initialise(libgamma_gamma_ramps16_t *restrict this) -{ - size_t n = this->red_size + this->green_size + this->blue_size; -#ifdef HAVE_LIBGAMMA_METHOD_LINUX_DRM - /* Valgrind complains about us reading uninitialize memory if we just use malloc */ - this->red = calloc(n, sizeof(uint16_t)); -#else - this->red = malloc(n * sizeof(uint16_t)); -#endif - this->green = &this-> red[this-> red_size]; - this->blue = &this->green[this->green_size]; - return this->red ? 0 : -1; -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps_initialise` or otherwise - * initialises in the proper manner - * - * @param this The gamma ramps - */ -void -libgamma_gamma_ramps16_destroy(libgamma_gamma_ramps16_t *restrict this) -{ - free(this->red); -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps_initialise` or otherwise - * initialises in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void -libgamma_gamma_ramps16_free(libgamma_gamma_ramps16_t *restrict this) -{ - free(this->red); - free(this); -} - - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int -libgamma_gamma_ramps32_initialise(libgamma_gamma_ramps32_t *restrict this) -{ - size_t n = this->red_size + this->green_size + this->blue_size; - this->red = malloc(n * sizeof(uint32_t)); - this->green = &this-> red[this-> red_size]; - this->blue = &this->green[this->green_size]; - return this->red ? 0 : -1; -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps32_initialise` or otherwise - * initialises in the proper manner - * - * @param this The gamma ramps - */ -void -libgamma_gamma_ramps32_destroy(libgamma_gamma_ramps32_t *restrict this) -{ - free(this->red); -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps32_initialise` or otherwise - * initialises in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void -libgamma_gamma_ramps32_free(libgamma_gamma_ramps32_t *restrict this) -{ - free(this->red); - free(this); -} - - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int -libgamma_gamma_ramps64_initialise(libgamma_gamma_ramps64_t *restrict this) -{ - size_t n = this->red_size + this->green_size + this->blue_size; - this->red = malloc(n * sizeof(uint64_t)); - this->green = &this-> red[this-> red_size]; - this->blue = &this->green[this->green_size]; - return this->red ? 0 : -1; -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps64_initialise` or otherwise - * initialises in the proper manner - * - * @param this The gamma ramps - */ -void -libgamma_gamma_ramps64_destroy(libgamma_gamma_ramps64_t *restrict this) -{ - free(this->red); -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps64_initialise` or otherwise - * initialises in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void -libgamma_gamma_ramps64_free(libgamma_gamma_ramps64_t *restrict this) -{ - free(this->red); - free(this); -} - - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int -libgamma_gamma_rampsf_initialise(libgamma_gamma_rampsf_t *restrict this) -{ - size_t n = this->red_size + this->green_size + this->blue_size; - this->red = malloc(n * sizeof(float)); - this->green = &this-> red[this-> red_size]; - this->blue = &this->green[this->green_size]; - return this->red ? 0 : -1; -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_rampsf_initialise` or otherwise - * initialises in the proper manner - * - * @param this The gamma ramps - */ -void -libgamma_gamma_rampsf_destroy(libgamma_gamma_rampsf_t *restrict this) -{ - free(this->red); -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_rampsf_initialise` or otherwise - * initialises in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void -libgamma_gamma_rampsf_free(libgamma_gamma_rampsf_t *restrict this) -{ - free(this->red); - free(this); -} - - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int -libgamma_gamma_rampsd_initialise(libgamma_gamma_rampsd_t *restrict this) -{ - size_t n = this->red_size + this->green_size + this->blue_size; - this->red = malloc(n * sizeof(double)); - this->green = &this-> red[this-> red_size]; - this->blue = &this->green[this->green_size]; - return this->red ? 0 : -1; -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_rampsd_initialise` or otherwise - * initialises in the proper manner - * - * @param this The gamma ramps - */ -void -libgamma_gamma_rampsd_destroy(libgamma_gamma_rampsd_t *restrict this) -{ - free(this->red); -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_rampsd_initialise` or otherwise - * initialises in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void -libgamma_gamma_rampsd_free(libgamma_gamma_rampsd_t *restrict this) -{ - free(this->red); - free(this); -} diff --git a/src/lib/libgamma-method.h b/src/lib/libgamma-method.h deleted file mode 100644 index e2a35c6..0000000 --- a/src/lib/libgamma-method.h +++ /dev/null @@ -1,1323 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_METHOD_H -#define LIBGAMMA_METHOD_H - -#if !defined(LIBGAMMA_CONFIG_H) && !defined(DEBUG) -# error libgamma-method.h should not be included directly, include libgamma.h instead -#endif - - -#ifndef __GNUC__ -# define __attribute__(x) -#endif - -#include <stddef.h> -#include <stdint.h> - - - -/** - * The identifier for the dummy adjustment method - * - * This method can be configured and is useful for - * testing your program's ability to handle errors - */ -#define LIBGAMMA_METHOD_DUMMY 0 - -/** - * The identifier for the adjustment method with - * uses the RandR protocol under the X display server - */ -#define LIBGAMMA_METHOD_X_RANDR 1 - -/** - * The identifier for the adjustment method with - * uses the VidMode protocol under the X display server - * - * This is an older alternative to RandR that can - * work on some drivers that are not supported by RandR, - * however it can only control the primary CRTC per - * screen (partition) - */ -#define LIBGAMMA_METHOD_X_VIDMODE 2 - -/** - * The identifier for the Direct Rendering Manager - * adjustment method that is available in Linux - * (built in to the Linux kernel with a userland - * library for access) and is a part of the - * Direct Rendering Infrastructure. - * - * This adjustment method will work when you are - * in non-graphical mode; however a display server - * cannot be started while this is running, but it - * can be started while a display server is running - */ -#define LIBGAMMA_METHOD_LINUX_DRM 3 - -/** - * The identifier for the Graphics Device Interface - * adjustment method that is available in Windows - * - * This method is not well tested; it can be compiled - * to be available under X.org using a translation layer - */ -#define LIBGAMMA_METHOD_W32_GDI 4 - -/** - * The identifier for the CoreGraphics adjustment - * method that is available in Mac OS X that can - * adjust gamma ramps under the Quartz display server - * - * This method is not well tested; it can be compiled - * to be available under X.org using a translation layer - */ -#define LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS 5 - - -/** - * The index of the last gamma method, neither it - * nor any index before it may actually be supported - * as it could have been disabled at compile-time - */ -#define LIBGAMMA_METHOD_MAX 5 - -/** - * The number adjustment methods provided by this library. - * Note however that this includes adjstment methods that - * have been removed at compile-time - */ -#define LIBGAMMA_METHOD_COUNT (LIBGAMMA_METHOD_MAX + 1) - - - -/** - * Capabilities of adjustment methods - */ -typedef struct libgamma_method_capabilities { - /** - * OR of the CRTC information fields in `libgamma_crtc_information_t` - * that may (but can fail) be read successfully - */ - int32_t crtc_information; - - /** - * Whether the default site is known, if true the site is integrated - * to the system or can be determined using environment variables - */ - unsigned default_site_known : 1; - - /** - * Whether the adjustment method supports multiple sites rather - * than just the default site - */ - unsigned multiple_sites : 1; - - /** - * Whether the adjustment method supports multiple partitions - * per site - */ - unsigned multiple_partitions : 1; - - /** - * Whether the adjustment method supports multiple CRTC:s - * per partition per site - */ - unsigned multiple_crtcs : 1; - - /** - * Whether the partition to graphics card is a bijection - */ - unsigned partitions_are_graphics_cards : 1; - - /** - * Whether the adjustment method supports `libgamma_site_restore` - */ - unsigned site_restore : 1; - - /** - * Whether the adjustment method supports `libgamma_partition_restore` - */ - unsigned partition_restore : 1; - - /** - * Whether the adjustment method supports `libgamma_crtc_restore` - */ - unsigned crtc_restore : 1; - - /** - * Whether the `red_gamma_size`, `green_gamma_size` and `blue_gamma_size` - * fields in `libgamma_crtc_information_t` will always have the same - * values as each other for the adjustment method - */ - unsigned identical_gamma_sizes : 1; - - /** - * Whether the `red_gamma_size`, `green_gamma_size` and `blue_gamma_size` - * fields in `libgamma_crtc_information_t` will always be filled with the - * same value for the adjustment method - */ - unsigned fixed_gamma_size : 1; - - /** - * Whether the `gamma_depth` field in `libgamma_crtc_information_t` - * will always be filled with the same value for the adjustment method - */ - unsigned fixed_gamma_depth : 1; - - /** - * Whether the adjustment method will actually perform adjustments - */ - unsigned real : 1; - - /** - * Whether the adjustment method is implement using a translation layer - */ - unsigned fake : 1; - - /** - * Whether adjustments are undone when the process disconnects from - * the display server - */ - unsigned auto_restore : 1; - -} libgamma_method_capabilities_t; - - - -/** - * 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. - */ -typedef struct libgamma_site_state { - /** - * Adjustment method implementation specific data. - * You as a user of this library should not touch this. - */ - void *data; - - /** - * 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 `NULL` or a string. - * `NULL` 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. - */ - char *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; - -} libgamma_site_state_t; - - -/** - * 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. - */ -typedef struct libgamma_partition_state { - /** - * Adjustment method implementation specific data - * - * You as a user of this library should not touch this - */ - void *data; - - /** - * The site this partition belongs to - */ - libgamma_site_state_t *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; - -} libgamma_partition_state_t; - - -/** - * 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 - */ -typedef struct libgamma_crtc_state { - /** - * Adjustment method implementation specific data - * - * You as a user of this library should not touch this - */ - void *data; - - /** - * The partition this CRTC belongs to - */ - libgamma_partition_state_t *partition; - - /** - * The index of the CRTC within its partition - */ - size_t crtc; - -} libgamma_crtc_state_t; - - -/** - * Types for connectors - */ -typedef enum libgamma_connector_type { - /** - * The adjustment method does not know the connector's type - * - * (This could be considered an error) - */ - LIBGAMMA_CONNECTOR_TYPE_Unknown = 0, - - /** - * Video Graphics Array (VGA) - */ - LIBGAMMA_CONNECTOR_TYPE_VGA, - - /** - * Digital Visual Interface, unknown type - */ - LIBGAMMA_CONNECTOR_TYPE_DVI, - - /** - * Digital Visual Interface, integrated (DVI-I) - */ - LIBGAMMA_CONNECTOR_TYPE_DVII, - - /** - * Digital Visual Interface, digital only (DVI-D) - */ - LIBGAMMA_CONNECTOR_TYPE_DVID, - - /** - * Digital Visual Interface, analogue only (DVI-A) - */ - LIBGAMMA_CONNECTOR_TYPE_DVIA, - - /** - * Composite video - */ - LIBGAMMA_CONNECTOR_TYPE_Composite, - - /** - * Separate Video (S-video) - */ - LIBGAMMA_CONNECTOR_TYPE_SVIDEO, - - /** - * Low-voltage differential signaling (LVDS) - */ - LIBGAMMA_CONNECTOR_TYPE_LVDS, - - /** - * Component video, usually separate cables for each channel - */ - LIBGAMMA_CONNECTOR_TYPE_Component, - - /** - * 9 pin DIN (Deutsches Institut für Normung) connector - */ - LIBGAMMA_CONNECTOR_TYPE_9PinDIN, - - /** - * DisplayPort - */ - LIBGAMMA_CONNECTOR_TYPE_DisplayPort, - - /** - * High-Definition Multimedia Interface (HDMI), unknown type - */ - LIBGAMMA_CONNECTOR_TYPE_HDMI, - - /** - * High-Definition Multimedia Interface, type A (HDMI-A) - */ - LIBGAMMA_CONNECTOR_TYPE_HDMIA, - - /** - * High-Definition Multimedia Interface, type B (HDMI-B) - */ - LIBGAMMA_CONNECTOR_TYPE_HDMIB, - - /** - * Television, unknown connector - */ - LIBGAMMA_CONNECTOR_TYPE_TV, - - /** - * Embedded DisplayPort (eDP) - */ - LIBGAMMA_CONNECTOR_TYPE_eDP, - - /** - * A virtual connector - */ - LIBGAMMA_CONNECTOR_TYPE_VIRTUAL, - - /** - * Display Serial Interface (DSI) - */ - LIBGAMMA_CONNECTOR_TYPE_DSI, - - /** - * LFP connector - * - * (What is this?) - */ - LIBGAMMA_CONNECTOR_TYPE_LFP - -} libgamma_connector_type_t; - -/** - * The number of values defined in `libgamma_connector_type_t` - */ -#define LIBGAMMA_CONNECTOR_TYPE_COUNT 20 - -/** - * Orders for subpixels - * - * Currently the possible values are very biased - * to LCD, Plasma and monochrome monitors - */ -typedef enum libgamma_subpixel_order { - /** - * The adjustment method does not know the order of the subpixels - * - * (This could be considered an error) - */ - LIBGAMMA_SUBPIXEL_ORDER_UNKNOWN = 0, - - /** - * There are no subpixels in the monitor - */ - LIBGAMMA_SUBPIXEL_ORDER_NONE, - - /** - * The subpixels are ordered red, green and then blue, from left to right - */ - LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_RGB, - - /** - * The subpixels are ordered blue, green and then red, from left to right - */ - LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_BGR, - - /** - * The subpixels are ordered red, green and then blue, from the top down - */ - LIBGAMMA_SUBPIXEL_ORDER_VERTICAL_RGB, - - /** - * The subpixels are ordered blue, green and then red, from the top down - */ - LIBGAMMA_SUBPIXEL_ORDER_VERTICAL_BGR - -} libgamma_subpixel_order_t; - -/** - * The number of values defined in `libgamma_subpixel_order_t` - */ -#define LIBGAMMA_SUBPIXEL_ORDER_COUNT 6 - - -/** - * Answer enum to a decision problem - */ -typedef enum libgamma_decision { - /** - * The answer is negative - */ - LIBGAMMA_NO = 0, - - /** - * The answer is unknown - */ - LIBGAMMA_MAYBE = 1, - - /** - * The answer is positive - */ - LIBGAMMA_YES = 2 - -} libgamma_decision_t; - - -/** - * For a `libgamma_crtc_information_t` fill in the values for - * `edid` and `edid_length` and report errors to `edid_error` - */ -#define LIBGAMMA_CRTC_INFO_EDID (1 << 0) - -/** - * For a `libgamma_crtc_information_t` fill in the value - * for `width_mm` and report errors to `width_mm_error` - */ -#define LIBGAMMA_CRTC_INFO_WIDTH_MM (1 << 1) - -/** - * For a `libgamma_crtc_information_t` fill in the value - * for `height_mm` and report errors to `height_mm_error` - */ -#define LIBGAMMA_CRTC_INFO_HEIGHT_MM (1 << 2) - -/** - * For a `libgamma_crtc_information_t` fill in the value for - * `width_mm_edid` and report errors to `width_mm_edid_error` - */ -#define LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID (1 << 3) - -/** - * For a `libgamma_crtc_information_t` fill in the value for - * `height_mm_edid` and report errors to `height_mm_edid_error` - */ -#define LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID (1 << 4) - -/** - * For a `libgamma_crtc_information_t` fill in the values for - * `red_gamma_size`, `green_gamma_size`, and `blue_gamma_size`, - * and report errors to `gamma_size_error` - */ -#define LIBGAMMA_CRTC_INFO_GAMMA_SIZE (1 << 5) - -/** - * For a `libgamma_crtc_information_t` fill in the value for - * `gamma_depth` and report errors to `gamma_depth_error` - */ -#define LIBGAMMA_CRTC_INFO_GAMMA_DEPTH (1 << 6) - -/** - * For a `libgamma_crtc_information_t` fill in the value for - * `gamma_support` and report errors to `gamma_support_error` - */ -#define LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT (1 << 7) - -/** - * For a `libgamma_crtc_information_t` fill in the value for - * `subpixel_order` and report errors to `subpixel_order_error` - */ -#define LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER (1 << 8) - -/** - * For a `libgamma_crtc_information_t` fill in the - * value for `active` and report errors to `active_error` - */ -#define LIBGAMMA_CRTC_INFO_ACTIVE (1 << 9) - -/** - * For a `libgamma_crtc_information_t` fill in the value for - * `connector_name` and report errors to `connector_name_error` - */ -#define LIBGAMMA_CRTC_INFO_CONNECTOR_NAME (1 << 10) - -/** - * For a `libgamma_crtc_information_t` fill in the value for - * `connector_type` and report errors to `connector_type_error` - */ -#define LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE (1 << 11) - -/** - * For a `libgamma_crtc_information_t` fill in the - * values for `gamma_red`, `gamma_green`, and `gamma_blue` - * and report errors to `gamma_error` - */ -#define LIBGAMMA_CRTC_INFO_GAMMA (1 << 12) - -/** - * The number of `LIBGAMMA_CRTC_INFO_*` values defined - */ -#define LIBGAMMA_CRTC_INFO_COUNT 13 - -/** - * Macro for both `libgamma_crtc_information_t` fields - * that can specify the size of the monitor's viewport - * as specified in the monitor's Extended Display - * Information Data - */ -#define LIBGAMMA_CRTC_INFO_MACRO_EDID_VIEWPORT (LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID | LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID) - -/** - * Macro for all `libgamma_crtc_information_t` fields - * that can be filled if the adjustment method have - * support for reading the monitors' Extended Display - * Information Data - */ -#define LIBGAMMA_CRTC_INFO_MACRO_EDID (LIBGAMMA_CRTC_INFO_EDID | LIBGAMMA_CRTC_INFO_MACRO_EDID_VIEWPORT | LIBGAMMA_CRTC_INFO_GAMMA) - -/** - * Macro for both `libgamma_crtc_information_t` fields - * that can specify the size of the monitor's viewport - * as provided by the adjustment method without this - * library having to parse the monitor's Extended Display - * Information Data - */ -#define LIBGAMMA_CRTC_INFO_MACRO_VIEWPORT (LIBGAMMA_CRTC_INFO_WIDTH_MM | LIBGAMMA_CRTC_INFO_HEIGHT_MM) - -/** - * Macro for the `libgamma_crtc_information_t` fields - * that specifies the CRTC's gamma ramp sizes and gamma - * ramp depth - */ -#define LIBGAMMA_CRTC_INFO_MACRO_RAMP (LIBGAMMA_CRTC_INFO_GAMMA_SIZE | LIBGAMMA_CRTC_INFO_GAMMA_DEPTH) - -/** - * Macro for the `libgamma_crtc_information_t` fields - * that specifies the CRTC's connector type and the - * partition unique name of the connector - */ -#define LIBGAMMA_CRTC_INFO_MACRO_CONNECTOR (LIBGAMMA_CRTC_INFO_CONNECTOR_NAME | LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE) - -/** - * Macro for the `libgamma_crtc_information_t` fields - * that required there is a monitor attached to the - * connector, and that status itself - */ -#define LIBGAMMA_CRTC_INFO_MACRO_ACTIVE (LIBGAMMA_CRTC_INFO_MACRO_EDID | LIBGAMMA_CRTC_INFO_MACRO_VIEWPORT |\ - LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER | LIBGAMMA_CRTC_INFO_ACTIVE) - - - -/** - * Cathode ray tube controller information data structure - */ -typedef struct libgamma_crtc_information { - /** - * The Extended Display Identification Data associated with - * the attached monitor. - * - * This is raw byte array that is usually 128 bytes long. - * It is not NUL-terminate, rather its length is stored in - * `edid_length`. - */ - unsigned char *edid; - - /** - * The length of `edid` - */ - size_t edid_length; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int edid_error; - - - /** - * The phyical width, in millimetres, of the viewport of the - * attached monitor, as reported by the adjustment method - * - * This value may be incorrect, which is a known issue with - * the X server where it is the result of the X server - * attempting the estimate the size on its own - * - * Zero means that its is not applicable, which is the case - * for projectors - */ - size_t width_mm; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int width_mm_error; - - - /** - * The phyical height, in millimetres, of the viewport of the - * attached monitor, as reported by the adjustment method - * - * This value may be incorrect, which is a known issue with - * the X server where it is the result of the X server - * attempting the estimate the size on its own - * - * Zero means that its is not applicable, which is the case - * for projectors - */ - size_t height_mm; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int height_mm_error; - - - /** - * The phyical width, in millimetres, of the viewport of the - * attached monitor, as reported by it the monitor's Extended - * Display Information Data - * - * This value can only contain whole centimetres, which means - * that the result is always zero modulus ten. However, this - * could change with revisions of the EDID structure. - * - * Zero means that its is not applicable, which is the case - * for projectors. - */ - size_t width_mm_edid; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int width_mm_edid_error; - - - /** - * The phyical height, in millimetres, of the viewport of the - * attached monitor, as reported by it the monitor's Extended - * Display Information Data - * - * This value can only contain whole centimetres, which means - * that the result is always zero modulus ten. However, this - * could change with revisions of the EDID structure. - * - * Zero means that its is not applicable, which is the case - * for projectors - */ - size_t height_mm_edid; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int height_mm_edid_error; - - - /** - * The size of the encoding axis of the red gamma ramp - */ - size_t red_gamma_size; - - /** - * The size of the encoding axis of the green gamma ramp - */ - size_t green_gamma_size; - - /** - * The size of the encoding axis of the blue gamma ramp - */ - size_t blue_gamma_size; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int gamma_size_error; - - - /** - * The bit-depth of the value axes of gamma ramps, - * -1 for single precision floating point, and -2 for - * double precision floating point - */ - signed gamma_depth; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int gamma_depth_error; - - - /** - * `LIBGAMMA_NO` indicates that the CRTC does not support - * gamma ramp adjustments. `LIBGAMMA_MAYBE` indicates that - * the CRTC may or may not support gamma ramp adjustments, - * meaning that the display server really does not know, but - * the protocol is available. `LIBGAMMA_NO` indicates that - * the CRTC does support gamma ramp adjustments. - */ - libgamma_decision_t gamma_support; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int gamma_support_error; - - - /** - * The layout of the subpixels - * - * You cannot count on this value — especially for CRT:s — - * but it is provided anyway as a means of distinguishing - * monitors - */ - libgamma_subpixel_order_t subpixel_order; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int subpixel_order_error; - - - /** - * Whether there is a monitor connected to the CRTC - */ - int active; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int active_error; - - - /** - * The name of the connector as designated by the display - * server or as give by this library in case the display - * server lacks this feature. - */ - char *connector_name; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library. - */ - int connector_name_error; - - - /** - * The type of the connector that is associated with the CRTC. - */ - libgamma_connector_type_t connector_type; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library. - */ - int connector_type_error; - - - /** - * The gamma characteristics of the monitor as reported - * in its Extended Display Information Data. The value - * holds the value for the red channel. - * - * If you do not have and more accurate measurement of the - * gamma for the monitor this could be used to give a rought - * gamma correction; simply divide the value with 2.2 and use - * the result for the red channel in the gamma correction. - */ - float gamma_red; - - /** - * The gamma characteristics of the monitor as reported - * in its Extended Display Information Data. The value - * holds the value for the green channel. - * - * If you do not have and more accurate measurement of the - * gamma for the monitor this could be used to give a rought - * gamma correction; simply divide the value with 2.2 and use - * the result for the green channel in the gamma correction. - */ - float gamma_green; - - /** - * The gamma characteristics of the monitor as reported - * in its Extended Display Information Data. The value - * holds the value for the blue channel. - * - * If you do not have and more accurate measurement of the - * gamma for the monitor this could be used to give a rought - * gamma correction; simply divide the value with 2.2 and use - * the result for the blue channel in the gamma correction. - */ - float gamma_blue; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int gamma_error; - -} libgamma_crtc_information_t; - - - -/** - * Gamma ramp structure for 8-bit gamma ramps - */ -typedef struct libgamma_gamma_ramps8 { - /** - * The size of `red` - */ - size_t red_size; - - /** - * The size of `green` - */ - size_t green_size; - - /** - * The size of `blue` - */ - size_t blue_size; - - /** - * The gamma ramp for the red channel - */ - uint8_t *red; - - /** - * The gamma ramp for the green channel - */ - uint8_t *green; - - /** - * The gamma ramp for the blue channel - */ - uint8_t *blue; - -} libgamma_gamma_ramps8_t; - - -/** - * Gamma ramp structure for 16-bit gamma ramps - */ -typedef struct libgamma_gamma_ramps16 -{ - /** - * The size of `red` - */ - size_t red_size; - - /** - * The size of `green` - */ - size_t green_size; - - /** - * The size of `blue` - */ - size_t blue_size; - - /** - * The gamma ramp for the red channel - */ - uint16_t *red; - - /** - * The gamma ramp for the green channel - */ - uint16_t *green; - - /** - * The gamma ramp for the blue channel - */ - uint16_t *blue; - -} libgamma_gamma_ramps16_t; - - -/** - * Gamma ramp structure for 32-bit gamma ramps - */ -typedef struct libgamma_gamma_ramps32 -{ - /** - * The size of `red` - */ - size_t red_size; - - /** - * The size of `green` - */ - size_t green_size; - - /** - * The size of `blue` - */ - size_t blue_size; - - /** - * The gamma ramp for the red channel - */ - uint32_t *red; - - /** - * The gamma ramp for the green channel - */ - uint32_t *green; - - /** - * The gamma ramp for the blue channel - */ - uint32_t *blue; - -} libgamma_gamma_ramps32_t; - - -/** - * Gamma ramp structure for 64-bit gamma ramps - */ -typedef struct libgamma_gamma_ramps64 -{ - /** - * The size of `red` - */ - size_t red_size; - - /** - * The size of `green` - */ - size_t green_size; - - /** - * The size of `blue` - */ - size_t blue_size; - - /** - * The gamma ramp for the red channel - */ - uint64_t *red; - - /** - * The gamma ramp for the green channel - */ - uint64_t *green; - - /** - * The gamma ramp for the blue channel - */ - uint64_t *blue; - -} libgamma_gamma_ramps64_t; - - -/** - * Gamma ramp structure for `float` gamma ramps - */ -typedef struct libgamma_gamma_rampsf -{ - /** - * The size of `red` - */ - size_t red_size; - - /** - * The size of `green` - */ - size_t green_size; - - /** - * The size of `blue` - */ - size_t blue_size; - - /** - * The gamma ramp for the red channel - */ - float *red; - - /** - * The gamma ramp for the green channel - */ - float *green; - - /** - * The gamma ramp for the blue channel - */ - float *blue; - -} libgamma_gamma_rampsf_t; - - -/** - * Gamma ramp structure for `double` gamma ramps - */ -typedef struct libgamma_gamma_rampsd -{ - /** - * The size of `red` - */ - size_t red_size; - - /** - * The size of `green` - */ - size_t green_size; - - /** - * The size of `blue` - */ - size_t blue_size; - - /** - * The gamma ramp for the red channel - */ - double *red; - - /** - * The gamma ramp for the green channel - */ - double *green; - - /** - * The gamma ramp for the blue channel - */ - double *blue; - -} libgamma_gamma_rampsd_t; - - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int libgamma_gamma_ramps8_initialise(libgamma_gamma_ramps8_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps8_initialise` or otherwise - * initialised in the proper manner - * - * @param this The gamma ramps - */ -void libgamma_gamma_ramps8_destroy(libgamma_gamma_ramps8_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps8_initialise` or otherwise - * initialised in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void libgamma_gamma_ramps8_free(libgamma_gamma_ramps8_t *restrict); - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int libgamma_gamma_ramps16_initialise(libgamma_gamma_ramps16_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps16_initialise` or otherwise - * initialised in the proper manner - * - * @param this The gamma ramps - */ -void libgamma_gamma_ramps16_destroy(libgamma_gamma_ramps16_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps16_initialise` or otherwise - * initialised in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void libgamma_gamma_ramps16_free(libgamma_gamma_ramps16_t *restrict); - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated. - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int libgamma_gamma_ramps32_initialise(libgamma_gamma_ramps32_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps32_initialise` or otherwise - * initialised in the proper manner - * - * @param this The gamma ramps - */ -void libgamma_gamma_ramps32_destroy(libgamma_gamma_ramps32_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps32_initialise` or otherwise - * initialised in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void libgamma_gamma_ramps32_free(libgamma_gamma_ramps32_t *restrict); - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation. - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated. - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int libgamma_gamma_ramps64_initialise(libgamma_gamma_ramps64_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps64_initialise` or otherwise - * initialised in the proper manner - * - * @param this The gamma ramps - */ -void libgamma_gamma_ramps64_destroy(libgamma_gamma_ramps64_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps64_initialise` or otherwise - * initialised in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void libgamma_gamma_ramps64_free(libgamma_gamma_ramps64_t *restrict); - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int libgamma_gamma_rampsf_initialise(libgamma_gamma_rampsf_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_rampsf_initialise` or otherwise - * initialised in the proper manner - * - * @param this The gamma ramps - */ -void libgamma_gamma_rampsf_destroy(libgamma_gamma_rampsf_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_rampsf_initialise` or otherwise - * initialised in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void libgamma_gamma_rampsf_free(libgamma_gamma_rampsf_t *restrict); - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int libgamma_gamma_rampsd_initialise(libgamma_gamma_rampsd_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_rampsd_initialise` or otherwise - * initialised in the proper manner - * - * @param this The gamma ramps - */ -void libgamma_gamma_rampsd_destroy(libgamma_gamma_rampsd_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_rampsd_initialise` or otherwise - * initialised in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void libgamma_gamma_rampsd_free(libgamma_gamma_rampsd_t *restrict); - - - -#ifndef __GNUC__ -# undef __attribute__ -#endif - -#endif diff --git a/src/lib/libgamma.h b/src/lib/libgamma.h deleted file mode 100644 index cc63fb8..0000000 --- a/src/lib/libgamma.h +++ /dev/null @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_H -#define LIBGAMMA_H - -#include "libgamma-config.h" /* Must be first. */ - -#include "libgamma-method.h" -#include "libgamma-facade.h" -#include "libgamma-error.h" - -#endif |