diff options
author | Mattias Andrée <maandree@kth.se> | 2021-03-02 18:11:58 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2021-03-02 18:11:58 +0100 |
commit | d615f10762649507aebee9419147246bb1dc2a93 (patch) | |
tree | 4b3196b0bb70b6be4dd720c4be533517634b7f7e /src | |
parent | X RAndR: Fix connection failure detection (diff) | |
download | libgamma-d615f10762649507aebee9419147246bb1dc2a93.tar.gz libgamma-d615f10762649507aebee9419147246bb1dc2a93.tar.bz2 libgamma-d615f10762649507aebee9419147246bb1dc2a93.tar.xz |
Change license + change style + misc
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
43 files changed, 7141 insertions, 7852 deletions
diff --git a/src/extract/cut b/src/extract/cut index a47e1d2..ad7c81a 100755 --- a/src/extract/cut +++ b/src/extract/cut @@ -1,21 +1,6 @@ #!/usr/bin/env python # -*- mode: python, encoding: utf-8 -*- - -# libgamma -- Display server abstraction layer for gamma ramp adjustments -# Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) -# -# This library is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this library. If not, see <http://www.gnu.org/licenses/>. +# See LICENSE file for copyright and license details. # Very limited GNU cut. diff --git a/src/extract/libgamma-error-extract b/src/extract/libgamma-error-extract index 86c8bb6..cbfb93a 100755 --- a/src/extract/libgamma-error-extract +++ b/src/extract/libgamma-error-extract @@ -1,21 +1,6 @@ #!/usr/bin/env python # -*- mode: python, encoding: utf-8 -*- - -# libgamma -- Display server abstraction layer for gamma ramp adjustments -# Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) -# -# This library is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this library. If not, see <http://www.gnu.org/licenses/>. +# See LICENSE file for copyright and license details. import sys import os diff --git a/src/extract/libgamma-method-extract b/src/extract/libgamma-method-extract index 1448b96..b967ab3 100755 --- a/src/extract/libgamma-method-extract +++ b/src/extract/libgamma-method-extract @@ -1,21 +1,6 @@ #!/usr/bin/env python # -*- mode: python, encoding: utf-8 -*- - -# libgamma -- Display server abstraction layer for gamma ramp adjustments -# Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) -# -# This library is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this library. If not, see <http://www.gnu.org/licenses/>. +# See LICENSE file for copyright and license details. import sys import os diff --git a/src/lib/edid.c b/src/lib/edid.c index 2a9f3f9..fc7b498 100644 --- a/src/lib/edid.c +++ b/src/lib/edid.c @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #include "edid.h" #include "libgamma-method.h" @@ -33,88 +17,89 @@ /** - * Parse the EDID of a monitor. + * 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 feilds 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. + * @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) +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; - int 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) == 0) - 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 == 0)) - { - 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_red = this->gamma_green = this->gamma_blue = (float)((int)(this->edid[23]) + 100) / 100.f; - } - - /* 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 == 0) - 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; - +#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 index f065a98..51529ae 100644 --- a/src/lib/edid.h +++ b/src/lib/edid.h @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #ifndef LIBGAMMA_EDID_H #define LIBGAMMA_EDID_H @@ -24,16 +8,15 @@ /** - * Parse the EDID of a monitor. + * 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 feilds 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. + * @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 out, int32_t fields); +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 index e7cbbc9..75f1fb2 100644 --- a/src/lib/fake-quartz-cg.c +++ b/src/lib/fake-quartz-cg.c @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* 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 @@ -34,129 +18,132 @@ #ifndef HAVE_LIBGAMMA_METHOD_X_RANDR -/* Use dummy translation. */ +/* Use dummy translation */ /** - * Get a list of all online displays on the system. + * 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`. + * @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. + * `*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) +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; + /* 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. + * 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. + * @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) +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; + (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. + * 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. + * @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) +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; + 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. + * Restore each display's gamma ramps to the settings in ColorSync */ -void CGDisplayRestoreColorSyncSettings(void) +void +CGDisplayRestoreColorSyncSettings(void) { - /* Do nothing. */ + /* Do nothing */ } /** - * Get the number of stops in the gamma ramps for a display. + * 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. + * @param display The ID of the display + * @return The number of stops in the gamma ramps */ -uint32_t CGDisplayGammaTableCapacity(CGDirectDisplayID display) +uint32_t +CGDisplayGammaTableCapacity(CGDirectDisplayID display) { - /* We pretend that our gamma ramps are of size 256. */ - (void) display; - return 256; + /* We pretend that our gamma ramps are of size 256 */ + (void) display; + return 256; } /** - * Release resources used by the backend. + * Release resources used by the backend */ -void close_fake_quartz(void) +void +close_fake_quartz(void) { - /* Do nothing. */ + /* Do nothing */ } #else -/* Use translation to X RandR. */ +/* Use translation to X RandR */ #include <xcb/xcb.h> @@ -165,306 +152,296 @@ void close_fake_quartz(void) /** - * Connection to the X RandR display. + * Connection to the X RandR display */ -static xcb_connection_t* restrict connection = NULL; +static xcb_connection_t *restrict connection = NULL; /** - * Resouces for the screen. - * We only have one screen, again this is a very sloppy compatibility layer. + * 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; +static xcb_randr_get_screen_resources_current_reply_t *restrict res_reply = NULL; /** - * The number of available CRTC:s. + * The number of available CRTC:s */ static uint32_t crtc_count = 0; /** - * List of X RandR CRTC:s. + * List of X RandR CRTC:s */ -static xcb_randr_crtc_t* restrict crtcs = NULL; +static xcb_randr_crtc_t *restrict crtcs = NULL; /** - * The original gamma ramps, used to emulate gamma ramp restoration to system settings. + * The original gamma ramps, used to emulate gamma ramp restoration to system settings */ -static uint16_t* restrict original_ramps = NULL; +static uint16_t *restrict original_ramps = NULL; -/* xcb violates the rule to never return struct:s. */ -#ifdef __GCC__ +/* 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. + * 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`. + * @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. + * `*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) +CGError +CGGetOnlineDisplayList(uint32_t max_size, CGDirectDisplayID *restrict displays_out, uint32_t *restrict count_out) { - uint32_t i; - - /* Connect to the display and get screen data if not already done so. */ - if (connection == NULL) - { - 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; - - /* 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 == NULL) - { - 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. */ + 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)); + 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); + + /* 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; + + /* 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. + * 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. + * @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) +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]; - uint16_t g_int[256]; - uint16_t b_int[256]; - long i; - int32_t v; - - /* 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) == NULL ? kCGErrorSuccess : ~kCGErrorSuccess; + 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. + * 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. + * @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) +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; + 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. + * Restore each display's gamma ramps to the settings in ColorSync */ -void CGDisplayRestoreColorSyncSettings(void) +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); - } + 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. + * 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. + * @param display The ID of the display + * @return The number of stops in the gamma ramps */ -uint32_t CGDisplayGammaTableCapacity(CGDirectDisplayID display) +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; + /* We assume that our gamma ramps are of the size + * 256 (this is a sloppy compatibility layer) */ + (void) display; + return 256; } -#ifdef __GCC__ +#ifdef __GNUC__ # pragma GCC diagnostic pop #endif /** - * Release resources used by the backend. + * Release resources used by the backend */ void close_fake_quartz_cg(void) { - free(res_reply), res_reply = NULL; - if (connection != NULL) - xcb_disconnect(connection), connection = NULL; - free(original_ramps), original_ramps = NULL; + 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 index a77ccc6..361a73c 100644 --- a/src/lib/fake-quartz-cg.h +++ b/src/lib/fake-quartz-cg.h @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #ifndef LIBGAMMA_FAKE_QUARTZ_CG_H #define LIBGAMMA_FAKE_QUARTZ_CG_H @@ -23,7 +7,7 @@ #endif -#ifndef __GCC__ +#ifndef __GNUC__ # define __attribute__(x) #endif @@ -44,79 +28,76 @@ /** - * Numerical `typedef` for errors that occur in CoreGraphics calls. + * Numerical `typedef` for errors that occur in CoreGraphics calls */ typedef int32_t CGError; /** - * The call was successful. + * The call was successful */ #define kCGErrorSuccess 0 /** - * The data type that is used for the values in the gamma ramps. + * The data type that is used for the values in the gamma ramps */ typedef float CGGammaValue; /** - * The data type for display ID:s. + * The data type for display ID:s */ typedef uint32_t CGDirectDisplayID; /** - * Get a list of all online displays on the system. + * 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`. + * @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. + * `*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); +CGError CGGetOnlineDisplayList(uint32_t, CGDirectDisplayID *restrict, uint32_t *restrict); /** - * Set the gamma ramps for a display. + * 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. + * @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); +CGError CGSetDisplayTransferByTable(CGDirectDisplayID, uint32_t, const CGGammaValue *, const CGGammaValue *, const CGGammaValue *); /** - * Get the current gamma ramps for a display. + * 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. + * @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); +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. + * 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. + * 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. + * @param display The ID of the display + * @return The number of stops in the gamma ramps */ -uint32_t CGDisplayGammaTableCapacity(CGDirectDisplayID display) __attribute__((const)); +uint32_t CGDisplayGammaTableCapacity(CGDirectDisplayID) __attribute__((const)); /* The follow part most only be used when this module is used, @@ -126,15 +107,14 @@ uint32_t CGDisplayGammaTableCapacity(CGDirectDisplayID display) __attribute__((c * and free resources needed by this module. */ /** - * Release resources used by the backend. + * Release resources used by the backend */ void close_fake_quartz_cg(void); -#ifndef __GCC__ +#ifndef __GNUC__ # undef __attribute__ #endif #endif - diff --git a/src/lib/fake-w32-gdi.c b/src/lib/fake-w32-gdi.c index 1489073..68847ca 100644 --- a/src/lib/fake-w32-gdi.c +++ b/src/lib/fake-w32-gdi.c @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* 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 @@ -34,93 +18,98 @@ #ifndef HAVE_LIBGAMMA_METHOD_X_RANDR -/* Use dummy translation. */ +/* Use dummy translation */ /** - * Get the device context for a window or the entire screen. + * Get the device context for a window or the entire screen * - * @param hWnd The windows, `NULL` for the entire screen. - * @return The device context. + * @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) +HDC +GetDC(HWND hWnd) { - /* Just a non-NULL value. */ - (void) hWnd; - return (HDC*)16; + /* Just a non-NULL value */ + (void) hWnd; + return (HDC *)16; } /** - * Free a device context. + * 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. + * @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) +int +ReleaseDC(HWND hWnd, HDC hDC) { - /* Always successful. */ - (void) hWnd; - (void) hDC; - return 1; + /* Always successful */ + (void) hWnd; + (void) hDC; + return 1; } /** - * Get information (capabilities) for a device context. + * Get information (capabilities) for a device context * - * @param hDC The device context. - * @param nIndex The information to retrieve, may be `COLORMGMTCAPS`. + * @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`. + * 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) +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; + /* 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. + * 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. + * @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) +BOOL +SetDeviceGammaRamp(HDC hDC, LPVOID restrict lpRamp) { - /* Always successful. */ - (void) hDC; - (void) lpRamp; - return TRUE; + /* Always successful */ + (void) hDC; + (void) lpRamp; + return TRUE; } /** - * Get the current gamma ramps for a device. + * 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. + * @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) +BOOL +GetDeviceGammaRamp(HDC hDC, LPVOID restrict lpRamp) { - /* Always successful. */ - (void) hDC; - (void) lpRamp; - return TRUE; + /* Always successful */ + (void) hDC; + (void) lpRamp; + return TRUE; } @@ -128,68 +117,66 @@ BOOL GetDeviceGammaRamp(HDC hDC, LPVOID restrict lpRamp) /** * Get the context for a device * - * @param lpszDriver The driver or a display device, use "DISPLAY" if you want a display. + * @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 - - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183490(v=vs.85).aspx + * `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) +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") ? NULL : (HDC*)16; + /* `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. + * 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. + * @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) +BOOL +EnumDisplayDevices(LPCTSTR lpDevice, restrict DWORD iDevNum, PDISPLAY_DEVICE restrict lpDisplayDevice, DWORD dwFlags) { - (void) dwFlags; - /* Check correctness of `lpDevice`. */ - if (lpDevice != NULL) - { - 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; + (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. */ +/* Use translation to X RandR */ #include <xcb/xcb.h> @@ -198,162 +185,168 @@ BOOL EnumDisplayDevices(LPCTSTR lpDevice, restrict DWORD iDevNum, /** * The gamma ramp size that devices will - * always have in Windows GDI. + * always have in Windows GDI */ -#define GAMMA_RAMP_SIZE 256 +#define GAMMA_RAMP_SIZE 256 /** - * Connection to the X RandR display. + * Connection to the X RandR display */ -static xcb_connection_t* restrict connection = NULL; +static xcb_connection_t *restrict connection = NULL; /** - * Resouces for the screen. - * We only have one screen, again this is a very sloppy compatibility layer. + * 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; +static xcb_randr_get_screen_resources_current_reply_t *restrict res_reply = NULL; /** - * The number of available CRTC:s, -1 if not known yet. + * The number of available CRTC:s, -1 if not known yet */ static ssize_t crtc_count = -1; /** - * List of X RandR CRTC:s. + * List of X RandR CRTC:s */ -static xcb_randr_crtc_t* restrict crtcs = NULL; +static xcb_randr_crtc_t *restrict crtcs = NULL; /** - * The number of opened CRTC:s. + * The number of opened CRTC:s */ static size_t dc_count = 0; /** - * Get the device context for a window or the entire screen. + * Get the device context for a window or the entire screen * - * @param hWnd The windows, `NULL` for the entire screen. - * @return The device context. + * @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) +HDC +GetDC(HWND hWnd) { - /* Return the primary CRTC. */ - (void) hWnd; - return CreateDC(TEXT("DISPLAY"), "0", NULL, NULL); + /* Return the primary CRTC */ + (void) hWnd; + return CreateDC(TEXT("DISPLAY"), "0", NULL, NULL); } /** - * Free a device context. + * 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. + * @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) +int +ReleaseDC(HWND hWnd, HDC hDC) { - /* Disconnect from the RandR display when all monitors have been closed. */ - (void) hWnd; - (void) hDC; - dc_count--; - if (dc_count == 0) - { - if (connection != NULL) - xcb_disconnect(connection); - connection = NULL; - free(res_reply); - res_reply = NULL; - } - return 1; + /* 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. + * Get information (capabilities) for a device context * - * @param hDC The device context. - * @param nIndex The information to retrieve, may be `COLORMGMTCAPS`. + * @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`. + * 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) +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; + /* 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 __GCC__ +/* 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. + * 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. + * @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) +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 == NULL ? TRUE : FALSE; + /* 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. + * 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. + * @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) +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) + 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)); - + + /* 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; + + free(gamma_reply); + return TRUE; } @@ -361,134 +354,126 @@ BOOL GetDeviceGammaRamp(HDC hDC, LPVOID restrict lpRamp) /** * Get the context for a device * - * @param lpszDriver The driver or a display device, use "DISPLAY" if you want a display. + * @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 - - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183490(v=vs.85).aspx + * `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) +HDC +CreateDC(LPCTSTR restrict lpszDriver, LPCTSTR restrict lpszDevice, LPCTSTR restrict lpszOutput, const void *restrict lpInitData) { - int crtc_index = atoi(lpszDevice); - - (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 == 0) - { - xcb_generic_error_t* error; - xcb_screen_iterator_t iter; - xcb_randr_get_screen_resources_current_cookie_t res_cookie; - - /* 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; + 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); } - - /* 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 == 0) - { - xcb_disconnect(connection); - free(res_reply); - res_reply = NULL; - crtc_count = -1; + + /* 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; } - return NULL; - } - - /* We have opened a new CRTC. */ - dc_count++; - /* Return the ID of the CRTC. */ - return crtcs + crtc_index; + + /* We have opened a new CRTC */ + dc_count++; + /* Return the ID of the CRTC */ + return &crtcs[crtc_index]; } -#ifdef __GCC__ +#ifdef __GNUC__ # pragma GCC diagnostic pop #endif /** - * Get a display device by its name or index. + * 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. + * @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) +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 != NULL) - { - 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) == 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; + 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 index 9462afe..bcc2fa9 100644 --- a/src/lib/fake-w32-gdi.h +++ b/src/lib/fake-w32-gdi.h @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #ifndef LIBGAMMA_FAKE_W32_GDI_H #define LIBGAMMA_FAKE_W32_GDI_H @@ -23,7 +7,7 @@ #endif -#ifndef __GCC__ +#ifndef __GNUC__ # define __attribute__(x) #endif @@ -31,64 +15,64 @@ /** - * One of the Windows `typedef`:s for unsigned 16-bit integer. - * This is the one used in these functions. + * 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. + * 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 ; -/* XXX Documented to be 32-bit but defined as `typedef unsigned long DWORD`, which is it? */ +typedef uint32_t DWORD; /** - * One of the Windows `typedef` for booleanic values, the `int` variant. + * 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'.) + * 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; +typedef void *HDC; /** - * Windows `typedef` for a window (the 'h' stands for 'handle'.) + * 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; +typedef void *HWND; /** - * One of the Windows `typedef`:s for `void*`, a generic pointer. + * 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; +typedef void *LPVOID; /** - * A silly Windows `typedef` for a `const char*`, a constant string. + * 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*`. + * 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; +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. + * `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 */ @@ -100,7 +84,7 @@ typedef char TCHAR; * * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#BOOL */ -#define TRUE 1 +#define TRUE 1 /** * Apperently we need `FALSE` to be defined to understand code, @@ -113,35 +97,35 @@ typedef char TCHAR; /** - * Get the device context for a window or the entire screen. + * Get the device context for a window or the entire screen * - * @param hWnd The window, `NULL` for the entire screen. - * @return The device context. + * @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 hWnd); +HDC GetDC(HWND); /** - * Free a device context. + * 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. + * @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); +int ReleaseDC(HWND, HDC); /** - * Get information (capabilities) for a device context. + * Get information (capabilities) for a device context * - * @param hDC The device context. - * @param nIndex The information to retrieve, may be `COLORMGMTCAPS`. + * @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`. + * 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) __attribute__((const)); +int GetDeviceCaps(HDC, int) __attribute__((const)); /** * Colour management capabilities @@ -159,45 +143,45 @@ int GetDeviceCaps(HDC hDC, int nIndex) __attribute__((const)); /** - * Set the gamma ramps for a device. + * 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. + * @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); +BOOL SetDeviceGammaRamp(HDC, LPVOID restrict); /** - * Get the current gamma ramps for a device. + * 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. + * @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); +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 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. + * @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); +HDC CreateDC(LPCTSTR restrict, LPCTSTR restrict, LPCTSTR restrict, const void *restrict); /** - * This macro does not seem to have an official documentation that is correct. + * 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 */ @@ -205,63 +189,60 @@ HDC CreateDC(LPCTSTR restrict lpszDriver, LPCTSTR restrict lpszDevice, /** - * Information about a display device. + * 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; - +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'. + * 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 + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183569(v=vs.85).aspx */ -typedef DISPLAY_DEVICE* PDISPLAY_DEVICE; +typedef DISPLAY_DEVICE *PDISPLAY_DEVICE; /** - * The monitor is "on". + * The monitor is "on" * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183569(v=vs.85).aspx + * @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. + * 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. + * @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); +BOOL EnumDisplayDevices(LPCTSTR restrict, DWORD, PDISPLAY_DEVICE restrict, DWORD) -#ifndef __GCC__ +#ifndef __GNUC__ # undef __attribute__ #endif #endif - diff --git a/src/lib/gamma-dummy.c.gpp b/src/lib/gamma-dummy.c.gpp index ef0ae44..a9aa724 100644 --- a/src/lib/gamma-dummy.c.gpp +++ b/src/lib/gamma-dummy.c.gpp @@ -1,21 +1,5 @@ /* -*- c -*- */ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #ifndef HAVE_LIBGAMMA_METHOD_DUMMY # error Compiling gamma-dummy.c without HAVE_LIBGAMMA_METHOD_DUMMY #endif @@ -36,277 +20,271 @@ /** - * 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; - + * 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; - + * 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. + * 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; - +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. + * 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; - +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. + * 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. + * error identifier provided by this library */ -static int libgamma_dummy_crtc_restore_forced(libgamma_dummy_crtc_t* restrict data); +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. +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. + * @param this The data structure to fill with the method's capabilities */ -void libgamma_dummy_method_capabilities(libgamma_method_capabilities_t* restrict this) +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; + 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. + * Initialise an allocated site state * - * @param this The site state to initialise. + * @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 @@ -314,712 +292,702 @@ void libgamma_dummy_method_capabilities(libgamma_method_capabilities_t* restrict * 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. + * error identifier provided by this library */ -int libgamma_dummy_site_initialise(libgamma_site_state_t* restrict this, - char* restrict site) +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 == 0) - sites = sites == 0 ? 0 : 1; - this->data = NULL; - - if ((site != NULL) && (*site) && ((atoll(site) < 0) || (sites <= (unsigned long long)atoll(site)))) - return LIBGAMMA_NO_SUCH_SITE; - - data = malloc(sizeof(libgamma_dummy_site_t)); - if (data == NULL) - goto fail; - - this->data = data; - data->state = this; - - data->partition_count = libgamma_dummy_configurations.default_partition_count; - if (libgamma_dummy_configurations.capabilities.multiple_partitions == 0) - data->partition_count = data->partition_count == 0 ? 0 : 1; - - crtcs = libgamma_dummy_configurations.default_crtc_count; - if (libgamma_dummy_configurations.capabilities.multiple_crtcs == 0) - crtcs = crtcs == 0 ? 0 : 1; - - data->partitions = malloc(data->partition_count * sizeof(libgamma_dummy_partition_t)); - if (data->partitions == NULL) - 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; + 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. + * Release all resources held by a site state * - * @param this The site state. + * @param this The site state */ -void libgamma_dummy_site_destroy(libgamma_site_state_t* restrict this) +void +libgamma_dummy_site_destroy(libgamma_site_state_t *restrict this) { - libgamma_dummy_site_t* data = this->data; - if (data == NULL) - return; - - free(data->partitions); - free(data); + 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. + * Restore the gamma ramps all CRTC:s with a site to the system settings * - * @param this The site state. + * @param this The site state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_dummy_site_restore(libgamma_site_state_t* restrict this) +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 == 0) - return errno = ENOTSUP, 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; + 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. + * Initialise an allocated partition state * - * @param this The partition state to initialise. + * @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. + * @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. + * 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) +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; - - 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 == NULL) - 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 != NULL) - { - crtc_data->info.edid = malloc(crtc_data->info.edid_length * sizeof(char)); - if (crtc_data->info.edid == NULL) - goto fail; - memcpy(crtc_data->info.edid, template.edid, crtc_data->info.edid_length * sizeof(char)); + 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)); + } } - if (crtc_data->info.connector_name != NULL) - { - size_t 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); } - } - - 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; + free(data->crtcs); + data->crtcs = NULL; + return LIBGAMMA_ERRNO_SET; } /** - * Release all resources held by a partition state. + * Release all resources held by a partition state * - * @param this The partition state. + * @param this The partition state */ -void libgamma_dummy_partition_destroy(libgamma_partition_state_t* restrict this) +void +libgamma_dummy_partition_destroy(libgamma_partition_state_t *restrict this) { - libgamma_dummy_partition_t* data = this->data; - size_t i; - - if (data == NULL) - return; - - 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; + 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. + * Restore the gamma ramps all CRTC:s with a partition to the system settings * - * @param this The partition state. + * @param this The partition state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_dummy_partition_restore(libgamma_partition_state_t* restrict this) +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 == 0) - return errno = ENOTSUP, 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; + 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. + * 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. + * @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. + * 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) +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; - - if ((data->gamma_red = malloc(data->info.red_gamma_size * stop_size)) == NULL) - goto fail; - if ((data->gamma_green = malloc(data->info.green_gamma_size * stop_size)) == NULL) - goto fail; - if ((data->gamma_blue = malloc(data->info.blue_gamma_size * stop_size)) == NULL) - 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; + 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. + * Release all resources held by a CRTC state * - * @param this The CRTC state. + * @param this The CRTC state */ -void libgamma_dummy_crtc_destroy(libgamma_crtc_state_t* restrict this) +void +libgamma_dummy_crtc_destroy(libgamma_crtc_state_t *restrict this) { - libgamma_dummy_crtc_t* data = this->data; - if (data == NULL) - return; - - free(data->gamma_red), data->gamma_red = NULL; - free(data->gamma_green), data->gamma_green = NULL; - free(data->gamma_blue), data->gamma_blue = NULL; + 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. + * Restore the gamma ramps for a CRTC to the system settings for that CRTC * - * @param this The CRTC state. + * @param this The CRTC state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_dummy_crtc_restore(libgamma_crtc_state_t* restrict this) +int +libgamma_dummy_crtc_restore(libgamma_crtc_state_t *restrict this) { - if (libgamma_dummy_configurations.capabilities.crtc_restore == 0) - return errno = ENOTSUP, LIBGAMMA_ERRNO_SET; - - return libgamma_dummy_crtc_restore_forced(this->data); + 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. + * 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. + * error identifier provided by this library */ -static int libgamma_dummy_crtc_restore_forced(libgamma_dummy_crtc_t* restrict data) +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 == NULL) - return 0; - -#define __reset_ramps(BITS) \ - int ## BITS ## _t* red = data->gamma_red; \ - int ## BITS ## _t* green = data->gamma_green; \ - int ## BITS ## _t* blue = data->gamma_blue; \ - double max = (double)INT ## BITS ## _MAX; \ - for (i = 0; i < rn; i++) \ - red[i] = (int ## BITS ## _t)(max * ((double)i / (double)(rn - 1))); \ - for (i = 0; i < gn; i++) \ - green[i] = (int ## BITS ## _t)(max * ((double)i / (double)(gn - 1))); \ - for (i = 0; i < bn; i++) \ - blue[i] = (int ## BITS ## _t)(max * ((double)i / (double)(bn - 1))) - - if (data->info.gamma_depth == 8) { __reset_ramps(8); } - else if (data->info.gamma_depth == 16) { __reset_ramps(16); } - else if (data->info.gamma_depth == 32) { __reset_ramps(32); } - else if (data->info.gamma_depth == 64) { __reset_ramps(64); } - else if (data->info.gamma_depth == -1) - { - float* red = data->gamma_red; - float* green = data->gamma_green; - float* blue = data->gamma_blue; - for (i = 0; i < rn; i++) - red[i] = (float)((double)i / (double)(rn - 1)); - for (i = 0; i < gn; i++) - green[i] = (float)((double)i / (double)(gn - 1)); - for (i = 0; i < bn; i++) - blue[i] = (float)((double)i / (double)(bn - 1)); - } - else - { - double* red = data->gamma_red; - double* green = data->gamma_green; - double* blue = data->gamma_blue; - for (i = 0; i < rn; i++) - red[i] = (double)i / (double)(rn - 1); - for (i = 0; i < gn; i++) - green[i] = (double)i / (double)(gn - 1); - for (i = 0; i < bn; i++) - blue[i] = (double)i / (double)(bn - 1); - } - + 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; + + return 0; } /** - * Read information about a CRTC. + * 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) + * @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; - - /* Copy over information. */ - *this = data->info; - - /* Duplicate strings. */ - if (this->edid != NULL) - { - this->edid = malloc(this->edid_length * sizeof(char)); - if (this->edid == NULL) - this->edid_error = errno; - memcpy(this->edid, data->info.edid, this->edid_length * sizeof(char)); - } - if (this->connector_name != NULL) - { - size_t n = strlen(this->connector_name); - this->connector_name = malloc((n + 1) * sizeof(char)); - if (this->connector_name == NULL) - 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; + 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; + + return e ? -1 : 0; } /** - * Get the current gamma ramps for a CRTC. + * Get the current gamma ramps for a CRTC * - * @param 1 The data type for the ramp stop elements. + * @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. + * 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. + * (`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. + * 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) +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; - + 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; + /* 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 == 0) - return LIBGAMMA_GAMMA_RAMP_READ_FAILED; - -#define __trans(DEPTH, SUFFIX) \ - if (data->info.gamma_depth == DEPTH) \ - return ramps_.${3} = *ramps, \ - libgamma_translated_ramp_get(this, &ramps_, ${4}, DEPTH, libgamma_crtc_get_gamma_ramps ## SUFFIX) + + 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); + __trans(8, 8); $>fi $>if [ ! ${4} = 16 ]; then - __trans(16, 16); + __trans(16, 16); $>fi $>if [ ! ${4} = 32 ]; then - __trans(32, 32); + __trans(32, 32); $>fi $>if [ ! ${4} = 64 ]; then - __trans(64, 64); + __trans(64, 64); $>fi $>if [ ! ${4} = -1 ]; then - __trans(-1, f); + __trans(-1, f); $>fi $>if [ ! ${4} = -2 ]; then - __trans(-2, d); + __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; + + 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. + * Set the gamma ramps for a CRTC * - * @param 1 The data type for the ramp stop elements. + * @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. + * 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. + * (`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. + * 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) +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; - + 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; + /* 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 == 0) - return LIBGAMMA_GAMMA_RAMP_READ_FAILED; - -#define __trans(DEPTH, SUFFIX) \ - if (data->info.gamma_depth == DEPTH) \ - return ramps_.${3} = ramps, \ - libgamma_translated_ramp_set(this, ramps_, ${4}, DEPTH, libgamma_crtc_set_gamma_ramps ## SUFFIX) + + 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); + __trans(8, 8); $>fi $>if [ ! ${4} = 16 ]; then - __trans(16, 16); + __trans(16, 16); $>fi $>if [ ! ${4} = 32 ]; then - __trans(32, 32); + __trans(32, 32); $>fi $>if [ ! ${4} = 64 ]; then - __trans(64, 64); + __trans(64, 64); $>fi $>if [ ! ${4} = -1 ]; then - __trans(-1, f); + __trans(-1, f); $>fi $>if [ ! ${4} = -2 ]; then - __trans(-2, d); + __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; + + 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. + * 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. + * @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. + * 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. + * Set the gamma ramps for a CRTC, 8-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * 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. + * 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. + * @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. + * 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. + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * 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. + * 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. + * @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. + * 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. + * Set the gamma ramps for a CRTC, 32-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * 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. + * 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. + * @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. + * 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. + * Set the gamma ramps for a CRTC, 64-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * 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. + * 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. + * @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. + * 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. + * Set the gamma ramps for a CRTC, `float` version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * 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. + * 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. + * @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. + * 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. + * Set the gamma ramps for a CRTC, `double` version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * 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 index c7d8e12..a52b087 100644 --- a/src/lib/gamma-dummy.h +++ b/src/lib/gamma-dummy.h @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #ifndef LIBGAMMA_GAMMA_DUMMY_H #define LIBGAMMA_GAMMA_DUMMY_H @@ -27,250 +11,233 @@ /** - * Return the capabilities of the adjustment method. + * Return the capabilities of the adjustment method * - * @param this The data structure to fill with the method's capabilities. + * @param this The data structure to fill with the method's capabilities */ -void libgamma_dummy_method_capabilities(libgamma_method_capabilities_t* restrict this); +void libgamma_dummy_method_capabilities(libgamma_method_capabilities_t *restrict); /** - * Initialise an allocated site state. + * 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. + * @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); +int libgamma_dummy_site_initialise(libgamma_site_state_t *restrict, char *restrict); /** - * Release all resources held by a site state. + * Release all resources held by a site state * - * @param this The site state. + * @param this The site state */ -void libgamma_dummy_site_destroy(libgamma_site_state_t* restrict this); +void libgamma_dummy_site_destroy(libgamma_site_state_t *restrict); /** - * Restore the gamma ramps all CRTC:s with a site to the system settings. + * Restore the gamma ramps all CRTC:s with a site to the system settings * - * @param this The site state. + * @param this The site state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_dummy_site_restore(libgamma_site_state_t* restrict this); +int libgamma_dummy_site_restore(libgamma_site_state_t *restrict); /** - * Initialise an allocated partition state. + * 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 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. + * 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); +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. + * Release all resources held by a partition state * - * @param this The partition state. + * @param this The partition state */ -void libgamma_dummy_partition_destroy(libgamma_partition_state_t* restrict this); +void libgamma_dummy_partition_destroy(libgamma_partition_state_t *restrict); /** - * Restore the gamma ramps all CRTC:s with a partition to the system settings. + * Restore the gamma ramps all CRTC:s with a partition to the system settings * - * @param this The partition state. + * @param this The partition state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_dummy_partition_restore(libgamma_partition_state_t* restrict this); +int libgamma_dummy_partition_restore(libgamma_partition_state_t *restrict); /** - * Initialise an allocated CRTC state. + * 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. + * @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. + * 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); +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. + * Release all resources held by a CRTC state * - * @param this The CRTC state. + * @param this The CRTC state */ -void libgamma_dummy_crtc_destroy(libgamma_crtc_state_t* restrict this); +void libgamma_dummy_crtc_destroy(libgamma_crtc_state_t *restrict); /** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC. + * Restore the gamma ramps for a CRTC to the system settings for that CRTC * - * @param this The CRTC state. + * @param this The CRTC state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_dummy_crtc_restore(libgamma_crtc_state_t* restrict this); +int libgamma_dummy_crtc_restore(libgamma_crtc_state_t *restrict); /** - * Read information about a CRTC. + * 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`. + * @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); +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. + * 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. + * @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. + * error identifier provided by this library */ -int libgamma_dummy_crtc_get_gamma_ramps8(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps8_t* restrict ramps); +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. + * Set the gamma ramps for a CRTC, 8-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * error identifier provided by this library */ -int libgamma_dummy_crtc_set_gamma_ramps8(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps8_t ramps); +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. + * 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. + * @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. + * error identifier provided by this library */ -int libgamma_dummy_crtc_get_gamma_ramps16(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps16_t* restrict ramps); +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. + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * error identifier provided by this library */ -int libgamma_dummy_crtc_set_gamma_ramps16(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps16_t ramps); +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. + * 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. + * @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. + * error identifier provided by this library */ -int libgamma_dummy_crtc_get_gamma_ramps32(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps32_t* restrict ramps); +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. + * Set the gamma ramps for a CRTC, 32-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * error identifier provided by this library */ -int libgamma_dummy_crtc_set_gamma_ramps32(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps32_t ramps); +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. + * 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. + * @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. + * error identifier provided by this library */ -int libgamma_dummy_crtc_get_gamma_ramps64(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps64_t* restrict ramps); +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. + * Set the gamma ramps for a CRTC, 64-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * error identifier provided by this library */ -int libgamma_dummy_crtc_set_gamma_ramps64(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps64_t ramps); +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. + * 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. + * @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. + * error identifier provided by this library */ -int libgamma_dummy_crtc_get_gamma_rampsf(libgamma_crtc_state_t* restrict this, - libgamma_gamma_rampsf_t* restrict ramps); +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. + * Set the gamma ramps for a CRTC, `float` version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * error identifier provided by this library */ -int libgamma_dummy_crtc_set_gamma_rampsf(libgamma_crtc_state_t* restrict this, - libgamma_gamma_rampsf_t ramps); +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. + * 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. + * @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. + * error identifier provided by this library */ -int libgamma_dummy_crtc_get_gamma_rampsd(libgamma_crtc_state_t* restrict this, - libgamma_gamma_rampsd_t* restrict ramps); +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. + * Set the gamma ramps for a CRTC, `double` version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * error identifier provided by this library */ -int libgamma_dummy_crtc_set_gamma_rampsd(libgamma_crtc_state_t* restrict this, - libgamma_gamma_rampsd_t ramps); +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 index 3aafbb3..f67e107 100644 --- a/src/lib/gamma-helper.c +++ b/src/lib/gamma-helper.c @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #include "gamma-helper.h" #include "libgamma-method.h" @@ -26,85 +10,86 @@ /** - * Just an arbitrary version. + * Just an arbitrary version */ -#define ANY bits64 +#define ANY bits64 /** - * Concatenation of all ramps. + * Concatenation of all ramps */ #define ALL red /** * Preform installation in an `for (i = 0; i < n; i++)` - * loop and do a `break` afterwords. + * loop and do a `break` afterwords */ -#define __translate(instruction) for (i = 0; i < n; i++) instruction; break +#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. + * value be 0 instead of ~0 and vice versa * - * @param value To `float` to convert. - * @return The value as an `uint64_t`. + * @param value To `float` to convert + * @return The value as an `uint64_t` */ -static inline uint64_t float_to_64(float value) +static inline uint64_t +float_to_64(float value) { - /* XXX Which is faster? */ + /* 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. */ + /* `__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; - + + /* 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 */ - /* 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; - + /* 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 } @@ -112,272 +97,276 @@ static inline uint64_t float_to_64(float value) /** * 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. + * value be 0 instead of ~0 and vice versa * - * @param value To `double` to convert. - * @return The value as an `uint64_t`. + * @param value To `double` to convert + * @return The value as an `uint64_t` */ -static inline uint64_t double_to_64(double value) +static inline uint64_t +double_to_64(double value) { - /* XXX Which is faster? */ - + /* 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. */ + /* `__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; - + + /* 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; - + + /* 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. + * 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. + * @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) +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; - } + 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`. + * 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. + * @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) +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; - } + 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. + * 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 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. + * `-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. + * 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) +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. */ + /* 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); + /* 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); + 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 == NULL ? LIBGAMMA_ERRNO_SET : 0; + 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. + * 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 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`. + * `-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`. + * `-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. + * and the return value too is identical * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * 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) +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; + 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. */ - if ((ramps_full = malloc(n * sizeof(uint64_t))) == NULL) - return free(ramps_sys.ANY.red), 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; + /* 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. + * Set the gamma ramps for a CRTC, re-encoding version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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`. + * `-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`. + * `-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. + * and the return value too is identical * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * 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) +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. */ - if ((ramps_full = malloc(n * sizeof(uint64_t))) == NULL) - return free(ramps_sys.ANY.red), 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; + 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 index 9ccc183..38d678d 100644 --- a/src/lib/gamma-helper.h +++ b/src/lib/gamma-helper.h @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #ifndef LIBGAMMA_GAMMA_HELPER_H #define LIBGAMMA_GAMMA_HELPER_H @@ -23,148 +7,138 @@ /** - * Gamma ramp structure union for different depths. + * 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; - +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. + * A function for reading the gamma ramps from a CRTC * - * @param this The CRTC state. - * @param ramps The store for the gamma ramps. + * @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. + * error identifier provided by this library */ -typedef int libgamma_get_ramps_any_fun(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps_any_t* restrict ramps); +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. + * A function for writing the gamma ramps to a CRTC * - * @param this The CRTC state. - * @param ramps The gamma ramps. + * @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. + * error identifier provided by this library */ -typedef int libgamma_set_ramps_any_fun(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps_any_t ramps); +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 versio.n + * 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 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`. + * `-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`. + * `-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. + * and the return value too is identical * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * 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)) +#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 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`. + * `-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`. + * `-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. + * and the return value too is identical * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * 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)) +#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. + * 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 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`. + * `-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`. + * `-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. + * and the return value too is identical * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * 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); +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. + * Set the gamma ramps for a CRTC, re-encoding version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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`. + * `-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`. + * `-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. + * and the return value too is identical * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * 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); +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 index 9693668..c2fc646 100644 --- a/src/lib/gamma-linux-drm.c +++ b/src/lib/gamma-linux-drm.c @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* 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 @@ -41,88 +25,87 @@ #include <xf86drmMode.h> #ifndef O_CLOEXEC -# define O_CLOEXEC 02000000 +# define O_CLOEXEC 02000000 #endif #ifndef NGROUPS_MAX -# define NGROUPS_MAX 65536 +# define NGROUPS_MAX 65536 #endif #ifndef PATH_MAX -# define PATH_MAX 4096 +# define PATH_MAX 4096 #endif /** - * Graphics card data for the Direct Rendering Manager adjustment method. + * 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; - +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. + * Return the capabilities of the adjustment method * - * @param this The data structure to fill with the method's capabilities. + * @param this The data structure to fill with the method's capabilities */ -void libgamma_linux_drm_method_capabilities(libgamma_method_capabilities_t* restrict this) +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; + /* 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. + * Initialise an allocated site state * - * @param this The site state to initialise. + * @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 @@ -130,58 +113,60 @@ void libgamma_linux_drm_method_capabilities(libgamma_method_capabilities_t* rest * 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. + * error identifier provided by this library */ -int libgamma_linux_drm_site_initialise(libgamma_site_state_t* restrict this, - char* restrict site) +int +libgamma_linux_drm_site_initialise(libgamma_site_state_t *restrict this, char *restrict site) { - char pathname[PATH_MAX]; - struct stat _attr; - - if (site != NULL) - 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; + 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. + * Release all resources held by a site state * - * @param this The site state. + * @param this The site state */ -void libgamma_linux_drm_site_destroy(libgamma_site_state_t* restrict this) +void +libgamma_linux_drm_site_destroy(libgamma_site_state_t *restrict this) { - (void) this; + (void) this; } /** - * Restore the gamma ramps all CRTC:s with a site to the system settings. + * Restore the gamma ramps all CRTC:s with a site to the system settings * - * @param this The site state. + * @param this The site state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_linux_drm_site_restore(libgamma_site_state_t* restrict this) +int +libgamma_linux_drm_site_restore(libgamma_site_state_t *restrict this) { - (void) this; - return errno = ENOTSUP, LIBGAMMA_ERRNO_SET; + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; } @@ -191,209 +176,199 @@ int libgamma_linux_drm_site_restore(libgamma_site_state_t* restrict this) * @param pathname The pathname of the error card * @return The error code to report */ -static int figure_out_card_open_error(const char* pathname) +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) < 0) - 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 == 0 /* root group */ || __test(S_IRGRP, S_IWGRP)) - return LIBGAMMA_DEVICE_RESTRICTED; - - - /* Get the user's supplemental group membership list. */ - if ((n = getgroups(NGROUPS_MAX, supplemental_groups)) < 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. */ -#if _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE || _POSIX_SOURCE - /* Thread-safe. */ - { - static __thread char buf[1024]; /* My output of `sysconf(_SC_GETGR_R_SIZE_MAX)`. */ - struct group _grp; + 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; - } -#else -# ifdef __GCC__ -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wcpp" -# warning figure_out_card_open_error is not thread-safe. -# pragma GCC diagnostic pop -# endif - /* Not thread-safe. */ - errno = 0; - group = getgrgid(attr.st_gid); -#endif - - libgamma_group_gid = attr.st_gid; - libgamma_group_name = group != NULL ? group->gr_name : NULL; - return LIBGAMMA_DEVICE_REQUIRE_GROUP; + 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. + * 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. + * @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. + * 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 +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 == NULL) - 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 == NULL) - { - 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; + 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. + * Release all connectors and encoders * - * @param this The graphics card data. + * @param this The graphics card data */ -static void release_connectors_and_encoders(libgamma_drm_card_data_t* restrict this) +static void +release_connectors_and_encoders(libgamma_drm_card_data_t *restrict this) { - size_t i, n; - /* Release individual encoders. */ - if (this->encoders != NULL) - for (i = 0, n = (size_t)(this->res->count_connectors); i < n; i++) - if (this->encoders[i] != NULL) - drmModeFreeEncoder(this->encoders[i]); - /* Release encoder array. */ - free(this->encoders); - this->encoders = NULL; - - /* Release individual connectors. */ - if (this->connectors != NULL) - for (i = 0, n = (size_t)(this->res->count_connectors); i < n; i++) - if (this->connectors[i] != NULL) - drmModeFreeConnector(this->connectors[i]); - /* Release connector array. */ - free(this->connectors); - this->connectors = NULL; + 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. + * Release all resources held by a partition state * - * @param this The partition state. + * @param this The partition state */ -void libgamma_linux_drm_partition_destroy(libgamma_partition_state_t* restrict this) +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 != NULL) drmModeFreeResources(data->res); - if (data->fd >= 0) close(data->fd); - free(data); + 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); } @@ -404,43 +379,47 @@ void libgamma_linux_drm_partition_destroy(libgamma_partition_state_t* restrict t * @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) +int +libgamma_linux_drm_partition_restore(libgamma_partition_state_t *restrict this) { - (void) this; - return errno = ENOTSUP, LIBGAMMA_ERRNO_SET; + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; } /** - * Initialise an allocated CRTC state. + * 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. + * @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. + * 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) +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; + 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. + * Release all resources held by a CRTC state * - * @param this The CRTC state. + * @param this The CRTC state */ -void libgamma_linux_drm_crtc_destroy(libgamma_crtc_state_t* restrict this) +void +libgamma_linux_drm_crtc_destroy(libgamma_crtc_state_t *restrict this) { - (void) this; + (void) this; } @@ -451,125 +430,130 @@ void libgamma_linux_drm_crtc_destroy(libgamma_crtc_state_t* restrict this) * @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) +int +libgamma_linux_drm_crtc_restore(libgamma_crtc_state_t *restrict this) { - (void) this; - return errno = ENOTSUP, LIBGAMMA_ERRNO_SET; + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; } /** - * Find the connector that a CRTC belongs to. + * Find the connector that a CRTC belongs to * - * @param this The CRTC state. + * @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. + * 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) +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 == NULL) - { - /* Allocate connector and encoder arrays. - We use `calloc` so all non-loaded elements are `NULL` after an error. */ - if ((card->connectors = calloc(n, sizeof(drmModeConnector*))) == NULL) goto fail; - if ((card->encoders = calloc(n, sizeof(drmModeEncoder*))) == NULL) goto fail; - /* Fill connector and encoder arrays. */ - for (i = 0; i < n; i++) - { - /* Get connector, */ - if ((card->connectors[i] = drmModeGetConnector(card->fd, card->res->connectors[i])) == NULL) - 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 != 0) && - ((card->encoders[i] = drmModeGetEncoder(card->fd, card->connectors[i]->encoder_id)) == NULL)) - goto fail; + 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] != NULL) && (card->connectors[i] != NULL) && (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; + /* 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. + * 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`. + * @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) +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 == NULL ? errno : 0; - /* Get gamma ramp size. */ - if (out->gamma_size_error == 0) - { - /* 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; + 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. + * @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) +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; - } - +#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 } @@ -577,346 +561,342 @@ static void get_subpixel_order(libgamma_crtc_information_t* restrict out, /** * 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. + * @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) +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. */ +#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 +# define DRM_MODE_CONNECTOR_VIRTUAL 15 #endif #ifndef DRM_MODE_CONNECTOR_DSI -# define DRM_MODE_CONNECTOR_DSI 16 +# 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; - } - + + /* 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. + * 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. + * @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) +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; - - /* 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 == 0) - { - 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; + 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 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 == 0)) - { - libgamma_drm_card_data_t* restrict card = crtc->partition->data; - uint32_t type = connector->connector_type; - size_t i, n = (size_t)(card->res->count_connectors), c = 0; - - /* Allocate memory for the name of the connector. */ - out->connector_name = malloc((strlen(connector_name_base) + 12) * sizeof(char)); - if (out->connector_name == NULL) - return out->connector_name_error = errno; - - /* Get the number of connectors with the same type on the same graphics card. */ - for (i = 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 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. + * 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. + * @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) +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, */ - if ((prop = drmModeGetProperty(card->fd, connector->props[prop_i])) == NULL) - continue; - /* Is this property the EDID? */ - if (!strcmp(prop->name, "EDID")) - { - /* Get the property value. */ - if ((blob = drmModeGetPropertyBlob(card->fd, (uint32_t)(connector->prop_values[prop_i]))) == NULL) - return drmModeFreeProperty(prop), out->edid_error = LIBGAMMA_PROPERTY_VALUE_QUERY_FAILED; - if (blob->data != NULL) - { - /* 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. */ - if ((out->edid = malloc(out->edid_length * sizeof(unsigned char))) == NULL) - 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 == NULL; - } - /* Free the propriety value. */ - drmModeFreePropertyBlob(blob); + 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); } - /* Free the propriety. */ - drmModeFreeProperty(prop); - } - /* If we get here, we did not find a EDID. */ - return out->edid_error = LIBGAMMA_EDID_NOT_FOUND; + /* If we get here, we did not find a EDID */ + return (out->edid_error = LIBGAMMA_EDID_NOT_FOUND); } /** - * Read information about a CRTC. + * 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`. + * @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) +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; - drmModeConnector* restrict connector; - int require_connector; - int free_edid; - int 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) == 0; - - /* 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 == 0) - goto cont; - /* Find connector. */ - if ((connector = find_connector(crtc, &error)) == NULL) - { - /* 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) == 0) - goto cont; - /* If there is not monitor that report error in EDID related fields. */ - if (this->active_error || (this->active == 0)) - { - 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 == NULL) - { - 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; +#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. + * 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. + * @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. + * 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) +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; + 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; + /* 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; + /* 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. + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * 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) +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; + 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; + /* 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; -} + /* 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 index ee037bc..41a654d 100644 --- a/src/lib/gamma-linux-drm.h +++ b/src/lib/gamma-linux-drm.h @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #ifndef LIBGAMMA_GAMMA_LINUX_DRM_H #define LIBGAMMA_GAMMA_LINUX_DRM_H @@ -27,16 +11,16 @@ /** - * Return the capabilities of the adjustment method. + * Return the capabilities of the adjustment method * - * @param this The data structure to fill with the method's capabilities. + * @param this The data structure to fill with the method's capabilities */ -void libgamma_linux_drm_method_capabilities(libgamma_method_capabilities_t* restrict this); +void libgamma_linux_drm_method_capabilities(libgamma_method_capabilities_t *restrict); /** - * Initialise an allocated site state. + * Initialise an allocated site state * - * @param this The site state to initialise. + * @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 @@ -44,119 +28,112 @@ void libgamma_linux_drm_method_capabilities(libgamma_method_capabilities_t* rest * 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. + * error identifier provided by this library */ -int libgamma_linux_drm_site_initialise(libgamma_site_state_t* restrict this, - char* restrict site); +int libgamma_linux_drm_site_initialise(libgamma_site_state_t *restrict, char *restrict); /** - * Release all resources held by a site state. + * Release all resources held by a site state * - * @param this The site state. + * @param this The site state */ -void libgamma_linux_drm_site_destroy(libgamma_site_state_t* restrict this) __attribute__((const)); +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. + * Restore the gamma ramps all CRTC:s with a site to the system settings * - * @param this The site state. + * @param this The site state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_linux_drm_site_restore(libgamma_site_state_t* restrict this); +int libgamma_linux_drm_site_restore(libgamma_site_state_t *restrict); /** - * Initialise an allocated partition state. + * 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. + * @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. + * 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 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. + * Release all resources held by a partition state * - * @param this The partition state. + * @param this The partition state */ -void libgamma_linux_drm_partition_destroy(libgamma_partition_state_t* restrict this); +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. + * Restore the gamma ramps all CRTC:s with a partition to the system settings * - * @param this The partition state. + * @param this The partition state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_linux_drm_partition_restore(libgamma_partition_state_t* restrict this); +int libgamma_linux_drm_partition_restore(libgamma_partition_state_t *restrict); /** - * Initialise an allocated CRTC state. + * Initialise an allocated CRTC state * - * @param this The CRTC state to initialise. + * @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. + * @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. + * 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); +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. + * Release all resources held by a CRTC state * - * @param this The CRTC state. + * @param this The CRTC state */ -void libgamma_linux_drm_crtc_destroy(libgamma_crtc_state_t* restrict this) __attribute__((const)); +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. + * Restore the gamma ramps for a CRTC to the system settings for that CRTC * - * @param this The CRTC state. + * @param this The CRTC state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_linux_drm_crtc_restore(libgamma_crtc_state_t* restrict this); +int libgamma_linux_drm_crtc_restore(libgamma_crtc_state_t *restrict); /** - * Read information about a CRTC. + * 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`. + * @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); +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. + * 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. + * @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. + * 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); +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. + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * 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); +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 index d2ceebc..b0e3a29 100644 --- a/src/lib/gamma-quartz-cg.c +++ b/src/lib/gamma-quartz-cg.c @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* 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 @@ -28,7 +12,7 @@ #else # include <ApplicationServices/ApplicationServices.h> # include <CoreGraphics/CGDirectDisplay.h> -# define close_fake_quartz_cg() /* For compatibility with "fake-quartz-cg.h". */ +# define close_fake_quartz_cg() /* For compatibility with "fake-quartz-cg.h" */ #endif #include <stdlib.h> @@ -36,56 +20,56 @@ /** - * Return the capabilities of the adjustment method. + * Return the capabilities of the adjustment method * - * @param this The data structure to fill with the method's capabilities. + * @param this The data structure to fill with the method's capabilities */ -void libgamma_quartz_cg_method_capabilities(libgamma_method_capabilities_t* restrict this) +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 support... */ - 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. */ + /* 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; + /* 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; + this->real = 1; # else - this->real = 0; + 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; + /* 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; + /* Gamma ramp adjustments are non-persistent */ + this->auto_restore = 1; } /** - * Initialise an allocated site state. + * Initialise an allocated site state * - * @param this The site state to initialise. + * @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 @@ -93,274 +77,283 @@ void libgamma_quartz_cg_method_capabilities(libgamma_method_capabilities_t* rest * 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. + * error identifier provided by this library */ -int libgamma_quartz_cg_site_initialise(libgamma_site_state_t* restrict this, - char* restrict site) +int +libgamma_quartz_cg_site_initialise(libgamma_site_state_t *restrict this, char *restrict site) { - this->partitions_available = 1; - return site != NULL ? LIBGAMMA_NO_SUCH_SITE : 0; + this->partitions_available = 1; + return site ? 0 : LIBGAMMA_NO_SUCH_SITE; } /** - * Release all resources held by a site state. + * Release all resources held by a site state * - * @param this The site state. + * @param this The site state */ -void libgamma_quartz_cg_site_destroy(libgamma_site_state_t* restrict this) +void +libgamma_quartz_cg_site_destroy(libgamma_site_state_t *restrict this) { - (void) this; - close_fake_quartz_cg(); + (void) this; + close_fake_quartz_cg(); } /** - * Restore the gamma ramps all CRTC:s with a site to the system settings. + * Restore the gamma ramps all CRTC:s with a site to the system settings * - * @param this The site state. + * @param this The site state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_quartz_cg_site_restore(libgamma_site_state_t* restrict this) +int +libgamma_quartz_cg_site_restore(libgamma_site_state_t *restrict this) { - (void) this; - CGDisplayRestoreColorSyncSettings(); - return 0; + (void) this; + CGDisplayRestoreColorSyncSettings(); + return 0; } /** - * Initialise an allocated partition state. + * 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. + * @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. + * 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) +int +libgamma_quartz_cg_partition_initialise(libgamma_partition_state_t *restrict this, + libgamma_site_state_t *restrict site, size_t partition) { - uint32_t cap = 4, n; - CGDirectDisplayID* crtcs; - CGDirectDisplayID* crtcs_old; - - (void) site; - - this->data = NULL; - - if (partition != 0) - return LIBGAMMA_NO_SUCH_PARTITION; - - /* Allocate array of CRTC ID:s. */ - if ((crtcs = malloc((size_t)cap * sizeof(CGDirectDisplayID))) == NULL) - 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 <<= 1) == 0) /* We could also test ~0, but it is still too many. */ - return free(crtcs), LIBGAMMA_IMPOSSIBLE_AMOUNT; - /* Grow the array of CRTC ID:s so that it can fit all we are asking for. */ - if ((crtcs = realloc(crtcs_old = crtcs, (size_t)cap * sizeof(CGDirectDisplayID))) == NULL) - return free(crtcs_old), LIBGAMMA_ERRNO_SET; - } - - /* Store CRTC ID:s and CRTC count. */ - this->data = crtcs; - this->crtcs_available = (size_t)n; - return 0; + 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. + * Release all resources held by a partition state * - * @param this The partition state. + * @param this The partition state */ -void libgamma_quartz_cg_partition_destroy(libgamma_partition_state_t* restrict this) +void +libgamma_quartz_cg_partition_destroy(libgamma_partition_state_t *restrict this) { - free(this->data); + free(this->data); } /** - * Restore the gamma ramps all CRTC:s with a partition to the system settings. + * Restore the gamma ramps all CRTC:s with a partition to the system settings * - * @param this The partition state. + * @param this The partition state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_quartz_cg_partition_restore(libgamma_partition_state_t* restrict this) +int +libgamma_quartz_cg_partition_restore(libgamma_partition_state_t *restrict this) { - return libgamma_quartz_cg_site_restore(this->site); + return libgamma_quartz_cg_site_restore(this->site); } /** - * Initialise an allocated CRTC state. + * 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. + * @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. + * 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) +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 ? LIBGAMMA_NO_SUCH_CRTC : 0; + (void) this; + return crtc < partition->crtcs_available ? 0 : LIBGAMMA_NO_SUCH_CRTC; } /** - * Release all resources held by a CRTC state. + * Release all resources held by a CRTC state * - * @param this The CRTC state. + * @param this The CRTC state */ -void libgamma_quartz_cg_crtc_destroy(libgamma_crtc_state_t* restrict this) +void +libgamma_quartz_cg_crtc_destroy(libgamma_crtc_state_t *restrict this) { - (void) this; + (void) this; } /** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC. + * Restore the gamma ramps for a CRTC to the system settings for that CRTC * - * @param this The CRTC state. + * @param this The CRTC state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_quartz_cg_crtc_restore(libgamma_crtc_state_t* restrict this) +int +libgamma_quartz_cg_crtc_restore(libgamma_crtc_state_t *restrict this) { - (void) this; - return errno = ENOTSUP, LIBGAMMA_ERRNO_SET; + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; } /** - * Read information about a CRTC. + * 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 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`. + * @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) +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) - - /* 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)) - { - CGDirectDisplayID* restrict crtcs = crtc->partition->data; - size_t 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; - +#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. + * 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. + * @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. + * 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) +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; + 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; + /* 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; + /* 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. + * Set the gamma ramps for a CRTC, `float` version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * 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) +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; + 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; + /* 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; + /* 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 index 43b9bb3..211e055 100644 --- a/src/lib/gamma-quartz-cg.h +++ b/src/lib/gamma-quartz-cg.h @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #ifndef LIBGAMMA_GAMMA_QUARTZ_CG_H #define LIBGAMMA_GAMMA_QUARTZ_CG_H @@ -27,16 +11,16 @@ /** - * Return the capabilities of the adjustment method. + * Return the capabilities of the adjustment method * - * @param this The data structure to fill with the method's capabilities. + * @param this The data structure to fill with the method's capabilities */ -void libgamma_quartz_cg_method_capabilities(libgamma_method_capabilities_t* restrict this); +void libgamma_quartz_cg_method_capabilities(libgamma_method_capabilities_t *restrict); /** - * Initialise an allocated site state. + * Initialise an allocated site state * - * @param this The site state to initialise. + * @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 @@ -44,119 +28,113 @@ void libgamma_quartz_cg_method_capabilities(libgamma_method_capabilities_t* rest * 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. + * error identifier provided by this library */ -int libgamma_quartz_cg_site_initialise(libgamma_site_state_t* restrict this, - char* restrict site); +int libgamma_quartz_cg_site_initialise(libgamma_site_state_t *restrict, char *restrict); /** - * Release all resources held by a site state. + * Release all resources held by a site state * - * @param this The site state. + * @param this The site state */ -void libgamma_quartz_cg_site_destroy(libgamma_site_state_t* restrict this); +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. + * Restore the gamma ramps all CRTC:s with a site to the system settings * - * @param this The site state. + * @param this The site state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_quartz_cg_site_restore(libgamma_site_state_t* restrict this); +int libgamma_quartz_cg_site_restore(libgamma_site_state_t *restrict); /** - * Initialise an allocated partition state. + * 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 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. + * 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); +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. + * Release all resources held by a partition state * - * @param this The partition state. + * @param this The partition state */ -void libgamma_quartz_cg_partition_destroy(libgamma_partition_state_t* restrict this); +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. + * Restore the gamma ramps all CRTC:s with a partition to the system settings * - * @param this The partition state. + * @param this The partition state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_quartz_cg_partition_restore(libgamma_partition_state_t* restrict this); +int libgamma_quartz_cg_partition_restore(libgamma_partition_state_t *restrict); /** - * Initialise an allocated CRTC state. + * 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. + * @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. + * 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) __attribute__((pure)); +__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. + * Release all resources held by a CRTC state * - * @param this The CRTC state. + * @param this The CRTC state */ -void libgamma_quartz_cg_crtc_destroy(libgamma_crtc_state_t* restrict this) __attribute__((const)); +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. + * Restore the gamma ramps for a CRTC to the system settings for that CRTC * - * @param this The CRTC state. + * @param this The CRTC state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_quartz_cg_crtc_restore(libgamma_crtc_state_t* restrict this); +int libgamma_quartz_cg_crtc_restore(libgamma_crtc_state_t *restrict); /** - * Read information about a CRTC. + * 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`. + * @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); +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. + * 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. + * @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. + * 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); +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. + * Set the gamma ramps for a CRTC, `float` version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * 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); +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 index f9c8094..e93b53d 100644 --- a/src/lib/gamma-w32-gdi.c +++ b/src/lib/gamma-w32-gdi.c @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* 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 @@ -24,7 +8,7 @@ #include "libgamma-error.h" #ifndef WINVER -# define WINVER 0x0500 +# define WINVER 0x0500 #endif #ifdef FAKE_LIBGAMMA_METHOD_W32_GDI # include "fake-w32-gdi.h" @@ -37,62 +21,62 @@ /** - * The gamma ramp size that devices will always have in Windows GDI. + * 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 +#define GAMMA_RAMP_SIZE 256 /** - * Return the capabilities of the adjustment method. + * Return the capabilities of the adjustment method * - * @param this The data structure to fill with the method's capabilities. + * @param this The data structure to fill with the method's capabilities */ -void libgamma_w32_gdi_method_capabilities(libgamma_method_capabilities_t* restrict this) +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 support... */ - 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. */ + /* 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; + /* 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; + this->real = 1; # else - this->real = 0; + this->real = 0; # endif #else - /* It is real and not fake if we are running on Windows. */ - this->fake = 0; - this->real = 1; + /* 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; + /* Gamma ramp adjustments are persistent */ + this->auto_restore = 0; } /** - * Initialise an allocated site state. + * Initialise an allocated site state * - * @param this The site state to initialise. + * @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 @@ -100,266 +84,277 @@ void libgamma_w32_gdi_method_capabilities(libgamma_method_capabilities_t* restri * 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. + * error identifier provided by this library */ -int libgamma_w32_gdi_site_initialise(libgamma_site_state_t* restrict this, - char* restrict site) +int +libgamma_w32_gdi_site_initialise(libgamma_site_state_t *restrict this, char* restrict site) { - this->partitions_available = 1; - return site != NULL ? LIBGAMMA_NO_SUCH_SITE : 0; + this->partitions_available = 1; + return !site ? 0 : LIBGAMMA_NO_SUCH_SITE; } /** - * Release all resources held by a site state. + * Release all resources held by a site state * - * @param this The site state. + * @param this The site state */ -void libgamma_w32_gdi_site_destroy(libgamma_site_state_t* restrict this) +void +libgamma_w32_gdi_site_destroy(libgamma_site_state_t *restrict this) { - (void) this; + (void) this; } /** - * Restore the gamma ramps all CRTC:s with a site to the system settings. + * Restore the gamma ramps all CRTC:s with a site to the system settings * - * @param this The site state. + * @param this The site state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_w32_gdi_site_restore(libgamma_site_state_t* restrict this) +int +libgamma_w32_gdi_site_restore(libgamma_site_state_t *restrict this) { - (void) this; - return errno = ENOTSUP, LIBGAMMA_ERRNO_SET; + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; } /** - * Initialise an allocated partition state. + * 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. + * @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. + * 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) +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 != 0) - 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++, n == 0) - return LIBGAMMA_IMPOSSIBLE_AMOUNT; - this->crtcs_available = (size_t)n; - return 0; + 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. + * Release all resources held by a partition state * - * @param this The partition state. + * @param this The partition state */ -void libgamma_w32_gdi_partition_destroy(libgamma_partition_state_t* restrict this) +void +libgamma_w32_gdi_partition_destroy(libgamma_partition_state_t *restrict this) { - (void) this; + (void) this; } /** - * Restore the gamma ramps all CRTC:s with a partition to the system settings. + * Restore the gamma ramps all CRTC:s with a partition to the system settings * - * @param this The partition state. + * @param this The partition state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_w32_gdi_partition_restore(libgamma_partition_state_t* restrict this) +int +libgamma_w32_gdi_partition_restore(libgamma_partition_state_t *restrict this) { - (void) this; - return errno = ENOTSUP, LIBGAMMA_ERRNO_SET; + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; } /** - * Initialise an allocated CRTC state. + * 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. + * @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. + * 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) +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 == NULL) - return LIBGAMMA_OPEN_CRTC_FAILED; - this->data = context; - return 0; + 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. + * Release all resources held by a CRTC state * - * @param this The CRTC state. + * @param this The CRTC state */ -void libgamma_w32_gdi_crtc_destroy(libgamma_crtc_state_t* restrict this) +void +libgamma_w32_gdi_crtc_destroy(libgamma_crtc_state_t *restrict this) { - if (this->data) - ReleaseDC(NULL, this->data); + if (this->data) + ReleaseDC(NULL, this->data); } /** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC. + * Restore the gamma ramps for a CRTC to the system settings for that CRTC * - * @param this The CRTC state. + * @param this The CRTC state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_w32_gdi_crtc_restore(libgamma_crtc_state_t* restrict this) +int +libgamma_w32_gdi_crtc_restore(libgamma_crtc_state_t *restrict this) { - (void) this; - return errno = ENOTSUP, LIBGAMMA_ERRNO_SET; + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; } /** - * Read information about a CRTC. + * 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 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`. + * @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) +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; - +#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 +#undef KNOWN_FIELDS } /** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version. + * 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. + * @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. + * 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) +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; + /* 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; + /* 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. + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * 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) +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; + /* 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; + /* 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 index 3efe691..d139f16 100644 --- a/src/lib/gamma-w32-gdi.h +++ b/src/lib/gamma-w32-gdi.h @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #ifndef LIBGAMMA_GAMMA_W32_GDI_H #define LIBGAMMA_GAMMA_W32_GDI_H @@ -27,16 +11,16 @@ /** - * Return the capabilities of the adjustment method. + * Return the capabilities of the adjustment method * - * @param this The data structure to fill with the method's capabilities. + * @param this The data structure to fill with the method's capabilities */ -void libgamma_w32_gdi_method_capabilities(libgamma_method_capabilities_t* restrict this); +void libgamma_w32_gdi_method_capabilities(libgamma_method_capabilities_t *restrict); /** - * Initialise an allocated site state. + * Initialise an allocated site state * - * @param this The site state to initialise. + * @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 @@ -44,119 +28,112 @@ void libgamma_w32_gdi_method_capabilities(libgamma_method_capabilities_t* restri * 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. + * error identifier provided by this library */ -int libgamma_w32_gdi_site_initialise(libgamma_site_state_t* restrict this, - char* restrict site); +int libgamma_w32_gdi_site_initialise(libgamma_site_state_t *restrict, char *restrict); /** - * Release all resources held by a site state. + * Release all resources held by a site state * - * @param this The site state. + * @param this The site state */ -void libgamma_w32_gdi_site_destroy(libgamma_site_state_t* restrict this) __attribute__((const)); +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. + * Restore the gamma ramps all CRTC:s with a site to the system settings * - * @param this The site state. + * @param this The site state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_w32_gdi_site_restore(libgamma_site_state_t* restrict this); +int libgamma_w32_gdi_site_restore(libgamma_site_state_t *restrict); /** - * Initialise an allocated partition state. + * 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. + * @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. + * 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); +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. + * Release all resources held by a partition state * - * @param this The partition state. + * @param this The partition state */ -void libgamma_w32_gdi_partition_destroy(libgamma_partition_state_t* restrict this) __attribute__((const)); +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. + * Restore the gamma ramps all CRTC:s with a partition to the system settings * - * @param this The partition state. + * @param this The partition state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_w32_gdi_partition_restore(libgamma_partition_state_t* restrict this); +int libgamma_w32_gdi_partition_restore(libgamma_partition_state_t *restrict); /** - * Initialise an allocated CRTC state. + * 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. + * @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. + * 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); +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. + * Release all resources held by a CRTC state * - * @param this The CRTC state. + * @param this The CRTC state */ -void libgamma_w32_gdi_crtc_destroy(libgamma_crtc_state_t* restrict this); +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. + * Restore the gamma ramps for a CRTC to the system settings for that CRTC * - * @param this The CRTC state. + * @param this The CRTC state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_w32_gdi_crtc_restore(libgamma_crtc_state_t* restrict this); +int libgamma_w32_gdi_crtc_restore(libgamma_crtc_state_t *restrict); /** - * Read information about a CRTC. + * 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`. + * @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); +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. + * 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. + * @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. + * 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); +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. + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * 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); +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 index fb002fc..fce65d1 100644 --- a/src/lib/gamma-x-randr.c +++ b/src/lib/gamma-x-randr.c @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* 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 @@ -37,143 +21,144 @@ /** - * The major version of RandR the library expects. + * The major version of RandR the library expects */ #define RANDR_VERSION_MAJOR 1 /** - * The minor version of RandR the library expects. + * The minor version of RandR the library expects */ #define RANDR_VERSION_MINOR 3 /** - * Data structure for partition data. + * 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; - +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. + * 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. + * @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) +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; - } + 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. + * 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. + * @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) +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) : r; + int r = translate_error_(error_code, default_error); + return (return_errno && r > 0) ? errno : r; } /** - * Return the capabilities of the adjustment method. + * Return the capabilities of the adjustment method * - * @param this The data structure to fill with the method's capabilities. + * @param this The data structure to fill with the method's capabilities */ -void libgamma_x_randr_method_capabilities(libgamma_method_capabilities_t* restrict this) +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) ? 1 : 0; - 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; + 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 __GCC__ +/* 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. + * Initialise an allocated site state * - * @param this The site state to initialise. + * @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 @@ -181,802 +166,820 @@ void libgamma_x_randr_method_capabilities(libgamma_method_capabilities_t* restri * 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. + * error identifier provided by this library */ -int libgamma_x_randr_site_initialise(libgamma_site_state_t* restrict this, - char* restrict site) +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 == NULL || 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 != NULL) || (reply == NULL)) - { - /* 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 == NULL) && (reply == NULL)) - 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)) - { + 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); + /* 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. */ - if ((setup = xcb_get_setup(connection)) == NULL) - return xcb_disconnect(connection), 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 : 0; + /* 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. + * Release all resources held by a site state * - * @param this The site state. + * @param this The site state */ -void libgamma_x_randr_site_destroy(libgamma_site_state_t* restrict this) +void +libgamma_x_randr_site_destroy(libgamma_site_state_t *restrict this) { - xcb_disconnect((xcb_connection_t*)(this->data)); + xcb_disconnect((xcb_connection_t *)this->data); } /** - * Restore the gamma ramps all CRTC:s with a site to the system settings. + * Restore the gamma ramps all CRTC:s with a site to the system settings * - * @param this The site state. + * @param this The site state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_x_randr_site_restore(libgamma_site_state_t* restrict this) +int +libgamma_x_randr_site_restore(libgamma_site_state_t *restrict this) { - (void) this; - return errno = ENOTSUP, LIBGAMMA_ERRNO_SET; + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; } /** - * Duplicate a memory area. + * Duplicate a memory area * - * @param ptr The memory aree. - * @param bytes The size, in bytes, of the memory area. - * @return A duplication of the memory, `NULL` if zero-length or on error. + * @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) +static inline void * +memdup(void *restrict ptr, size_t bytes) { - char* restrict rc; - if ((bytes == 0) || ((rc = malloc(bytes)) == NULL)) - return NULL; - memcpy(rc, ptr, bytes); - return rc; + 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. + * Initialise an allocated partition state * - * @param this The partition state to initialise. + * @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. + * @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. + * 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 +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; - size_t i; - - /* Get screen list. */ - if ((setup = xcb_get_setup(connection)) == NULL) - 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 == 0) - return LIBGAMMA_NO_SUCH_PARTITION; - - /* Check that the screen is not `NULL`. (Do not think this can happen, but why not.) */ - if (screen == NULL) - 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 != NULL) - 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 == NULL) || (outputs == NULL)) - return free(reply), 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`. */ - if ((data = calloc(1, sizeof(libgamma_x_randr_partition_data_t))) == NULL) - 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 == NULL) && (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 == NULL) && (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) */ - if ((data->crtc_to_output = malloc((size_t)(reply->num_crtcs) * sizeof(size_t))) == NULL) - 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++) - { - xcb_randr_get_output_info_cookie_t out_cookie; - xcb_randr_get_output_info_reply_t* out_reply; - uint16_t j; - - /* 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 != NULL) - { - fail_rc = translate_error(error->error_code, LIBGAMMA_OUTPUT_INFORMATION_QUERY_FAILED, 0); - goto fail; + 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; } - - /* 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 != NULL) - { - free(data->crtcs); - free(data->outputs); - free(data->crtc_to_output); - free(data); - } - free(reply); - return fail_rc; + + /* 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. + * Release all resources held by a partition state * - * @param this The partition state. + * @param this The partition state */ -void libgamma_x_randr_partition_destroy(libgamma_partition_state_t* restrict this) +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); + 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. + * Restore the gamma ramps all CRTC:s with a partition to the system settings * - * @param this The partition state. + * @param this The partition state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_x_randr_partition_restore(libgamma_partition_state_t* restrict this) +int +libgamma_x_randr_partition_restore(libgamma_partition_state_t *restrict this) { - (void) this; - return errno = ENOTSUP, LIBGAMMA_ERRNO_SET; + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; } /** - * Initialise an allocated CRTC state. + * Initialise an allocated CRTC state * - * @param this The CRTC state to initialise. + * @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. + * @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. + * 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) +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 ? LIBGAMMA_NO_SUCH_CRTC : 0; + 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. + * Release all resources held by a CRTC state * - * @param this The CRTC state. + * @param this The CRTC state */ -void libgamma_x_randr_crtc_destroy(libgamma_crtc_state_t* restrict this) +void +libgamma_x_randr_crtc_destroy(libgamma_crtc_state_t *restrict this) { - (void) this; + (void) this; } /** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC. + * Restore the gamma ramps for a CRTC to the system settings for that CRTC * - * @param this The CRTC state. + * @param this The CRTC state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_x_randr_crtc_restore(libgamma_crtc_state_t* restrict this) +int +libgamma_x_randr_crtc_restore(libgamma_crtc_state_t *restrict this) { - (void) this; - return errno = ENOTSUP, LIBGAMMA_ERRNO_SET; + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; } /** - * Get the gamma ramp size of a CRTC. + * 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. + * @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) +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 != NULL) - return out->gamma_size_error = translate_error(error->error_code, LIBGAMMA_GAMMA_RAMPS_SIZE_QUERY_FAILED, 1); - /* 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; + 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. + * 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. + * @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) +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); +#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: - out->subpixel_order_error = LIBGAMMA_SUBPIXEL_ORDER_NOT_RECOGNISED; - break; + /* 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; } - 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) ? -1 : 0; - } - + #undef __select } /** - * Determine the connector type from the connector name. + * Determine the connector type from the connector name * - * @param this The CRTC information to use and extend. - * @param Non-zero on error. + * @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) +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) \ - if (strstr(this->connector_name, name "-") == this->connector_name) \ - return this->connector_type = LIBGAMMA_CONNECTOR_TYPE_##type, 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); - + /* 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; + + /* 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. + * 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. + * @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) +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 == NULL) - 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 == NULL) - return out->connector_name_error = errno, -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; + 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. + * 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. + * @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) +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; - - /* 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 != NULL) - 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 == NULL) - return free(prop_reply), out->edid_error = LIBGAMMA_REPLY_VALUE_EXTRACTION_FAILED; - - /* For each property */ - for (; atoms != atoms_end; atoms++) - { - 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 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 == NULL) || /* (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; + 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; } - - /* 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 == NULL) || (length < 1)) - { - free(atom_reply); - free(atom_name_reply); - 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; } - - /* Store the EDID. */ - out->edid_length = (size_t)length; - out->edid = malloc((size_t)length * sizeof(unsigned char)); - if (out->edid == NULL) - 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; + + return out->edid_error = LIBGAMMA_EDID_NOT_FOUND; } /** - * Read information about a CRTC. + * 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`.. + * @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) +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; - - /* 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) == 0; - - /* We need to free the output's name after us if it is not explicitly requested. */ - free_name = (fields & LIBGAMMA_CRTC_INFO_CONNECTOR_NAME) == 0; - - /* Jump if the output information is not required. */ - if ((fields & (LIBGAMMA_CRTC_INFO_MACRO_ACTIVE | LIBGAMMA_CRTC_INFO_MACRO_CONNECTOR)) == 0) - goto cont; - - /* Get connector and connector information. */ - { - xcb_connection_t* restrict connection = crtc->partition->site->data; - libgamma_x_randr_partition_data_t* restrict screen_data = crtc->partition->data; - size_t output_index = screen_data->crtc_to_output[crtc->crtc]; - xcb_randr_get_output_info_cookie_t cookie; - xcb_generic_error_t* error; - /* `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 != NULL) - { - 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) == 0) - goto cont; - /* If there is not monitor that report error in EDID related fields. */ - if (this->active == 0) - { - 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 == NULL) - { - 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; +#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. + * 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. + * @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. + * 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) +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; + 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; + /* 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 != NULL) - 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; + + /* 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. + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * 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) +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; + 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; + /* 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. */ - if ((error = xcb_request_check(connection, cookie)) != NULL) - return translate_error(error->error_code, LIBGAMMA_GAMMA_RAMP_WRITE_FAILED, 0); - return 0; + + /* 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 __GCC__ +#ifdef __GNUC__ # pragma GCC diagnostic pop #endif - diff --git a/src/lib/gamma-x-randr.h b/src/lib/gamma-x-randr.h index 7d03436..59c64bc 100644 --- a/src/lib/gamma-x-randr.h +++ b/src/lib/gamma-x-randr.h @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #ifndef LIBGAMMA_GAMMA_X_RANDR_H #define LIBGAMMA_GAMMA_X_RANDR_H @@ -27,136 +11,129 @@ /** - * Return the capabilities of the adjustment method. + * Return the capabilities of the adjustment method * - * @param this The data structure to fill with the method's capabilities. + * @param this The data structure to fill with the method's capabilities */ -void libgamma_x_randr_method_capabilities(libgamma_method_capabilities_t* restrict this); +void libgamma_x_randr_method_capabilities(libgamma_method_capabilities_t *restrict); /** - * Initialise an allocated site state. + * Initialise an allocated site state * - * @param this The site state to initialise. + * @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. + * not be free:d if this function fails * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_x_randr_site_initialise(libgamma_site_state_t* restrict this, - char* restrict site); +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 this); +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. + * Restore the gamma ramps all CRTC:s with a site to the system settings * - * @param this The site state. + * @param this The site state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_x_randr_site_restore(libgamma_site_state_t* restrict this); +int libgamma_x_randr_site_restore(libgamma_site_state_t *restrict); /** - * Initialise an allocated partition state. + * 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. + * @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. + * 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 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. + * Release all resources held by a partition state * - * @param this The partition state. + * @param this The partition state */ -void libgamma_x_randr_partition_destroy(libgamma_partition_state_t* restrict this); +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. + * Restore the gamma ramps all CRTC:s with a partition to the system settings * - * @param this The partition state. + * @param this The partition state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_x_randr_partition_restore(libgamma_partition_state_t* restrict this); +int libgamma_x_randr_partition_restore(libgamma_partition_state_t *restrict); /** - * Initialise an allocated CRTC state. + * 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. + * @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. + * 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); +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. + * Release all resources held by a CRTC state * - * @param this The CRTC state. + * @param this The CRTC state */ -void libgamma_x_randr_crtc_destroy(libgamma_crtc_state_t* restrict this) __attribute__((const)); +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. + * Restore the gamma ramps for a CRTC to the system settings for that CRTC * - * @param this The CRTC state. + * @param this The CRTC state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_x_randr_crtc_restore(libgamma_crtc_state_t* restrict this); +int libgamma_x_randr_crtc_restore(libgamma_crtc_state_t *restrict); /** - * Read information about a CRTC. + * 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`. + * @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); +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. + * 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. + * @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. + * 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); +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. + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * 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); +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 index 13cc740..bae9d31 100644 --- a/src/lib/gamma-x-vidmode.c +++ b/src/lib/gamma-x-vidmode.c @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* 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 @@ -31,44 +15,44 @@ /** - * Return the capabilities of the adjustment method. + * Return the capabilities of the adjustment method * - * @param this The data structure to fill with the method's capabilities. + * @param this The data structure to fill with the method's capabilities */ -void libgamma_x_vidmode_method_capabilities(libgamma_method_capabilities_t* restrict this) +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; + 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. + * Initialise an allocated site state * - * @param this The site state to initialise. + * @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 @@ -76,237 +60,253 @@ void libgamma_x_vidmode_method_capabilities(libgamma_method_capabilities_t* rest * 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. + * error identifier provided by this library */ -int libgamma_x_vidmode_site_initialise(libgamma_site_state_t* restrict this, - char* restrict site) +int +libgamma_x_vidmode_site_initialise(libgamma_site_state_t *restrict this, char *restrict site) { - /* Connect to the display. */ - Display* restrict connection = XOpenDisplay(site); - int _major, _minor, screens; - if ((this->data = connection) == NULL) - return LIBGAMMA_OPEN_SITE_FAILED; - /* Query X VidMode extension protocol version. */ - if (!XF86VidModeQueryVersion(connection, &_major, &_minor)) - return XCloseDisplay(connection), LIBGAMMA_PROTOCOL_VERSION_QUERY_FAILED; - /* Query the number of available screens. */ - if ((screens = ScreenCount(connection)) < 0) - return XCloseDisplay(connection), LIBGAMMA_NEGATIVE_PARTITION_COUNT; - this->partitions_available = (size_t)screens; - return 0; + /* 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. + * Release all resources held by a site state * - * @param this The site state. + * @param this The site state */ -void libgamma_x_vidmode_site_destroy(libgamma_site_state_t* restrict this) +void +libgamma_x_vidmode_site_destroy(libgamma_site_state_t *restrict this) { - XCloseDisplay((Display*)(this->data)); + XCloseDisplay((Display *)this->data); } /** - * Restore the gamma ramps all CRTC:s with a site to the system settings. + * Restore the gamma ramps all CRTC:s with a site to the system settings * - * @param this The site state. + * @param this The site state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_x_vidmode_site_restore(libgamma_site_state_t* restrict this) +int +libgamma_x_vidmode_site_restore(libgamma_site_state_t *restrict this) { - (void) this; - return errno = ENOTSUP, LIBGAMMA_ERRNO_SET; + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; } /** - * Initialise an allocated partition state. + * 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. + * @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. + * 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) +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 ? LIBGAMMA_NO_SUCH_PARTITION : 0; + this->crtcs_available = 1; + return partition < site->partitions_available ? 0 : LIBGAMMA_NO_SUCH_PARTITION; } /** - * Release all resources held by a partition state. + * Release all resources held by a partition state * - * @param this The partition state. + * @param this The partition state */ -void libgamma_x_vidmode_partition_destroy(libgamma_partition_state_t* restrict this) +void +libgamma_x_vidmode_partition_destroy(libgamma_partition_state_t *restrict this) { - (void) this; + (void) this; } /** - * Restore the gamma ramps all CRTC:s with a partition to the system settings. + * Restore the gamma ramps all CRTC:s with a partition to the system settings * - * @param this The partition state. + * @param this The partition state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_x_vidmode_partition_restore(libgamma_partition_state_t* restrict this) +int +libgamma_x_vidmode_partition_restore(libgamma_partition_state_t *restrict this) { - (void) this; - return errno = ENOTSUP, LIBGAMMA_ERRNO_SET; + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; } /** - * Initialise an allocated CRTC state. + * 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. + * @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. + * 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) +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 ? 0 : LIBGAMMA_NO_SUCH_CRTC; + (void) this; + (void) partition; + return !crtc ? 0 : LIBGAMMA_NO_SUCH_CRTC; } /** - * Release all resources held by a CRTC state. + * Release all resources held by a CRTC state * - * @param this The CRTC state. + * @param this The CRTC state */ -void libgamma_x_vidmode_crtc_destroy(libgamma_crtc_state_t* restrict this) +void +libgamma_x_vidmode_crtc_destroy(libgamma_crtc_state_t *restrict this) { - (void) this; + (void) this; } /** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC. + * Restore the gamma ramps for a CRTC to the system settings for that CRTC * - * @param this The CRTC state. + * @param this The CRTC state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_x_vidmode_crtc_restore(libgamma_crtc_state_t* restrict this) +int +libgamma_x_vidmode_crtc_restore(libgamma_crtc_state_t *restrict this) { - (void) this; - return errno = ENOTSUP, LIBGAMMA_ERRNO_SET; + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; } /** - * Read information about a CRTC. + * 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 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`. + * @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) +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) - - /* 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)) - { - Display* restrict connection = crtc->partition->site->data; - int stops = 0; - 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; - + + 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. + * 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. + * @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. + * 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) +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; + /* 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; + /* 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. + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * 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) +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; + /* 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; + /* 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 index 510f03b..618d5c4 100644 --- a/src/lib/gamma-x-vidmode.h +++ b/src/lib/gamma-x-vidmode.h @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #ifndef LIBGAMMA_GAMMA_X_VIDMODE_H #define LIBGAMMA_GAMMA_X_VIDMODE_H @@ -27,16 +11,16 @@ /** - * Return the capabilities of the adjustment method. + * Return the capabilities of the adjustment method * - * @param this The data structure to fill with the method's capabilities. + * @param this The data structure to fill with the method's capabilities */ -void libgamma_x_vidmode_method_capabilities(libgamma_method_capabilities_t* restrict this); +void libgamma_x_vidmode_method_capabilities(libgamma_method_capabilities_t *restrict); /** - * Initialise an allocated site state. + * Initialise an allocated site state * - * @param this The site state to initialise. + * @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 @@ -44,119 +28,113 @@ void libgamma_x_vidmode_method_capabilities(libgamma_method_capabilities_t* rest * 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. + * error identifier provided by this library */ -int libgamma_x_vidmode_site_initialise(libgamma_site_state_t* restrict this, - char* restrict site); +int libgamma_x_vidmode_site_initialise(libgamma_site_state_t *restrict, char *restrict); /** - * Release all resources held by a site state. + * Release all resources held by a site state * - * @param this The site state. + * @param this The site state */ -void libgamma_x_vidmode_site_destroy(libgamma_site_state_t* restrict this); +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. + * Restore the gamma ramps all CRTC:s with a site to the system settings * - * @param this The site state. + * @param this The site state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_x_vidmode_site_restore(libgamma_site_state_t* restrict this); +int libgamma_x_vidmode_site_restore(libgamma_site_state_t *restrict); /** - * Initialise an allocated partition state. + * 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. + * @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. + * 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); +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. + * Release all resources held by a partition state * - * @param this The partition state. + * @param this The partition state */ -void libgamma_x_vidmode_partition_destroy(libgamma_partition_state_t* restrict this) __attribute__((const)); +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. + * Restore the gamma ramps all CRTC:s with a partition to the system settings * - * @param this The partition state. + * @param this The partition state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_x_vidmode_partition_restore(libgamma_partition_state_t* restrict this); +int libgamma_x_vidmode_partition_restore(libgamma_partition_state_t *restrict); /** - * Initialise an allocated CRTC state. + * 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. + * @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. + * 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) __attribute__((const)); +__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. + * Release all resources held by a CRTC state * - * @param this The CRTC state. + * @param this The CRTC state */ -void libgamma_x_vidmode_crtc_destroy(libgamma_crtc_state_t* restrict this) __attribute__((const)); +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. + * Restore the gamma ramps for a CRTC to the system settings for that CRTC * - * @param this The CRTC state. + * @param this The CRTC state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_x_vidmode_crtc_restore(libgamma_crtc_state_t* restrict this); +int libgamma_x_vidmode_crtc_restore(libgamma_crtc_state_t *restrict); /** - * Read information about a CRTC. + * 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`. + * @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 this, - libgamma_crtc_state_t* restrict crtc, int32_t fields); +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. + * 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. + * @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. + * 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); +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. + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * 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); +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 index a606dd7..d43f336 100644 --- a/src/lib/libgamma-error.c.gpp +++ b/src/lib/libgamma-error.c.gpp @@ -1,21 +1,5 @@ /* -*- c -*- */ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #include "libgamma-error.h" @@ -35,17 +19,19 @@ $>export PATH=".:${PATH}" /** * Group that the user needs to be a member of if - * `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned. + * `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned */ #ifndef __WIN32__ __thread gid_t libgamma_group_gid = 0; -gid_t libgamma_group_gid_get(void) +gid_t +libgamma_group_gid_get(void) { - return libgamma_group_gid; + return libgamma_group_gid; } -void libgamma_group_gid_set(gid_t value) +void +libgamma_group_gid_set(gid_t value) { - libgamma_group_gid = value; + libgamma_group_gid = value; } #else short libgamma_group_gid = 0; @@ -55,20 +41,21 @@ short libgamma_group_gid = 0; * 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. + * cannot be determined */ #ifndef __WIN32__ -__thread const char* libgamma_group_name = NULL; -const char* libgamma_group_name_get(void) -{ - return libgamma_group_name; +__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) +void +libgamma_group_name_set(const char *value) { - libgamma_group_name = value; + libgamma_group_name = value; } #else -const char* libgamma_group_name = NULL; +const char *libgamma_group_name = NULL; #endif @@ -87,102 +74,105 @@ const char* libgamma_group_name = NULL; * 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. + * @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) +void +libgamma_perror(const char *name, int error_code) { - 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. */ - const char* error = libgamma_name_of_error(error_code); - long int gid = (long int)libgamma_group_gid; - if (libgamma_group_name == NULL) - /* 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)); + 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. + * Returns the name of the definition associated with a `libgamma` error code * - * @param value The 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. + * `NULL` if the error code does not exist; the return string + * should not be `free`:d */ -const char* libgamma_name_of_error(int value) +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[] = - { + /* 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}", + "${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 `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. + * Return the value of a `libgamma` error definition refered to by name * - * @param name The name of the definition associated with the error code. + * @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. + * or does not refer to a `libgamma` error */ -int libgamma_value_of_error(const char* name) +int +libgamma_value_of_error(const char *name) { - /* Return 0 (not a valid error code) if the error name is `NULL`. */ - if (name == NULL) - return 0; - - /* Test error names against `name` and return the value of the match error. */ + /* 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}; + if (!strcmp(name, "${error}")) + return ${error}; $>done - - /* Return 0 (not a valid error code) if the error name is unknown. */ - return 0; -} + /* 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 index 3a562ee..a026e79 100644 --- a/src/lib/libgamma-error.h +++ b/src/lib/libgamma-error.h @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #ifndef LIBGAMMA_ERROR_H #define LIBGAMMA_ERROR_H @@ -25,14 +9,14 @@ #include <sys/types.h> -#ifndef __GCC__ +#ifndef __GNUC__ # define __attribute__(x) #endif /** * Group that the user needs to be a member of if - * `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned. + * `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned */ #ifndef __WIN32__ extern __thread gid_t libgamma_group_gid; @@ -46,292 +30,295 @@ extern short libgamma_group_gid; * 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. + * 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); +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; +extern const char *libgamma_group_name; #endif /** * `errno` has be set with a standard error number - * to indicate the what has gone wrong. + * to indicate the what has gone wrong */ -#define LIBGAMMA_ERRNO_SET (-1) +#define LIBGAMMA_ERRNO_SET (-1) /** * The selected adjustment method does not exist - * or has been excluded at compile-time. + * or has been excluded at compile-time */ -#define LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD (-2) +#define LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD (-2) /** - * The selected site does not exist. + * The selected site does not exist */ -#define LIBGAMMA_NO_SUCH_SITE (-3) +#define LIBGAMMA_NO_SUCH_SITE (-3) /** - * The selected partition does not exist. + * The selected partition does not exist */ -#define LIBGAMMA_NO_SUCH_PARTITION (-4) +#define LIBGAMMA_NO_SUCH_PARTITION (-4) /** - * The selected CRTC does not exist. + * The selected CRTC does not exist */ -#define LIBGAMMA_NO_SUCH_CRTC (-5) +#define LIBGAMMA_NO_SUCH_CRTC (-5) /** * Counter overflowed when counting the number - * of available items. + * of available items */ -#define LIBGAMMA_IMPOSSIBLE_AMOUNT (-6) +#define LIBGAMMA_IMPOSSIBLE_AMOUNT (-6) /** * The selected connector is disabled, it does - * not have a CRTC. + * not have a CRTC */ -#define LIBGAMMA_CONNECTOR_DISABLED (-7) +#define LIBGAMMA_CONNECTOR_DISABLED (-7) /** * The selected CRTC could not be opened, - * reason unknown. + * reason unknown */ -#define LIBGAMMA_OPEN_CRTC_FAILED (-8) +#define LIBGAMMA_OPEN_CRTC_FAILED (-8) /** * The CRTC information field is not supported - * by the adjustment method. + * by the adjustment method */ -#define LIBGAMMA_CRTC_INFO_NOT_SUPPORTED (-9) +#define LIBGAMMA_CRTC_INFO_NOT_SUPPORTED (-9) /** * Failed to read the current gamma ramps for - * the selected CRTC, reason unknown. + * the selected CRTC, reason unknown */ -#define LIBGAMMA_GAMMA_RAMP_READ_FAILED (-10) +#define LIBGAMMA_GAMMA_RAMP_READ_FAILED (-10) /** * Failed to write the current gamma ramps for - * the selected CRTC, reason unknown. + * the selected CRTC, reason unknown */ -#define LIBGAMMA_GAMMA_RAMP_WRITE_FAILED (-11) +#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. + * in response to the query/command */ -#define LIBGAMMA_GAMMA_RAMP_SIZE_CHANGED (-12) +#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.) + * which is required by the adjustment method + * + * (Only returned in debug mode) */ -#define LIBGAMMA_MIXED_GAMMA_RAMP_SIZE (-13) +#define LIBGAMMA_MIXED_GAMMA_RAMP_SIZE (-13) /** * The specified ramp sizes are not supported - * by the adjustment method. - * (Only returned in debug mode.) + * by the adjustment method + * + * (Only returned in debug mode) */ -#define LIBGAMMA_WRONG_GAMMA_RAMP_SIZE (-14) +#define LIBGAMMA_WRONG_GAMMA_RAMP_SIZE (-14) /** * The adjustment method reported that the gamma - * ramps size is 1, or perhaps even zero or negative. + * ramps size is 1, or perhaps even zero or negative */ -#define LIBGAMMA_SINGLETON_GAMMA_RAMP (-15) +#define LIBGAMMA_SINGLETON_GAMMA_RAMP (-15) /** * The adjustment method failed to list - * available CRTC:s, reason unknown. + * available CRTC:s, reason unknown */ -#define LIBGAMMA_LIST_CRTCS_FAILED (-16) +#define LIBGAMMA_LIST_CRTCS_FAILED (-16) /** * Failed to acquire mode resources from the - * adjustment method. + * adjustment method */ -#define LIBGAMMA_ACQUIRING_MODE_RESOURCES_FAILED (-17) +#define LIBGAMMA_ACQUIRING_MODE_RESOURCES_FAILED (-17) /** * The adjustment method reported that a negative - * number of partitions exists in the site. + * number of partitions exists in the site */ -#define LIBGAMMA_NEGATIVE_PARTITION_COUNT (-18) +#define LIBGAMMA_NEGATIVE_PARTITION_COUNT (-18) /** * The adjustment method reported that a negative - * number of CRTC:s exists in the partition. + * number of CRTC:s exists in the partition */ -#define LIBGAMMA_NEGATIVE_CRTC_COUNT (-19) +#define LIBGAMMA_NEGATIVE_CRTC_COUNT (-19) /** * Device cannot be access becauses of - * insufficient permissions. + * insufficient permissions */ -#define LIBGAMMA_DEVICE_RESTRICTED (-20) +#define LIBGAMMA_DEVICE_RESTRICTED (-20) /** - * Device cannot be access, reason unknown. + * Device cannot be access, reason unknown */ -#define LIBGAMMA_DEVICE_ACCESS_FAILED (-21) +#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. + * to tell why)) is required */ -#define LIBGAMMA_DEVICE_REQUIRE_GROUP (-22) +#define LIBGAMMA_DEVICE_REQUIRE_GROUP (-22) /** - * The graphics card appear to have been removed. + * The graphics card appear to have been removed */ -#define LIBGAMMA_GRAPHICS_CARD_REMOVED (-23) +#define LIBGAMMA_GRAPHICS_CARD_REMOVED (-23) /** - * The state of the requested information is unknown. + * The state of the requested information is unknown */ -#define LIBGAMMA_STATE_UNKNOWN (-24) +#define LIBGAMMA_STATE_UNKNOWN (-24) /** * Failed to determine which connector the - * CRTC belongs to. + * CRTC belongs to */ -#define LIBGAMMA_CONNECTOR_UNKNOWN (-25) +#define LIBGAMMA_CONNECTOR_UNKNOWN (-25) /** * The detected connector type is not listed - * in this library and has to be updated. + * in this library and has to be updated */ -#define LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED (-26) +#define LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED (-26) /** * The detected subpixel order is not listed - * in this library and has to be updated. + * in this library and has to be updated */ -#define LIBGAMMA_SUBPIXEL_ORDER_NOT_RECOGNISED (-27) +#define LIBGAMMA_SUBPIXEL_ORDER_NOT_RECOGNISED (-27) /** * The length of the EDID does not match that - * of any supported EDID structure revision. + * of any supported EDID structure revision */ -#define LIBGAMMA_EDID_LENGTH_UNSUPPORTED (-28) +#define LIBGAMMA_EDID_LENGTH_UNSUPPORTED (-28) /** * The magic number in the EDID does not match - * that of any supported EDID structure revision. + * that of any supported EDID structure revision */ -#define LIBGAMMA_EDID_WRONG_MAGIC_NUMBER (-29) +#define LIBGAMMA_EDID_WRONG_MAGIC_NUMBER (-29) /** * The EDID structure revision used by the - * monitor is not supported. + * monitor is not supported */ -#define LIBGAMMA_EDID_REVISION_UNSUPPORTED (-30) +#define LIBGAMMA_EDID_REVISION_UNSUPPORTED (-30) /** * The gamma characteristics field in the EDID - * is left unspecified. - * (This could be considered a non-error.) + * is left unspecified + * + * (This could be considered a non-error) */ -#define LIBGAMMA_GAMMA_NOT_SPECIFIED (-31) +#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. + * by you cannot count on it */ -#define LIBGAMMA_EDID_CHECKSUM_ERROR (-32) +#define LIBGAMMA_EDID_CHECKSUM_ERROR (-32) /** * Both of the errors `LIBGAMMA_GAMMA_NOT_SPECIFIED` - * and `LIBGAMMA_EDID_CHECKSUM_ERROR` have occurred. + * and `LIBGAMMA_EDID_CHECKSUM_ERROR` have occurred */ -#define LIBGAMMA_GAMMA_NOT_SPECIFIED_AND_EDID_CHECKSUM_ERROR (-33) +#define LIBGAMMA_GAMMA_NOT_SPECIFIED_AND_EDID_CHECKSUM_ERROR (-33) /** * Failed to query the gamma ramps size from the - * adjustment method, reason unknown. + * adjustment method, reason unknown */ -#define LIBGAMMA_GAMMA_RAMPS_SIZE_QUERY_FAILED (-34) +#define LIBGAMMA_GAMMA_RAMPS_SIZE_QUERY_FAILED (-34) /** * The selected partition could not be opened, - * reason unknown. + * reason unknown */ -#define LIBGAMMA_OPEN_PARTITION_FAILED (-35) +#define LIBGAMMA_OPEN_PARTITION_FAILED (-35) /** * The selected site could not be opened, - * reason unknown. + * reason unknown */ -#define LIBGAMMA_OPEN_SITE_FAILED (-36) +#define LIBGAMMA_OPEN_SITE_FAILED (-36) /** * Failed to query the adjustment method for - * its protocol version, reason unknown. + * its protocol version, reason unknown */ -#define LIBGAMMA_PROTOCOL_VERSION_QUERY_FAILED (-37) +#define LIBGAMMA_PROTOCOL_VERSION_QUERY_FAILED (-37) /** * The adjustment method's version of its - * protocol is not supported. + * protocol is not supported */ -#define LIBGAMMA_PROTOCOL_VERSION_NOT_SUPPORTED (-38) +#define LIBGAMMA_PROTOCOL_VERSION_NOT_SUPPORTED (-38) /** * The adjustment method failed to list - * available partitions, reason unknown. + * available partitions, reason unknown */ -#define LIBGAMMA_LIST_PARTITIONS_FAILED (-39) +#define LIBGAMMA_LIST_PARTITIONS_FAILED (-39) /** * Partition exists by index, but the partition - * at that index does not exist. + * at that index does not exist */ -#define LIBGAMMA_NULL_PARTITION (-40) +#define LIBGAMMA_NULL_PARTITION (-40) /** * There is not monitor connected to the - * connector of the selected CRTC. + * connector of the selected CRTC */ -#define LIBGAMMA_NOT_CONNECTED (-41) +#define LIBGAMMA_NOT_CONNECTED (-41) /** * Data extraction from a reply from the - * adjustment method failed, reason unknown. + * adjustment method failed, reason unknown */ -#define LIBGAMMA_REPLY_VALUE_EXTRACTION_FAILED (-42) +#define LIBGAMMA_REPLY_VALUE_EXTRACTION_FAILED (-42) /** - * No EDID property was found on the output. + * No EDID property was found on the output */ -#define LIBGAMMA_EDID_NOT_FOUND (-43) +#define LIBGAMMA_EDID_NOT_FOUND (-43) /** * Failed to list properties on the output, - * reason unknown. + * reason unknown */ -#define LIBGAMMA_LIST_PROPERTIES_FAILED (-44) +#define LIBGAMMA_LIST_PROPERTIES_FAILED (-44) /** * Failed to query a property's value from - * the output, reason unknown. + * the output, reason unknown */ -#define LIBGAMMA_PROPERTY_VALUE_QUERY_FAILED (-45) +#define LIBGAMMA_PROPERTY_VALUE_QUERY_FAILED (-45) /** * A request for information on an output - * failed, reason unknown. + * failed, reason unknown */ -#define LIBGAMMA_OUTPUT_INFORMATION_QUERY_FAILED (-46) +#define LIBGAMMA_OUTPUT_INFORMATION_QUERY_FAILED (-46) @@ -341,7 +328,7 @@ extern const char* libgamma_group_name; * number your program thinks it should be sould * update your program for new errors. */ -#define LIBGAMMA_ERROR_MIN (-46) +#define LIBGAMMA_ERROR_MIN (-46) @@ -359,35 +346,34 @@ extern const char* libgamma_group_name; * 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. + * @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* name, int error_code); +void libgamma_perror(const char *, int); /** - * Returns the name of the definition associated with a `libgamma` error code. + * Returns the name of the definition associated with a `libgamma` error code * - * @param value The 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) __attribute__((const)); +const char *libgamma_name_of_error(int) __attribute__((const)); /** - * Return the value of a `libgamma` error definition refered to by name. + * Return the value of a `libgamma` error definition refered to by name * - * @param name The name of the definition associated with the error code. + * @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. + * or does not refer to a `libgamma` error */ -int libgamma_value_of_error(const char* name) __attribute__((const)); +int libgamma_value_of_error(const char *) __attribute__((const)); -#ifndef __GCC__ +#ifndef __GNUC__ # undef __attribute__ #endif #endif - diff --git a/src/lib/libgamma-facade.c.gpp b/src/lib/libgamma-facade.c.gpp index 1dfa99b..9f90ed6 100644 --- a/src/lib/libgamma-facade.c.gpp +++ b/src/lib/libgamma-facade.c.gpp @@ -1,21 +1,5 @@ /* -*- c -*- */ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #include "libgamma-facade.h" #include "libgamma-error.h" @@ -23,22 +7,23 @@ #include "gamma-helper.h" -/* Initialise the general preprocessor. */ +/* Initialise the general preprocessor */ $>cd src/extract $>export PATH=".:${PATH}" -/* Some general preprocessor we will use frequently. */ +/* Some general preprocessor we will use frequently */ $< get-methods () -{ ./libgamma-method-extract --list --method | cut -d _ -f 1,2 --complement +{ + ./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 +{ + echo "$*" | sed -e y/QWERTYUIOPASDFGHJKLZXCVBNM/qwertyuiopasdfghjklzxcvbnm/ | sed -e s:core_graphics:cg:g } $> -/* Include all adjustment methods that - are enabled at compile-time. */ +/* 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" @@ -57,10 +42,10 @@ $>done /* Some things to reduce warnings when we do - not have any adjustment methods enabled. */ + * not have any adjustment methods enabled */ #ifndef HAVE_LIBGAMMA_METHODS # define HAVE_NO_LIBGAMMA_METHODS -# ifdef __GCC__ +# ifdef __GNUC__ # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wsuggest-attribute=const" # endif @@ -71,282 +56,286 @@ $>done #ifdef HAVE_LIBGAMMA_METHODS # ifdef HAVE_LIBGAMMA_METHOD_LINUX_DRM /** - * Test whether a file descriptor refers to a VT. + * Test whether a file descriptor refers to a VT * - * @param fd The file descriptor. - * @return Whether the 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) +static int +libgamma_is_vt_proper(int fd) { - char buf[32]; - char 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'); + 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. + * Test the availability of an adjustment method * - * @param method The 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. + * 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) +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 == 0) - return 0; + 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); + 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; + 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 == 0); - - case 3: - /* All real methods. */ - return caps.real; - - default: - /* All methods. */ - return 1; - } + 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. + * 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 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. + * 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. + * 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) +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; + (void) methods; + (void) buf_size; + (void) operation; + return 0; #else - size_t n = 0; - + 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}; + if (libgamma_list_method_test(LIBGAMMA_METHOD_${method}, operation) && n++ < buf_size) + methods[n - 1] = LIBGAMMA_METHOD_${method}; #endif $>done - - return n; + + 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. + * 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. + * @param method The adjustment method + * @return Whether the adjustment method is available */ -int libgamma_is_method_available(int method) +int +libgamma_is_method_available(int method) { #ifdef HAVE_NO_LIBGAMMA_METHODS - (void) method; - return 0; + (void) method; + return 0; #else - switch (method) - { + switch (method) { $>for method in $(get-methods); do #ifdef HAVE_LIBGAMMA_METHOD_${method} - case LIBGAMMA_METHOD_${method}: + case LIBGAMMA_METHOD_${method}: #endif $>done - return 1; - - default: - return 0; - } + return 1; + + default: + return 0; + } #endif } /** - * Call the adjustment method's implementation of the called function. + * 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 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. + * this is expended into without the libgamma namespace prefix + * @param * The function's parameters */ $<switch () $>{ - /* Read out macro's parameters. */ + /* Read out macro's parameters */ $<method="${1//./->}" ctrl=$2 fun=$3 shift 3 params="$*" $>params="${params// /, }" - - switch (${method}) - { + + 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. */ -$>[ $ctrl = return ] && + 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}); -$>[ ! $ctrl = return ] && +$> 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. */ + 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; + return LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD; $>else - /* Method does not exists/excluded at compile-time. - We will assume that this is not done... */ - break; + /* 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. + * 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.) + * @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) +void +libgamma_method_capabilities(libgamma_method_capabilities_t *restrict this, int method) { - memset(this, 0, sizeof(libgamma_method_capabilities_t)); + memset(this, 0, sizeof(libgamma_method_capabilities_t)); $>switch method break method_capabilities this } /** - * Return the default site for an adjustment method. + * Return the default site for an adjustment method * - * @param method The adjustment method (display server and protocol.) + * @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. + * method; this value should not be `free`:d */ -char* libgamma_method_default_site(int method) +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 == NULL) - return NULL; - - /* Read the variable. */ - env = getenv(var); - /* Return `NULL` if it does not exist (or is empty). */ - if ((env == NULL) || (*env == '\0')) - return NULL; - - /* Return the variable's value. */ - return env; + 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. + * the default site for an adjustment method * - * @param method The adjustment method (display server and protocol.) + * @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) +const char * +libgamma_method_default_site_variable(int method) { - switch (method) - { + switch (method) { #ifdef HAVE_LIBGAMMA_METHOD_X_RANDR - case LIBGAMMA_METHOD_X_RANDR: - return "DISPLAY"; + case LIBGAMMA_METHOD_X_RANDR: + return "DISPLAY"; #endif #ifdef HAVE_LIBGAMMA_METHOD_X_VIDMODE - case LIBGAMMA_METHOD_X_VIDMODE: - return "DISPLAY"; + case LIBGAMMA_METHOD_X_VIDMODE: + return "DISPLAY"; #endif - default: - return NULL; - } + default: + return NULL; + } } /** - * Initialise an allocated site state. + * Initialise an allocated site state * - * @param this The site state to initialise. - * @param method The adjustment method (display server and protocol.) + * @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 @@ -354,50 +343,53 @@ const char* libgamma_method_default_site_variable(int method) * 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. + * error identifier provided by this library */ -int libgamma_site_initialise(libgamma_site_state_t* restrict this, - int method, char* restrict site) +int +libgamma_site_initialise(libgamma_site_state_t *restrict this, int method, char *restrict site) { - this->method = method; - this->site = site; + this->method = method; + this->site = site; $>switch method return site_initialise this site } /** - * Release all resources held by a site state. + * Release all resources held by a site state * - * @param this The site state. + * @param this The site state */ -void libgamma_site_destroy(libgamma_site_state_t* restrict this) +void +libgamma_site_destroy(libgamma_site_state_t *restrict this) { $>switch this.method break site_destroy this - free(this->site); + free(this->site); } /** * Release all resources held by a site state - * and free the site state pointer. + * and free the site state pointer * - * @param this The site state. + * @param this The site state */ -void libgamma_site_free(libgamma_site_state_t* restrict this) +void +libgamma_site_free(libgamma_site_state_t *restrict this) { - libgamma_site_destroy(this); - free(this); + libgamma_site_destroy(this); + free(this); } /** - * Restore the gamma ramps all CRTC:s with a site to the system settings. + * Restore the gamma ramps all CRTC:s with a site to the system settings * - * @param this The site state. + * @param this The site state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_site_restore(libgamma_site_state_t* restrict this) +int +libgamma_site_restore(libgamma_site_state_t *restrict this) { $>switch this.method return site_restore this } @@ -405,29 +397,30 @@ $>switch this.method return site_restore this /** - * Initialise an allocated partition state. + * 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. + * @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. + * 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) +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; + this->site = site; + this->partition = partition; $>switch site.method return partition_initialise this site partition } /** - * Release all resources held by a partition state. + * Release all resources held by a partition state * - * @param this The partition state. + * @param this The partition state */ -void libgamma_partition_destroy(libgamma_partition_state_t* restrict this) +void +libgamma_partition_destroy(libgamma_partition_state_t *restrict this) { $>switch this.site.method break partition_destroy this } @@ -435,25 +428,27 @@ $>switch this.site.method break partition_destroy this /** * Release all resources held by a partition state - * and free the partition state pointer. + * and free the partition state pointer * - * @param this The partition state. + * @param this The partition state */ -void libgamma_partition_free(libgamma_partition_state_t* restrict this) +void +libgamma_partition_free(libgamma_partition_state_t *restrict this) { - libgamma_partition_destroy(this); - free(this); + libgamma_partition_destroy(this); + free(this); } /** - * Restore the gamma ramps all CRTC:s with a partition to the system settings. + * Restore the gamma ramps all CRTC:s with a partition to the system settings * - * @param this The partition state. + * @param this The partition state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_partition_restore(libgamma_partition_state_t* restrict this) +int +libgamma_partition_restore(libgamma_partition_state_t *restrict this) { $>switch this.site.method return partition_restore this } @@ -461,29 +456,30 @@ $>switch this.site.method return partition_restore this /** - * Initialise an allocated CRTC state. + * 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. + * @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. + * 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) +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; + this->partition = partition; + this->crtc = crtc; $>switch partition.site.method return crtc_initialise this partition crtc } /** - * Release all resources held by a CRTC state. + * Release all resources held by a CRTC state * - * @param this The CRTC state. + * @param this The CRTC state */ -void libgamma_crtc_destroy(libgamma_crtc_state_t* restrict this) +void +libgamma_crtc_destroy(libgamma_crtc_state_t *restrict this) { $>switch this.partition.site.method break crtc_destroy this } @@ -491,25 +487,27 @@ $>switch this.partition.site.method break crtc_destroy this /** * Release all resources held by a CRTC state - * and free the CRTC state pointer. + * and free the CRTC state pointer * - * @param this The CRTC state. + * @param this The CRTC state */ -void libgamma_crtc_free(libgamma_crtc_state_t* restrict this) +void +libgamma_crtc_free(libgamma_crtc_state_t *restrict this) { - libgamma_crtc_destroy(this); - free(this); + libgamma_crtc_destroy(this); + free(this); } /** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC. + * Restore the gamma ramps for a CRTC to the system settings for that CRTC * - * @param this The CRTC state. + * @param this The CRTC state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_crtc_restore(libgamma_crtc_state_t* restrict this) +int +libgamma_crtc_restore(libgamma_crtc_state_t *restrict this) { $>switch this.partition.site.method return crtc_restore this } @@ -517,585 +515,583 @@ $>switch this.partition.site.method return crtc_restore this /** - * Read information about a CRTC. + * 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`. + * @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) +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; + (void) fields; #endif - this->edid = NULL; - this->connector_name = NULL; + 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. + * Release all resources in an information data structure for a CRTC * - * @param this The CRTC information. + * @param this The CRTC information */ -void libgamma_crtc_information_destroy(libgamma_crtc_information_t* restrict this) +void +libgamma_crtc_information_destroy(libgamma_crtc_information_t *restrict this) { - free(this->edid); - free(this->connector_name); + free(this->edid); + free(this->connector_name); } /** * Release all resources in an information data structure for a CRTC - * and free the data structure pointer. + * and free the data structure pointer * - * @param this The CRTC information. + * @param this The CRTC information */ -void libgamma_crtc_information_free(libgamma_crtc_information_t* restrict this) +void +libgamma_crtc_information_free(libgamma_crtc_information_t *restrict this) { - libgamma_crtc_information_destroy(this); - free(this); + libgamma_crtc_information_destroy(this); + free(this); } /** - * Convert a raw representation of an EDID to a hexadecimal representation. + * 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. + * @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 * +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. */ - if ((out = malloc((length * 2 + 1) * sizeof(char))) == NULL) - 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; + 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. + * 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`. + * @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. + * `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. + * 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`. + * @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. + * NULL` on allocation error, `errno` will be set accordingly */ $>behex_edid uppercase 0123456789ABCDEF /** - * Convert an hexadecimal representation of an EDID to a raw representation. + * Convert an hexadecimal representation of an EDID to a raw representation * - * @param edid The EDID in hexadecimal 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. + * 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) +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 n = strlen(edid); - size_t i; - - /* Check that the length of the strings is even, - otherwise it cannot represent bytes. */ - if ((n & 1)) - return errno = EINVAL, NULL; - - /* Allocate memory area for output octet array. */ - if ((out = malloc(n /= 2 * sizeof(unsigned char))) == NULL) - return NULL; - - /* Convert to raw octet array. */ - for (i = 0; i < n; i++) - { - /* Get the next character pair that, it represents an octet.o */ - char a = edid[i * 2 + 0]; - char b = edid[i * 2 + 1]; - - /* Verify that the input is in hexadecimal. */ - if (is_not_hex(a) || is_not_hex(b)) - { - free(out); - return errno = EINVAL, NULL; +#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; } - - /* 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; - + + /* 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. + * 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. + * @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. + * error identifier provided by this library */ -int libgamma_crtc_get_gamma_ramps16(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps16_t* restrict ramps) +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; + (void) ramps; #endif - - switch (this->partition->site->method) - { - /* Methods other than Quartz/CoreGraphics uses 16-bit integers. */ + + 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); + case LIBGAMMA_METHOD_${method}: + return libgamma_$(lowercase $method)_crtc_get_gamma_ramps16(this, ramps); #endif - - /* The Quartz/CoreGraphics method uses single precision float. */ + + /* The Quartz/CoreGraphics method uses single precision float */ $>done #ifdef HAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS - case LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS: - { - libgamma_gamma_ramps_any_t ramps_; - ramps_.bits16 = *ramps; - return libgamma_translated_ramp_get(this, &ramps_, 16, -1, - libgamma_crtc_get_gamma_rampsf); - } + 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; - } + + /* 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. + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * error identifier provided by this library */ -int libgamma_crtc_set_gamma_ramps16(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps16_t ramps) +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; + (void) ramps; #endif - switch (this->partition->site->method) - { - /* Methods other than Quartz/CoreGraphics uses 16-bit integers. */ + 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); + case LIBGAMMA_METHOD_${method}: + return libgamma_$(lowercase $method)_crtc_set_gamma_ramps16(this, ramps); #endif $>done - /* The Quartz/CoreGraphics method uses single precision float. */ + /* The Quartz/CoreGraphics method uses single precision float */ #ifdef HAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS - case LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS: - { - libgamma_gamma_ramps_any_t ramps_; - ramps_.bits16 = ramps; - return libgamma_translated_ramp_set(this, ramps_, 16, -1, - libgamma_crtc_set_gamma_rampsf); - } + 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; - } + + /* 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. + * 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 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. + * 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. + * (`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. + * 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. + * error identifier provided by this library */ $>crtc_set_get_gamma_ramps () $>{ -$< - action=$1 +$<action=$1 ramps=$2 type=$3 bits=$4 p= - [ $action = get ] && 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) +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. */ + 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); + case LIBGAMMA_METHOD_DUMMY: + return libgamma_dummy_crtc_${action}_gamma_${ramps}(this, ramps); #endif - - /* The Quartz/CoreGraphics method uses single precision float. */ + + /* The Quartz/CoreGraphics method uses single precision float */ #ifdef HAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS - case 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); + /* 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); + /* 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); - } + + /* 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. + * 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. + * @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. + * 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. + * Set the gamma ramps for a CRTC, 32-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * 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. + * 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. + * @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. + * 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. + * Set the gamma ramps for a CRTC, 32-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * 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. + * 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. + * @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. + * 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. + * Set the gamma ramps for a CRTC, 64-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * 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. + * 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. + * @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. + * 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. + * Set the gamma ramps for a CRTC, `float` version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * 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. + * 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. + * @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. + * 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. + * Set the gamma ramps for a CRTC, `double` version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * error identifier provided by this library */ $>crtc_set_get_gamma_ramps set rampsd float_double -2 /** - * Set the gamma ramps for a CRTC. + * Set the gamma ramps for a CRTC * - * Note that this will probably involve the library allocating temporary data. + * 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. + * @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. + * 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) +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)) - { - if ((e = info.gamma_size_error) < 0) + 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; - return errno = e, 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 == NULL) - 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. + * Set the gamma ramps for a CRTC, 8-bit gamma-depth function version * - * Note that this will probably involve the library allocating temporary data. + * 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. + * @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. + * 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. + * Set the gamma ramps for a CRTC, 16-bit gamma-depth function version * - * Note that this will probably involve the library allocating temporary data. + * 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. + * @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. + * 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. + * Set the gamma ramps for a CRTC, 32-bit gamma-depth function version * - * Note that this will probably involve the library allocating temporary data. + * 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. + * @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. + * 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. + * Set the gamma ramps for a CRTC, 64-bit gamma-depth function version * - * Note that this will probably involve the library allocating temporary data. + * 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. + * @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. + * error identifier provided by this library */ $>crtc_set_gamma_ramps_f uint64_t ramps64 /** - * Set the gamma ramps for a CRTC, `float` function version. + * Set the gamma ramps for a CRTC, `float` function version * - * Note that this will probably involve the library allocating temporary data. + * 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. + * @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. + * error identifier provided by this library */ $>crtc_set_gamma_ramps_f float rampsf /** - * Set the gamma ramps for a CRTC, `double` function version. + * Set the gamma ramps for a CRTC, `double` function version * - * Note that this will probably involve the library allocating temporary data. + * 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. + * @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. + * error identifier provided by this library */ $>crtc_set_gamma_ramps_f double rampsd #ifdef HAVE_NO_LIBGAMMA_METHODS -# ifdef __GCC__ +# ifdef __GNUC__ # pragma GCC diagnostic pop # endif #endif - diff --git a/src/lib/libgamma-facade.h b/src/lib/libgamma-facade.h index a24c0e0..596094c 100644 --- a/src/lib/libgamma-facade.h +++ b/src/lib/libgamma-facade.h @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #ifndef LIBGAMMA_FACADE_H #define LIBGAMMA_FACADE_H @@ -28,127 +12,127 @@ #include <stddef.h> #include <stdint.h> -#ifndef __GCC__ +#ifndef __GNUC__ # define __attribute__(x) #endif /** - * Mapping function from [0, 1] float encoding value to [0, 2⁸ − 1] integer output value. + * 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. + * @param encoding [0, 1] float encoding value + * @return [0, 2⁸ − 1] integer output value */ -typedef uint8_t libgamma_gamma_ramps8_fun(float encoding); +typedef uint8_t libgamma_gamma_ramps8_fun(float); /** - * Mapping function from [0, 1] float encoding value to [0, 2¹⁶ − 1] integer output value. + * 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. + * @param encoding [0, 1] float encoding value + * @return [0, 2¹⁶ − 1] integer output value */ -typedef uint16_t libgamma_gamma_ramps16_fun(float encoding); +typedef uint16_t libgamma_gamma_ramps16_fun(float); /** - * Mapping function from [0, 1] float encoding value to [0, 2³² − 1] integer output value. + * 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. + * @param encoding [0, 1] float encoding value + * @return [0, 2³² − 1] integer output value */ -typedef uint32_t libgamma_gamma_ramps32_fun(float encoding); +typedef uint32_t libgamma_gamma_ramps32_fun(float); /** - * Mapping function from [0, 1] float encoding value to [0, 2⁶⁴ − 1] integer output value. + * 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. + * @param encoding [0, 1] float encoding value + * @return [0, 2⁶⁴ − 1] integer output value */ -typedef uint64_t libgamma_gamma_ramps64_fun(float encoding); +typedef uint64_t libgamma_gamma_ramps64_fun(float); /** - * Mapping function from [0, 1] float encoding value to [0, 1] float output value. + * 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. + * @param encoding [0, 1] float encoding value + * @return [0, 1] float output value */ -typedef float libgamma_gamma_rampsf_fun(float encoding); +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. + * value to [0, 1] double precision float output value * - * @param encoding [0, 1] float encoding value. - * @return [0, 1] float output value. + * @param encoding [0, 1] float encoding value + * @return [0, 1] float output value */ -typedef double libgamma_gamma_rampsd_fun(double encoding); +typedef double libgamma_gamma_rampsd_fun(double); /** - * List available adjustment methods by their order of preference based on the environment. + * 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 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. + * 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. + * 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. + * have been stored if the buffer was large enough */ -size_t libgamma_list_methods(int* restrict methods, size_t buf_size, int operation); +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. + * 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. + * @param method The adjustment method + * @return Whether the adjustment method is available */ -int libgamma_is_method_available(int method) __attribute__((const)); +int libgamma_is_method_available(int) __attribute__((const)); /** - * Return the capabilities of an adjustment method. + * 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). + * @param method The adjustment method (display server and protocol) */ -void libgamma_method_capabilities(libgamma_method_capabilities_t* restrict this, int method); +void libgamma_method_capabilities(libgamma_method_capabilities_t *restrict, int); /** - * Return the default site for an adjustment method. + * Return the default site for an adjustment method * - * @param method The adjustment method (display server and protocol.) + * @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. + * method; this value should not be `free`:d */ -char* libgamma_method_default_site(int method); +char *libgamma_method_default_site(int); /** * Return the default variable that determines - * the default site for an adjustment method. + * the default site for an adjustment method * - * @param method The adjustment method (display server and protocol.) + * @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. + * 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) __attribute__((const)); +const char *libgamma_method_default_site_variable(int) __attribute__((const)); /** - * Initialise an allocated site state. + * Initialise an allocated site state * - * @param this The site state to initialise. - * @param method The adjustment method (display server and protocol.) + * @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 @@ -156,422 +140,398 @@ const char* libgamma_method_default_site_variable(int method) __attribute__((con * 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. + * error identifier provided by this library */ -int libgamma_site_initialise(libgamma_site_state_t* restrict this, - int method, char* restrict site); +int libgamma_site_initialise(libgamma_site_state_t *restrict, int, char *restrict); /** - * Release all resources held by a site state. + * Release all resources held by a site state * - * @param this The site state. + * @param this The site state */ -void libgamma_site_destroy(libgamma_site_state_t* restrict this); +void libgamma_site_destroy(libgamma_site_state_t *restrict); /** * Release all resources held by a site state - * and free the site state pointer. + * and free the site state pointer * - * @param this The site state. + * @param this The site state */ -void libgamma_site_free(libgamma_site_state_t* restrict this); +void libgamma_site_free(libgamma_site_state_t *restrict); /** - * Restore the gamma ramps all CRTC:s within a site to the system settings. + * Restore the gamma ramps all CRTC:s within a site to the system settings * - * @param this The site state. + * @param this The site state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_site_restore(libgamma_site_state_t* restrict this); +int libgamma_site_restore(libgamma_site_state_t *restrict); /** - * Initialise an allocated partition state. + * 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. + * @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. + * 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); +int libgamma_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); /** - * Release all resources held by a partition state. + * Release all resources held by a partition state * - * @param this The partition state. + * @param this The partition state */ -void libgamma_partition_destroy(libgamma_partition_state_t* restrict this); +void libgamma_partition_destroy(libgamma_partition_state_t *restrict); /** * Release all resources held by a partition state - * and free the partition state pointer. + * and free the partition state pointer * - * @param this The partition state. + * @param this The partition state */ -void libgamma_partition_free(libgamma_partition_state_t* restrict this); +void libgamma_partition_free(libgamma_partition_state_t *restrict); /** - * Restore the gamma ramps all CRTC:s within a partition to the system settings. + * Restore the gamma ramps all CRTC:s within a partition to the system settings * - * @param this The partition state. + * @param this The partition state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_partition_restore(libgamma_partition_state_t* restrict this); +int libgamma_partition_restore(libgamma_partition_state_t *restrict); /** - * Initialise an allocated CRTC state. + * 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. + * @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. + * 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); +int libgamma_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); /** - * Release all resources held by a CRTC state. + * Release all resources held by a CRTC state * - * @param this The CRTC state. + * @param this The CRTC state */ -void libgamma_crtc_destroy(libgamma_crtc_state_t* restrict this); +void libgamma_crtc_destroy(libgamma_crtc_state_t *restrict); /** * Release all resources held by a CRTC state - * and free the CRTC state pointer. + * and free the CRTC state pointer * - * @param this The CRTC state. + * @param this The CRTC state */ -void libgamma_crtc_free(libgamma_crtc_state_t* restrict this); +void libgamma_crtc_free(libgamma_crtc_state_t *restrict); /** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC. + * Restore the gamma ramps for a CRTC to the system settings for that CRTC * - * @param this The CRTC state. + * @param this The CRTC state * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. + * error identifier provided by this library */ -int libgamma_crtc_restore(libgamma_crtc_state_t* restrict this); +int libgamma_crtc_restore(libgamma_crtc_state_t *restrict); /** - * Read information about a CRTC. + * 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`. + * @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); +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. + * Release all resources in an information data structure for a CRTC * - * @param this The CRTC information. + * @param this The CRTC information */ -void libgamma_crtc_information_destroy(libgamma_crtc_information_t* restrict this); +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. + * and free the data structure pointer * - * @param this The CRTC information. + * @param this The CRTC information */ -void libgamma_crtc_information_free(libgamma_crtc_information_t* restrict this); +void libgamma_crtc_information_free(libgamma_crtc_information_t *restrict); /** - * Convert a raw representation of an EDID to a lowercase hexadecimal representation. + * 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`. + * @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. + * `NULL` on allocation error, `errno` will be set accordingly */ -#define libgamma_behex_edid(edid, length) \ - libgamma_behex_edid_lowercase(edid, length) +#define libgamma_behex_edid(edid, length) libgamma_behex_edid_lowercase((edid), (length)) /** - * Convert a raw representation of an EDID to a lowercase hexadecimal representation. + * 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. + * @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 edid, size_t length); +char *libgamma_behex_edid_lowercase(const unsigned char *restrict, size_t); /** - * Convert a raw representation of an EDID to an uppercase hexadecimal representation. + * 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`. + * @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. + * NULL` on allocation error, `errno` will be set accordingly */ -char* libgamma_behex_edid_uppercase(const unsigned char* restrict edid, size_t length); +char *libgamma_behex_edid_uppercase(const unsigned char *restrict, size_t); /** - * Convert an hexadecimal representation of an EDID to a raw representation. + * Convert an hexadecimal representation of an EDID to a raw representation * - * @param edid The EDID in hexadecimal 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. + * 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); +unsigned char *libgamma_unhex_edid(const char *restrict); /** - * Get the current gamma ramps for a CRTC, 8-bit gamma-depth version. + * Get the current gamma ramps for a CRTC, 8-bit gamma-depth version * - * @param this The CRTC state. + * @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. + * error identifier provided by this library */ -int libgamma_crtc_get_gamma_ramps8(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps8_t* restrict ramps); +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. + * @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. + * error identifier provided by this library */ -int libgamma_crtc_set_gamma_ramps8(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps8_t ramps); +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. + * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version * - * @param this The CRTC state. + * @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. + * error identifier provided by this library */ -int libgamma_crtc_get_gamma_ramps16(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps16_t* restrict ramps); +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. + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * error identifier provided by this library */ -int libgamma_crtc_set_gamma_ramps16(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps16_t ramps) __attribute__((hot)); +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. + * 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. + * @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. + * error identifier provided by this library */ -int libgamma_crtc_get_gamma_ramps32(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps32_t* restrict ramps); +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. + * Set the gamma ramps for a CRTC, 32-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * error identifier provided by this library */ -int libgamma_crtc_set_gamma_ramps32(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps32_t ramps); +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. + * 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. + * @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. + * error identifier provided by this library */ -int libgamma_crtc_get_gamma_ramps64(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps64_t* restrict ramps); +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. + * Set the gamma ramps for a CRTC, 64-bit gamma-depth version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * error identifier provided by this library */ -int libgamma_crtc_set_gamma_ramps64(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps64_t ramps); +int libgamma_crtc_set_gamma_ramps64(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps64_t); /** - * Set the gamma ramps for a CRTC, `float` version. + * Set the gamma ramps for a CRTC, `float` version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * error identifier provided by this library */ -int libgamma_crtc_set_gamma_rampsf(libgamma_crtc_state_t* restrict this, - libgamma_gamma_rampsf_t ramps); +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. + * 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. + * @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. + * error identifier provided by this library */ -int libgamma_crtc_get_gamma_rampsf(libgamma_crtc_state_t* restrict this, - libgamma_gamma_rampsf_t* restrict ramps); +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. + * 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. + * @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. + * error identifier provided by this library */ -int libgamma_crtc_get_gamma_rampsd(libgamma_crtc_state_t* restrict this, - libgamma_gamma_rampsd_t* restrict ramps); +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. + * Set the gamma ramps for a CRTC, `double` version * - * @param this The CRTC state. - * @param ramps The gamma ramps to apply. + * @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. + * error identifier provided by this library */ -int libgamma_crtc_set_gamma_rampsd(libgamma_crtc_state_t* restrict this, - libgamma_gamma_rampsd_t ramps); +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. + * Set the gamma ramps for a CRTC, 8-bit gamma-depth function version * - * Note that this will probably involve the library allocating temporary data. + * 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. + * @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. + * error identifier provided by this library */ -int libgamma_crtc_set_gamma_ramps8_f(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps8_fun* red_function, - libgamma_gamma_ramps8_fun* green_function, - libgamma_gamma_ramps8_fun* blue_function) __attribute__((cold)); +__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. + * Set the gamma ramps for a CRTC, 16-bit gamma-depth function version * - * Note that this will probably involve the library allocating temporary data. + * 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. + * @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. + * error identifier provided by this library */ -int libgamma_crtc_set_gamma_ramps16_f(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps16_fun* red_function, - libgamma_gamma_ramps16_fun* green_function, - libgamma_gamma_ramps16_fun* blue_function) __attribute__((cold)); +__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. + * Set the gamma ramps for a CRTC, 32-bit gamma-depth function version * - * Note that this will probably involve the library allocating temporary data. + * 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. + * @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. + * error identifier provided by this library */ -int libgamma_crtc_set_gamma_ramps32_f(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps32_fun* red_function, - libgamma_gamma_ramps32_fun* green_function, - libgamma_gamma_ramps32_fun* blue_function) __attribute__((cold)); +__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. + * Set the gamma ramps for a CRTC, 64-bit gamma-depth function version * - * Note that this will probably involve the library allocating temporary data. + * 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. + * @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. + * error identifier provided by this library */ -int libgamma_crtc_set_gamma_ramps64_f(libgamma_crtc_state_t* restrict this, - libgamma_gamma_ramps64_fun* red_function, - libgamma_gamma_ramps64_fun* green_function, - libgamma_gamma_ramps64_fun* blue_function) __attribute__((cold)); +__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. + * Set the gamma ramps for a CRTC, `float` function version * - * Note that this will probably involve the library allocating temporary data. + * 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. + * @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. + * error identifier provided by this library */ -int libgamma_crtc_set_gamma_rampsf_f(libgamma_crtc_state_t* restrict this, - libgamma_gamma_rampsf_fun* red_function, - libgamma_gamma_rampsf_fun* green_function, - libgamma_gamma_rampsf_fun* blue_function) __attribute__((cold)); +__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. + * Set the gamma ramps for a CRTC, `double` function version * - * Note that this will probably involve the library allocating temporary data. + * 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. + * @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. + * error identifier provided by this library */ -int libgamma_crtc_set_gamma_rampsd_f(libgamma_crtc_state_t* restrict this, - libgamma_gamma_rampsd_fun* red_function, - libgamma_gamma_rampsd_fun* green_function, - libgamma_gamma_rampsd_fun* blue_function) __attribute__((cold)); +__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 __GCC__ +#ifndef __GNUC__ # undef __attribute__ #endif #endif - diff --git a/src/lib/libgamma-method.c b/src/lib/libgamma-method.c index 40b4013..cf9a23b 100644 --- a/src/lib/libgamma-method.c +++ b/src/lib/libgamma-method.c @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #include "libgamma-method.h" @@ -26,34 +10,36 @@ /** * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation. + * 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. + * 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. + * @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) +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 == NULL ? -1 : 0; + 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. + * initialises in the proper manner * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_ramps8_destroy(libgamma_gamma_ramps8_t* restrict this) +void +libgamma_gamma_ramps8_destroy(libgamma_gamma_ramps8_t *restrict this) { - free(this->red); + free(this->red); } @@ -61,53 +47,56 @@ void libgamma_gamma_ramps8_destroy(libgamma_gamma_ramps8_t* restrict this) * 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. + * to the structure * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_ramps8_free(libgamma_gamma_ramps8_t* restrict this) +void +libgamma_gamma_ramps8_free(libgamma_gamma_ramps8_t *restrict this) { - free(this->red); - free(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. + * 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. + * 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. + * @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) +int +libgamma_gamma_ramps16_initialise(libgamma_gamma_ramps16_t *restrict this) { - size_t n = this->red_size + this->green_size + this->blue_size; + 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)); + /* 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)); + 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 == NULL ? -1 : 0; + 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. + * initialises in the proper manner * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_ramps16_destroy(libgamma_gamma_ramps16_t* restrict this) +void +libgamma_gamma_ramps16_destroy(libgamma_gamma_ramps16_t *restrict this) { - free(this->red); + free(this->red); } @@ -115,48 +104,51 @@ void libgamma_gamma_ramps16_destroy(libgamma_gamma_ramps16_t* restrict this) * 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. + * to the structure * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_ramps16_free(libgamma_gamma_ramps16_t* restrict this) +void +libgamma_gamma_ramps16_free(libgamma_gamma_ramps16_t *restrict this) { - free(this->red); - free(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. + * 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. + * 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. + * @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) +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 == NULL ? -1 : 0; + 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. + * initialises in the proper manner * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_ramps32_destroy(libgamma_gamma_ramps32_t* restrict this) +void +libgamma_gamma_ramps32_destroy(libgamma_gamma_ramps32_t *restrict this) { - free(this->red); + free(this->red); } @@ -164,48 +156,51 @@ void libgamma_gamma_ramps32_destroy(libgamma_gamma_ramps32_t* restrict this) * 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. + * to the structure * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_ramps32_free(libgamma_gamma_ramps32_t* restrict this) +void +libgamma_gamma_ramps32_free(libgamma_gamma_ramps32_t *restrict this) { - free(this->red); - free(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. + * 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. + * 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. + * @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) +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 == NULL ? -1 : 0; + 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. + * initialises in the proper manner * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_ramps64_destroy(libgamma_gamma_ramps64_t* restrict this) +void +libgamma_gamma_ramps64_destroy(libgamma_gamma_ramps64_t *restrict this) { - free(this->red); + free(this->red); } @@ -213,48 +208,51 @@ void libgamma_gamma_ramps64_destroy(libgamma_gamma_ramps64_t* restrict this) * 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. + * to the structure * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_ramps64_free(libgamma_gamma_ramps64_t* restrict this) +void +libgamma_gamma_ramps64_free(libgamma_gamma_ramps64_t *restrict this) { - free(this->red); - free(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. + * 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. + * 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. + * @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) +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 == NULL ? -1 : 0; + 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. + * initialises in the proper manner * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_rampsf_destroy(libgamma_gamma_rampsf_t* restrict this) +void +libgamma_gamma_rampsf_destroy(libgamma_gamma_rampsf_t *restrict this) { - free(this->red); + free(this->red); } @@ -262,48 +260,51 @@ void libgamma_gamma_rampsf_destroy(libgamma_gamma_rampsf_t* restrict this) * 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. + * to the structure * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_rampsf_free(libgamma_gamma_rampsf_t* restrict this) +void +libgamma_gamma_rampsf_free(libgamma_gamma_rampsf_t *restrict this) { - free(this->red); - free(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. + * 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. + * 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. + * @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) +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 == NULL ? -1 : 0; + 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. + * initialises in the proper manner * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_rampsd_destroy(libgamma_gamma_rampsd_t* restrict this) +void +libgamma_gamma_rampsd_destroy(libgamma_gamma_rampsd_t *restrict this) { - free(this->red); + free(this->red); } @@ -311,13 +312,13 @@ void libgamma_gamma_rampsd_destroy(libgamma_gamma_rampsd_t* restrict this) * 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. + * to the structure * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_rampsd_free(libgamma_gamma_rampsd_t* restrict this) +void +libgamma_gamma_rampsd_free(libgamma_gamma_rampsd_t *restrict this) { - free(this->red); - free(this); + free(this->red); + free(this); } - diff --git a/src/lib/libgamma-method.h b/src/lib/libgamma-method.h index 7c5ca08..e2a35c6 100644 --- a/src/lib/libgamma-method.h +++ b/src/lib/libgamma-method.h @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #ifndef LIBGAMMA_METHOD_H #define LIBGAMMA_METHOD_H @@ -23,7 +7,7 @@ #endif -#ifndef __GCC__ +#ifndef __GNUC__ # define __attribute__(x) #endif @@ -33,171 +17,176 @@ /** - * The identifier for the dummy adjustment method. + * The identifier for the dummy adjustment method + * * This method can be configured and is useful for - * testing your program's ability to handle errors. + * testing your program's ability to handle errors */ -#define LIBGAMMA_METHOD_DUMMY 0 +#define LIBGAMMA_METHOD_DUMMY 0 /** * The identifier for the adjustment method with - * uses the RandR protocol under the X display server. + * uses the RandR protocol under the X display server */ -#define LIBGAMMA_METHOD_X_RANDR 1 +#define LIBGAMMA_METHOD_X_RANDR 1 /** * The identifier for the adjustment method with - * uses the VidMode protocol under the X display server. + * 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). + * screen (partition) */ -#define LIBGAMMA_METHOD_X_VIDMODE 2 +#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 all work when you are in non-graphical - * mode; however a display server cannnot be - * started while this is running, but it can be - * started while a display server is running. + * 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 +#define LIBGAMMA_METHOD_LINUX_DRM 3 /** * The identifier for the Graphics Device Interface - * adjustment method that is available in Windows. + * 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. + * to be available under X.org using a translation layer */ -#define LIBGAMMA_METHOD_W32_GDI 4 +#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. + * 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. + * to be available under X.org using a translation layer */ -#define LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS 5 +#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. + * as it could have been disabled at compile-time */ -#define LIBGAMMA_METHOD_MAX 5 +#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. + * have been removed at compile-time */ -#define LIBGAMMA_METHOD_COUNT (LIBGAMMA_METHOD_MAX + 1) +#define LIBGAMMA_METHOD_COUNT (LIBGAMMA_METHOD_MAX + 1) /** - * Capabilities of adjustment methods. + * 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; +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. + * Site state * * On operating systems that integrate a graphical environment * there is usually just one site. However, one systems with @@ -205,48 +194,47 @@ typedef struct libgamma_method_capabilities * 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; - +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. + * Partition state * * Probably the majority of display server only one partition * per site. However, X.org can, and traditional used to have @@ -257,1069 +245,1079 @@ typedef struct libgamma_site_state * 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; - +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. + * 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. + * 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; - +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. + * 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. - * (If you know what this is add it to Wikipedia.) - */ - LIBGAMMA_CONNECTOR_TYPE_LFP - - } libgamma_connector_type_t; +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`. + * The number of values defined in `libgamma_connector_type_t` */ -#define LIBGAMMA_CONNECTOR_TYPE_COUNT 20 +#define LIBGAMMA_CONNECTOR_TYPE_COUNT 20 /** - * Orders for subpixels. Currently the possible values are - * very biased to LCD, Plasma and monochrome monitors. + * 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; +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`. + * The number of values defined in `libgamma_subpixel_order_t` */ -#define LIBGAMMA_SUBPIXEL_ORDER_COUNT 6 +#define LIBGAMMA_SUBPIXEL_ORDER_COUNT 6 /** - * Answer enum to a decision problem. + * 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; +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`. + * 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) +#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`. + * 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) +#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`. + * 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) +#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`. + * 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) +#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`. + * 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) +#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`. + * 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) +#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`. + * 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) +#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`. + * 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) +#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`. + * 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) +#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`. + * 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`. + * 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) +#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`. + * 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) +#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`. + * values for `gamma_red`, `gamma_green`, and `gamma_blue` + * and report errors to `gamma_error` */ -#define LIBGAMMA_CRTC_INFO_GAMMA (1 << 12) +#define LIBGAMMA_CRTC_INFO_GAMMA (1 << 12) /** - * The number of `LIBGAMMA_CRTC_INFO_*` values defined. + * The number of `LIBGAMMA_CRTC_INFO_*` values defined */ -#define LIBGAMMA_CRTC_INFO_COUNT 13 +#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. + * Information Data */ -#define LIBGAMMA_CRTC_INFO_MACRO_EDID_VIEWPORT ( LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID \ - | LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID ) +#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. + * Information Data */ -#define LIBGAMMA_CRTC_INFO_MACRO_EDID ( LIBGAMMA_CRTC_INFO_EDID \ - | LIBGAMMA_CRTC_INFO_MACRO_EDID_VIEWPORT \ - | LIBGAMMA_CRTC_INFO_GAMMA ) +#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. + * Information Data */ -#define LIBGAMMA_CRTC_INFO_MACRO_VIEWPORT (LIBGAMMA_CRTC_INFO_WIDTH_MM | LIBGAMMA_CRTC_INFO_HEIGHT_MM) +#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. + * ramp depth */ -#define LIBGAMMA_CRTC_INFO_MACRO_RAMP (LIBGAMMA_CRTC_INFO_GAMMA_SIZE | LIBGAMMA_CRTC_INFO_GAMMA_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. + * partition unique name of the connector */ -#define LIBGAMMA_CRTC_INFO_MACRO_CONNECTOR ( LIBGAMMA_CRTC_INFO_CONNECTOR_NAME \ - | LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE ) +#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. + * 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 ) +#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. + * 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; - +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. + * 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; - +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. + * 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; - + /** + * 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. + * 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; - + /** + * 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. + * 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; - + /** + * 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. + * 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; - + /** + * 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. + * 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; - + /** + * 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. + * 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. + * 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. + * @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); +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. + * initialised in the proper manner * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_ramps8_destroy(libgamma_gamma_ramps8_t* restrict this); +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. + * to the structure * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_ramps8_free(libgamma_gamma_ramps8_t* restrict this); +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. + * 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. + * 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. + * @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); +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. + * initialised in the proper manner * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_ramps16_destroy(libgamma_gamma_ramps16_t* restrict this); +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. + * to the structure * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_ramps16_free(libgamma_gamma_ramps16_t* restrict this); +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. + * 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 + * 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. + * @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); +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. + * initialised in the proper manner * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_ramps32_destroy(libgamma_gamma_ramps32_t* restrict this); +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. + * to the structure * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_ramps32_free(libgamma_gamma_ramps32_t* restrict this); +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 + * 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. + * @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); +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. + * initialised in the proper manner * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_ramps64_destroy(libgamma_gamma_ramps64_t* restrict this); +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. + * to the structure * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_ramps64_free(libgamma_gamma_ramps64_t* restrict this); +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. + * 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. + * 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. + * @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); +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. + * initialised in the proper manner * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_rampsf_destroy(libgamma_gamma_rampsf_t* restrict this); +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. + * to the structure * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_rampsf_free(libgamma_gamma_rampsf_t* restrict this); +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. + * 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. + * 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. + * @return Zero on success, -1 on allocation error, `errno` will be set accordingly */ -int libgamma_gamma_rampsd_initialise(libgamma_gamma_rampsd_t* restrict this); +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. + * initialised in the proper manner * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_rampsd_destroy(libgamma_gamma_rampsd_t* restrict this); +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. + * to the structure * - * @param this The gamma ramps. + * @param this The gamma ramps */ -void libgamma_gamma_rampsd_free(libgamma_gamma_rampsd_t* restrict this); +void libgamma_gamma_rampsd_free(libgamma_gamma_rampsd_t *restrict); -#ifndef __GCC__ +#ifndef __GNUC__ # undef __attribute__ #endif #endif - diff --git a/src/lib/libgamma.h b/src/lib/libgamma.h index 9bec47c..cc63fb8 100644 --- a/src/lib/libgamma.h +++ b/src/lib/libgamma.h @@ -1,30 +1,11 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* 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 - diff --git a/src/test/crtcinfo.c b/src/test/crtcinfo.c index 8fd2b03..627559b 100644 --- a/src/test/crtcinfo.c +++ b/src/test/crtcinfo.c @@ -1,56 +1,39 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #include "crtcinfo.h" /** - * Conditionally print a CRTC information field. + * Conditionally print a CRTC information field * - * @param type:data-type Data type for the value of the information field. - * @param notation:string-literal %-pattern for `printf` (excluding the %) that is used for `type`. - * @param do_print Whether the information should be included in the process's output. - * @param description A description of the information field. - * @param error The error of the information field. - * @param value The value of the information field. + * @param type:data-type Data type for the value of the information field + * @param notation:string-literal %-pattern for `printf` (excluding the %) that is used for `type` + * @param do_print Whether the information should be included in the process's output + * @param description A description of the information field + * @param error The error of the information field + * @param value The value of the information field */ -#define print_crtc_information_(type, notation) \ - static void print_crtc_information_##type(int do_print, const char* description, int error, type value) \ - { \ - char buf[256]; \ - if (do_print) \ - { \ - if (error) \ - { \ - snprintf(buf, sizeof(buf) / sizeof(char), " (error) %s", description); \ - libgamma_perror(buf, error); \ - } \ - else \ - printf(" %s: %" notation "\n", description, value); \ - } \ - } +#define print_crtc_information_(type, notation)\ + static void\ + print_crtc_information_##type(int do_print, const char *description, int error, type value)\ + {\ + char buf[256];\ + if (do_print) {\ + if (error) {\ + snprintf(buf, sizeof(buf) / sizeof(char), " (error) %s", description);\ + libgamma_perror(buf, error);\ + } else {\ + printf(" %s: %" notation "\n", description, value);\ + }\ + }\ + } /** * A single [a-z] word alternative to `const char*`, we need it for the - * function name of the string variant of `print_crtc_information_*`. + * function name of the string variant of `print_crtc_information_*` */ -typedef const char* str; -/* Create `print_crtc_information_*` variants. */ +typedef const char *str; +/* Create `print_crtc_information_*` variants */ print_crtc_information_(size_t, "lu") print_crtc_information_(signed, "i") print_crtc_information_(int, "i") @@ -66,193 +49,188 @@ print_crtc_information_(str, "s") #undef print_crtc_information_ -#define __case(VALUE) case VALUE: return #VALUE; +#define __case(VALUE) case VALUE: return #VALUE /** - * Get a string representation of a subpixel order. + * Get a string representation of a subpixel order * - * @param value The subpixel order. - * @return String representation. + * @param value The subpixel order + * @return String representation */ -static const char* subpixel_order_str(libgamma_subpixel_order_t value) +static const char * +subpixel_order_str(libgamma_subpixel_order_t value) { - switch (value) - { - __case (LIBGAMMA_SUBPIXEL_ORDER_UNKNOWN) - __case (LIBGAMMA_SUBPIXEL_ORDER_NONE) - __case (LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_RGB) - __case (LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_BGR) - __case (LIBGAMMA_SUBPIXEL_ORDER_VERTICAL_RGB) - __case (LIBGAMMA_SUBPIXEL_ORDER_VERTICAL_BGR) - default: - return "(unknown)"; - } + switch (value) { + __case (LIBGAMMA_SUBPIXEL_ORDER_UNKNOWN); + __case (LIBGAMMA_SUBPIXEL_ORDER_NONE); + __case (LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_RGB); + __case (LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_BGR); + __case (LIBGAMMA_SUBPIXEL_ORDER_VERTICAL_RGB); + __case (LIBGAMMA_SUBPIXEL_ORDER_VERTICAL_BGR); + default: + return "(unknown)"; + } } /** - * Get a string representation of a connector type. + * Get a string representation of a connector type * - * @param value The connector type. - * @return String representation. + * @param value The connector type + * @return String representation */ -static const char* connector_type_str(libgamma_connector_type_t value) +static const char * +connector_type_str(libgamma_connector_type_t value) { - switch (value) - { - __case (LIBGAMMA_CONNECTOR_TYPE_Unknown) - __case (LIBGAMMA_CONNECTOR_TYPE_VGA) - __case (LIBGAMMA_CONNECTOR_TYPE_DVI) - __case (LIBGAMMA_CONNECTOR_TYPE_DVII) - __case (LIBGAMMA_CONNECTOR_TYPE_DVID) - __case (LIBGAMMA_CONNECTOR_TYPE_DVIA) - __case (LIBGAMMA_CONNECTOR_TYPE_Composite) - __case (LIBGAMMA_CONNECTOR_TYPE_SVIDEO) - __case (LIBGAMMA_CONNECTOR_TYPE_LVDS) - __case (LIBGAMMA_CONNECTOR_TYPE_Component) - __case (LIBGAMMA_CONNECTOR_TYPE_9PinDIN) - __case (LIBGAMMA_CONNECTOR_TYPE_DisplayPort) - __case (LIBGAMMA_CONNECTOR_TYPE_HDMI) - __case (LIBGAMMA_CONNECTOR_TYPE_HDMIA) - __case (LIBGAMMA_CONNECTOR_TYPE_HDMIB) - __case (LIBGAMMA_CONNECTOR_TYPE_TV) - __case (LIBGAMMA_CONNECTOR_TYPE_eDP) - __case (LIBGAMMA_CONNECTOR_TYPE_VIRTUAL) - __case (LIBGAMMA_CONNECTOR_TYPE_DSI) - __case (LIBGAMMA_CONNECTOR_TYPE_LFP) - default: - return "(unknown)"; - } + switch (value) { + __case (LIBGAMMA_CONNECTOR_TYPE_Unknown); + __case (LIBGAMMA_CONNECTOR_TYPE_VGA); + __case (LIBGAMMA_CONNECTOR_TYPE_DVI); + __case (LIBGAMMA_CONNECTOR_TYPE_DVII); + __case (LIBGAMMA_CONNECTOR_TYPE_DVID); + __case (LIBGAMMA_CONNECTOR_TYPE_DVIA); + __case (LIBGAMMA_CONNECTOR_TYPE_Composite); + __case (LIBGAMMA_CONNECTOR_TYPE_SVIDEO); + __case (LIBGAMMA_CONNECTOR_TYPE_LVDS); + __case (LIBGAMMA_CONNECTOR_TYPE_Component); + __case (LIBGAMMA_CONNECTOR_TYPE_9PinDIN); + __case (LIBGAMMA_CONNECTOR_TYPE_DisplayPort); + __case (LIBGAMMA_CONNECTOR_TYPE_HDMI); + __case (LIBGAMMA_CONNECTOR_TYPE_HDMIA); + __case (LIBGAMMA_CONNECTOR_TYPE_HDMIB); + __case (LIBGAMMA_CONNECTOR_TYPE_TV); + __case (LIBGAMMA_CONNECTOR_TYPE_eDP); + __case (LIBGAMMA_CONNECTOR_TYPE_VIRTUAL); + __case (LIBGAMMA_CONNECTOR_TYPE_DSI); + __case (LIBGAMMA_CONNECTOR_TYPE_LFP); + default: + return "(unknown)"; + } } /** - * Get a string representation of a decision. + * Get a string representation of a decision * - * @param value The decision. - * @return String representation. + * @param value The decision + * @return String representation */ -static const char* decision_str(libgamma_decision_t value) +static const char * +decision_str(libgamma_decision_t value) { - switch (value) - { - __case (LIBGAMMA_NO) - __case (LIBGAMMA_MAYBE) - __case (LIBGAMMA_YES) - default: - return "(unknown)"; - } + switch (value) { + __case (LIBGAMMA_NO); + __case (LIBGAMMA_MAYBE); + __case (LIBGAMMA_YES); + default: + return "(unknown)"; + } } #undef __case /** - * The CRTC information for a CRTC. + * The CRTC information for a CRTC * - * @param crtc The CRTC. + * @param crtc The CRTC */ -void crtc_information(libgamma_crtc_state_t* restrict crtc) +void +crtc_information(libgamma_crtc_state_t *restrict crtc) { - libgamma_method_capabilities_t caps; - libgamma_crtc_information_t info; - int fields; - int field; - - /* Get supported CRTC informations fields. */ - libgamma_method_capabilities(&caps, crtc->partition->site->method); - - /* List unsupport information fields by testing them one by one. */ - for (fields = caps.crtc_information; field = fields & -fields, fields; fields ^= field) - { - if (libgamma_get_crtc_information(&info, crtc, field)) - printf("Could not read CRTC information field %i\n", field); - free(info.edid); - free(info.connector_name); - } - - /* Get CRTC information, that is supported. */ - fields = caps.crtc_information; - if (libgamma_get_crtc_information(&info, crtc, fields)) - printf("An error occurred while reading CRTC information\n"); - - /* Macros for printing CRTC information. */ -#define print2(TYPE, FIELD_ID, DESCRIPTION, FIELD_VAR, ERROR_VAR) \ - print_crtc_information_##TYPE(fields & FIELD_ID, DESCRIPTION, info.ERROR_VAR, info.FIELD_VAR); -#define print(TYPE, FIELD_ID, DESCRIPTION, FIELD_VAR) \ - print2(TYPE, FIELD_ID, DESCRIPTION, FIELD_VAR, FIELD_VAR##_error); - - - /* Print CRTC information. */ - - printf("CRTC information:\n"); - - /* Print the EDID field. */ - if ((fields & LIBGAMMA_CRTC_INFO_EDID)) - { - if (info.edid_error) - libgamma_perror(" (error) EDID", info.edid_error); - else - { - char* edid_lc = libgamma_behex_edid(info.edid, info.edid_length); - unsigned char* edid_raw = libgamma_unhex_edid(edid_lc); - char* edid_uc = libgamma_behex_edid_uppercase(edid_raw, info.edid_length);; - printf(" EDID: %s\n", edid_lc); - printf(" EDID (uppercase): %s\n", edid_uc); - printf(" EDID (length): %lu\n", info.edid_length); - free(edid_lc); - free(edid_raw); - free(edid_uc); + libgamma_method_capabilities_t caps; + libgamma_crtc_information_t info; + int fields, field; + char *edid_lc, *edid_uc; + unsigned char *edid_raw; + + /* Get supported CRTC informations fields */ + libgamma_method_capabilities(&caps, crtc->partition->site->method); + + /* List unsupport information fields by testing them one by one */ + for (fields = caps.crtc_information; field = fields & -fields, fields; fields ^= field) { + if (libgamma_get_crtc_information(&info, crtc, field)) + printf("Could not read CRTC information field %i\n", field); + free(info.edid); + free(info.connector_name); } - } - /* Print physical dimensions of the monitor. */ - print(size_t, LIBGAMMA_CRTC_INFO_WIDTH_MM, "width", width_mm); - print(size_t, LIBGAMMA_CRTC_INFO_HEIGHT_MM, "height", height_mm); - print(size_t, LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID, "width per EDID", width_mm_edid); - print(size_t, LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID, "height per EDID", height_mm_edid); - /* Print gamma ramp information. */ - print2(size_t, LIBGAMMA_CRTC_INFO_GAMMA_SIZE, "red gamma ramp size", red_gamma_size, gamma_size_error); - print2(size_t, LIBGAMMA_CRTC_INFO_GAMMA_SIZE, "green gamma ramp size", green_gamma_size, gamma_size_error); - print2(size_t, LIBGAMMA_CRTC_INFO_GAMMA_SIZE, "blue gamma ramp size", blue_gamma_size, gamma_size_error); - print(signed, LIBGAMMA_CRTC_INFO_GAMMA_DEPTH, "gamma ramp depth", gamma_depth); - /* Print gamma ramp support. */ - if ((fields & LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT)) - { - if (info.gamma_support_error) - libgamma_perror(" (error) gamma support", info.gamma_support_error); - else - printf(" gamma support: %s\n", decision_str(info.gamma_support)); - } - /* Print subpixel order for the monitor. */ - if ((fields & LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER)) - { - if (info.subpixel_order_error) - libgamma_perror(" (error) subpixel order", info.subpixel_order_error); - else - printf(" subpixel order: %s\n", subpixel_order_str(info.subpixel_order)); - } - /* Print connector information. */ - print(int, LIBGAMMA_CRTC_INFO_ACTIVE, "active", active); - print(str, LIBGAMMA_CRTC_INFO_CONNECTOR_NAME, "connector name", connector_name); - if ((fields & LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE)) - { - if (info.connector_type_error) - libgamma_perror(" (error) subpixel order", info.connector_type_error); - else - printf(" subpixel order: %s\n", connector_type_str(info.connector_type)); - } - /* Print the gamma characteristics of the monitor. */ - print2(float, LIBGAMMA_CRTC_INFO_GAMMA, "red gamma characteristics", gamma_red, gamma_error); - print2(float, LIBGAMMA_CRTC_INFO_GAMMA, "green gamma characteristics", gamma_green, gamma_error); - print2(float, LIBGAMMA_CRTC_INFO_GAMMA, "blue gamma characteristics", gamma_blue, gamma_error); - printf("\n"); + + /* Get CRTC information, that is supported */ + fields = caps.crtc_information; + if (libgamma_get_crtc_information(&info, crtc, fields)) + printf("An error occurred while reading CRTC information\n"); + + /* Macros for printing CRTC information */ +#define print2(TYPE, FIELD_ID, DESCRIPTION, FIELD_VAR, ERROR_VAR)\ + print_crtc_information_##TYPE(fields & FIELD_ID, DESCRIPTION, info.ERROR_VAR, info.FIELD_VAR); +#define print(TYPE, FIELD_ID, DESCRIPTION, FIELD_VAR)\ + print2(TYPE, FIELD_ID, DESCRIPTION, FIELD_VAR, FIELD_VAR##_error); + /* Print CRTC information */ + + printf("CRTC information:\n"); + + /* Print the EDID field */ + if ((fields & LIBGAMMA_CRTC_INFO_EDID)) { + if (info.edid_error) { + libgamma_perror(" (error) EDID", info.edid_error); + } else { + edid_lc = libgamma_behex_edid(info.edid, info.edid_length); + edid_raw = libgamma_unhex_edid(edid_lc); + edid_uc = libgamma_behex_edid_uppercase(edid_raw, info.edid_length); + printf(" EDID: %s\n", edid_lc); + printf(" EDID (uppercase): %s\n", edid_uc); + printf(" EDID (length): %lu\n", info.edid_length); + free(edid_lc); + free(edid_raw); + free(edid_uc); + } + } + /* Print physical dimensions of the monitor */ + print(size_t, LIBGAMMA_CRTC_INFO_WIDTH_MM, "width", width_mm); + print(size_t, LIBGAMMA_CRTC_INFO_HEIGHT_MM, "height", height_mm); + print(size_t, LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID, "width per EDID", width_mm_edid); + print(size_t, LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID, "height per EDID", height_mm_edid); + /* Print gamma ramp information */ + print2(size_t, LIBGAMMA_CRTC_INFO_GAMMA_SIZE, "red gamma ramp size", red_gamma_size, gamma_size_error); + print2(size_t, LIBGAMMA_CRTC_INFO_GAMMA_SIZE, "green gamma ramp size", green_gamma_size, gamma_size_error); + print2(size_t, LIBGAMMA_CRTC_INFO_GAMMA_SIZE, "blue gamma ramp size", blue_gamma_size, gamma_size_error); + print(signed, LIBGAMMA_CRTC_INFO_GAMMA_DEPTH, "gamma ramp depth", gamma_depth); + /* Print gamma ramp support */ + if (fields & LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT) { + if (info.gamma_support_error) + libgamma_perror(" (error) gamma support", info.gamma_support_error); + else + printf(" gamma support: %s\n", decision_str(info.gamma_support)); + } + /* Print subpixel order for the monitor */ + if (fields & LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER) { + if (info.subpixel_order_error) + libgamma_perror(" (error) subpixel order", info.subpixel_order_error); + else + printf(" subpixel order: %s\n", subpixel_order_str(info.subpixel_order)); + } + /* Print connector information */ + print(int, LIBGAMMA_CRTC_INFO_ACTIVE, "active", active); + print(str, LIBGAMMA_CRTC_INFO_CONNECTOR_NAME, "connector name", connector_name); + if (fields & LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE) { + if (info.connector_type_error) + libgamma_perror(" (error) subpixel order", info.connector_type_error); + else + printf(" subpixel order: %s\n", connector_type_str(info.connector_type)); + } + /* Print the gamma characteristics of the monitor */ + print2(float, LIBGAMMA_CRTC_INFO_GAMMA, "red gamma characteristics", gamma_red, gamma_error); + print2(float, LIBGAMMA_CRTC_INFO_GAMMA, "green gamma characteristics", gamma_green, gamma_error); + print2(float, LIBGAMMA_CRTC_INFO_GAMMA, "blue gamma characteristics", gamma_blue, gamma_error); + printf("\n"); + + #undef print #undef print2 - - /* Release resouces. */ - free(info.edid); - free(info.connector_name); -} + /* Release resouces */ + free(info.edid); + free(info.connector_name); +} diff --git a/src/test/crtcinfo.h b/src/test/crtcinfo.h index 3c22c0b..dc120e7 100644 --- a/src/test/crtcinfo.h +++ b/src/test/crtcinfo.h @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #ifndef LIBGAMMA_TEST_CRTCINFO_H #define LIBGAMMA_TEST_CRTCINFO_H @@ -26,12 +10,11 @@ /** - * The CRTC information for a CRTC. + * The CRTC information for a CRTC * - * @param crtc The CRTC. + * @param crtc The CRTC */ -void crtc_information(libgamma_crtc_state_t* restrict crtc); +void crtc_information(libgamma_crtc_state_t *restrict crtc); #endif - diff --git a/src/test/errors.c b/src/test/errors.c index 6496d21..ffe9bd2 100644 --- a/src/test/errors.c +++ b/src/test/errors.c @@ -1,82 +1,66 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #include "errors.h" /** - * The error API. + * The error API */ -void error_test(void) +void +error_test(void) { - int i; + int i; - /* Test that naming and name dereferencing - of errors work. Because the mappings in - these [`libgamma_value_of_error` and - `libgamma_name_of_error`] functions are - generated, it should work if and only if - one test passes, assumming the errors are - unique whihc is tested in the end of this - function. */ - printf("Testing error API using LIBGAMMA_STATE_UNKNOWN:\n"); - printf(" Expecting %i: %i\n", LIBGAMMA_STATE_UNKNOWN, libgamma_value_of_error("LIBGAMMA_STATE_UNKNOWN")); - printf(" Expecting %s: %s\n", "LIBGAMMA_STATE_UNKNOWN", libgamma_name_of_error(LIBGAMMA_STATE_UNKNOWN)); - printf("\n"); + /* Test that naming and name dereferencing + of errors work. Because the mappings in + these [`libgamma_value_of_error` and + `libgamma_name_of_error`] functions are + generated, it should work if and only if + one test passes, assumming the errors are + unique whihc is tested in the end of this + function. */ + printf("Testing error API using LIBGAMMA_STATE_UNKNOWN:\n"); + printf(" Expecting %i: %i\n", LIBGAMMA_STATE_UNKNOWN, libgamma_value_of_error("LIBGAMMA_STATE_UNKNOWN")); + printf(" Expecting %s: %s\n", "LIBGAMMA_STATE_UNKNOWN", libgamma_name_of_error(LIBGAMMA_STATE_UNKNOWN)); + printf("\n"); - /* Test that `libgamma_perror` can print - libgamma errors and system errors, and - handle success in the same with as `perror`. */ - printf("Testing libgamma_perror:\n"); - libgamma_perror(" Expecting LIBGAMMA_STATE_UNKNOWN", LIBGAMMA_STATE_UNKNOWN); - libgamma_perror(" Expecting a description for ENOMEM", ENOMEM); - libgamma_perror(" Expecting a description for successfulness", 0); - /* Test that `libgamma_perror` handles - `LIBGAMMA_ERRNO_SET` correctly. */ - libgamma_perror(" Expecting a description for ENOMEM", (errno = ENOMEM, LIBGAMMA_ERRNO_SET)); - /* That that `libgamma_perror` handles - `LIBGAMMA_DEVICE_REQUIRE_GROUP` - correctly both when the required - group's name is known and when it - is unknown. */ - libgamma_group_gid = 10; - libgamma_group_name = "test"; - libgamma_perror(" Expecting 'LIBGAMMA_DEVICE_REQUIRE_GROUP: test (10)'", LIBGAMMA_DEVICE_REQUIRE_GROUP); - libgamma_group_name = NULL; - libgamma_perror(" Expecting 'LIBGAMMA_DEVICE_REQUIRE_GROUP: 10'", LIBGAMMA_DEVICE_REQUIRE_GROUP); - printf("\n"); + /* Test that `libgamma_perror` can print + libgamma errors and system errors, and + handle success in the same with as `perror`. */ + printf("Testing libgamma_perror:\n"); + libgamma_perror(" Expecting LIBGAMMA_STATE_UNKNOWN", LIBGAMMA_STATE_UNKNOWN); + libgamma_perror(" Expecting a description for ENOMEM", ENOMEM); + libgamma_perror(" Expecting a description for successfulness", 0); + /* Test that `libgamma_perror` handles + `LIBGAMMA_ERRNO_SET` correctly. */ + libgamma_perror(" Expecting a description for ENOMEM", (errno = ENOMEM, LIBGAMMA_ERRNO_SET)); + /* That that `libgamma_perror` handles + `LIBGAMMA_DEVICE_REQUIRE_GROUP` + correctly both when the required + group's name is known and when it + is unknown. */ + libgamma_group_gid = 10; + libgamma_group_name = "test"; + libgamma_perror(" Expecting 'LIBGAMMA_DEVICE_REQUIRE_GROUP: test (10)'", LIBGAMMA_DEVICE_REQUIRE_GROUP); + libgamma_group_name = NULL; + libgamma_perror(" Expecting 'LIBGAMMA_DEVICE_REQUIRE_GROUP: 10'", LIBGAMMA_DEVICE_REQUIRE_GROUP); + printf("\n"); - /* That all libgamma error codes - are unique. This is done by - getting the name associated - with an error code and the getting - the error code associated that - name and test that the same - error code is returned as put in, - for each libgamma error code. */ - printf("Testing error code uniqueness: "); - for (i = -1; i >= LIBGAMMA_ERROR_MIN; i--) - if (libgamma_value_of_error(libgamma_name_of_error(i)) != i) - { - printf("failed\n"); - goto not_unique; - } - printf("passed\n"); - not_unique: - printf("\n"); + /* That all libgamma error codes + are unique. This is done by + getting the name associated + with an error code and the getting + the error code associated that + name and test that the same + error code is returned as put in, + for each libgamma error code. */ + printf("Testing error code uniqueness: "); + for (i = -1; i >= LIBGAMMA_ERROR_MIN; i--) { + if (libgamma_value_of_error(libgamma_name_of_error(i)) != i) { + printf("failed\n"); + goto not_unique; + } + } + printf("passed\n"); +not_unique: + printf("\n"); } - diff --git a/src/test/errors.h b/src/test/errors.h index 616cdaa..1000a7d 100644 --- a/src/test/errors.h +++ b/src/test/errors.h @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #ifndef LIBGAMMA_TEST_ERRROS_H #define LIBGAMMA_TEST_ERRROS_H @@ -26,10 +10,9 @@ /** - * The error API. + * The error API */ void error_test(void); #endif - diff --git a/src/test/methods.c b/src/test/methods.c index 0b58f98..5cd8bd6 100644 --- a/src/test/methods.c +++ b/src/test/methods.c @@ -1,121 +1,103 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #include "methods.h" /** * Get the name representation of an - * adjustment method by its identifier. + * adjustment method by its identifier * - * @param method The identifier of the adjustment method. - * @return The name of the adjustment method, should not be `free`:d. + * @param method The identifier of the adjustment method + * @return The name of the adjustment method, should not be `free`:d */ -const char* method_name(int method) +const char * +method_name(int method) { - switch (method) - { - case LIBGAMMA_METHOD_DUMMY: return "dummy"; - case LIBGAMMA_METHOD_X_RANDR: return "RandR X extension"; - case LIBGAMMA_METHOD_X_VIDMODE: return "VidMode X extension"; - case LIBGAMMA_METHOD_LINUX_DRM: return "Linux DRM"; - case LIBGAMMA_METHOD_W32_GDI: return "Windows GDI"; - case LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS: return "Quartz using CoreGraphics"; - default: - return "(unknown)"; - } + switch (method) { + case LIBGAMMA_METHOD_DUMMY: return "dummy"; + case LIBGAMMA_METHOD_X_RANDR: return "RandR X extension"; + case LIBGAMMA_METHOD_X_VIDMODE: return "VidMode X extension"; + case LIBGAMMA_METHOD_LINUX_DRM: return "Linux DRM"; + case LIBGAMMA_METHOD_W32_GDI: return "Windows GDI"; + case LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS: return "Quartz using CoreGraphics"; + default: + return "(unknown)"; + } } /** - * Print a list of adjustment methods. + * Print a list of adjustment methods * - * @param description Precursory text for the list. - * @param methods An array allocated to fit all adjustment methods. - * @param operation See the `operation` parameter for `libgamma_list_methods`. + * @param description Precursory text for the list + * @param methods An array allocated to fit all adjustment methods + * @param operation See the `operation` parameter for `libgamma_list_methods` */ -void list_methods(const char* description, int* methods, int operation) +void +list_methods(const char *description, int *methods, int operation) { - /* Get adjustment method list. */ - size_t i, n = libgamma_list_methods(methods, LIBGAMMA_METHOD_COUNT, operation); - - /* Print adjustment method list. */ - printf("%s:\n", description); - for (i = 0; i < n; i++) - printf(" %s\n", method_name(methods[i])); - printf("\n"); + /* Get adjustment method list */ + size_t i, n = libgamma_list_methods(methods, LIBGAMMA_METHOD_COUNT, operation); + + /* Print adjustment method list */ + printf("%s:\n", description); + for (i = 0; i < n; i++) + printf(" %s\n", method_name(methods[i])); + printf("\n"); } /** * Print all lists, of adjustments methods, that - * are made available by `libgamma_list_methods`. + * are made available by `libgamma_list_methods` */ -void list_methods_lists(void) +void +list_methods_lists(void) { - /* Allocate a list for adjustment methods that is - large enough if the program is up to date with - the library. */ - int* methods = malloc(LIBGAMMA_METHOD_COUNT * sizeof(int)); - /* Get a list of all adjustment methods. */ - size_t n = libgamma_list_methods(methods, LIBGAMMA_METHOD_COUNT, 4); - - /* If we got more adjustment methods than we expected - the program needs to be recompiled, and we need to - reallocate the list so it is large enough. */ - if (n > LIBGAMMA_METHOD_COUNT) - { - printf("Warning: you should to recompile the program, libgamma has been updated.\n"); - methods = realloc(methods, n * sizeof(int)); - } - - /* Print adjustment method lists. */ - list_methods("Available adjustment methods", methods, 4); - list_methods("Available real adjustment methods", methods, 3); - list_methods("Available real non-fake adjustment methods", methods, 2); - list_methods("Recommended adjustment methods", methods, 1); - list_methods("Recommended non-fake adjustment methods", methods, 0); - - free(methods); + /* Allocate a list for adjustment methods that is large + enough if the program is up to date with the library */ + int *methods = malloc(LIBGAMMA_METHOD_COUNT * sizeof(int)); + /* Get a list of all adjustment methods */ + size_t n = libgamma_list_methods(methods, LIBGAMMA_METHOD_COUNT, 4); + + /* If we got more adjustment methods than we expected + the program needs to be recompiled, and we need to + reallocate the list so it is large enough. */ + if (n > LIBGAMMA_METHOD_COUNT) { + printf("Warning: you should to recompile the program, libgamma has been updated.\n"); + methods = realloc(methods, n * sizeof(int)); + } + + /* Print adjustment method lists. */ + list_methods("Available adjustment methods", methods, 4); + list_methods("Available real adjustment methods", methods, 3); + list_methods("Available real non-fake adjustment methods", methods, 2); + list_methods("Recommended adjustment methods", methods, 1); + list_methods("Recommended non-fake adjustment methods", methods, 0); + + free(methods); } /** - * Test the availability (determined - * at compile-time) of all adjustment - * methods and one that does not exist. + * Test the availability (determined at + * compile-time) of all adjustment + * methods and one that does not exist */ -void method_availability(void) +void +method_availability(void) { - int method; - /* The availability of an adjustmen method - whose identifier is invalid. It should - say it is not available. */ - printf("Testing the availability of a non-existing adjustment method: "); - printf("%s\n", libgamma_is_method_available(-1) ? "available" : "not available"); - - /* Test the availability of the adjustment - methods that does exist. */ - for (method = 0; method < LIBGAMMA_METHOD_COUNT; method++) - { - printf("Testing the availability of %s: ", method_name(method)); - printf("%s\n", libgamma_is_method_available(method) ? "available" : "not available"); - } - printf("\n"); + int method; + /* The availability of an adjustmen method whose identifier + is invalid. It should say it is not available. */ + printf("Testing the availability of a non-existing adjustment method: "); + printf("%s\n", libgamma_is_method_available(-1) ? "available" : "not available"); + + /* Test the availability of the adjustment methods that does exist */ + for (method = 0; method < LIBGAMMA_METHOD_COUNT; method++) { + printf("Testing the availability of %s: ", method_name(method)); + printf("%s\n", libgamma_is_method_available(method) ? "available" : "not available"); + } + printf("\n"); } @@ -123,23 +105,24 @@ void method_availability(void) * List the default site and the environment * variable, if any, that determines the * default site, for all availiable adjustment - * methods. + * methods */ -void list_default_sites(void) +void +list_default_sites(void) { - int method; - for (method = 0; method < LIBGAMMA_METHOD_COUNT; method++) - /* Include only available adjustment methods. - If an adjustment method is not available - it should print "(null)", but as that can - be misguiding we exclude those methods. */ - if (libgamma_is_method_available(method)) - { - printf("Default site for %s:\n", method_name(method)); - printf(" Variable: %s\n", libgamma_method_default_site_variable(method)); - printf(" Site name: %s\n", libgamma_method_default_site(method)); - printf("\n"); - } + int method; + for (method = 0; method < LIBGAMMA_METHOD_COUNT; method++) { + /* Include only available adjustment methods. + If an adjustment method is not available + it should print "(null)", but as that can + be misguiding we exclude those methods. */ + if (libgamma_is_method_available(method)) { + printf("Default site for %s:\n", method_name(method)); + printf(" Variable: %s\n", libgamma_method_default_site_variable(method)); + printf(" Site name: %s\n", libgamma_method_default_site(method)); + printf("\n"); + } + } } @@ -147,39 +130,39 @@ void list_default_sites(void) * Print the capabilities of all availiable * adjustment methods. */ -void method_capabilities(void) +void +method_capabilities(void) { - libgamma_method_capabilities_t caps; - int method; - for (method = 0; method < LIBGAMMA_METHOD_COUNT; method++) - if (libgamma_is_method_available(method)) - { - /* Print adjustment method name and get the - adjustment method's capabilities. */ - printf("Capabilities of %s:\n", method_name(method)); - libgamma_method_capabilities(&caps, method); - - /* Print capabilities. The CRTC information - capabilities is printed hexadecimal. See - the `LIBGAMMA_CRTC_INFO_*` definitions in - `src/lib/libgamma-method.h` for what each - bit represents. */ - printf(" %s: %X\n", "CRTC information", caps.crtc_information); - printf(" %s: %s\n", "Default site known", caps.default_site_known ? "yes" : "no"); - printf(" %s: %s\n", "Multiple sites", caps.multiple_sites ? "yes" : "no"); - printf(" %s: %s\n", "Multiple partitions", caps.multiple_partitions ? "yes" : "no"); - printf(" %s: %s\n", "Multiple crtcs", caps.multiple_crtcs ? "yes" : "no"); - printf(" %s: %s\n", "Graphics cards", caps.partitions_are_graphics_cards ? "yes" : "no"); - printf(" %s: %s\n", "Site restore", caps.site_restore ? "yes" : "no"); - printf(" %s: %s\n", "Partition restore", caps.partition_restore ? "yes" : "no"); - printf(" %s: %s\n", "CRTC restore", caps.crtc_restore ? "yes" : "no"); - printf(" %s: %s\n", "Identical gamma sizes", caps.identical_gamma_sizes ? "yes" : "no"); - printf(" %s: %s\n", "Fixed gamma size", caps.fixed_gamma_size ? "yes" : "no"); - printf(" %s: %s\n", "Fixed gamma depth", caps.fixed_gamma_depth ? "yes" : "no"); - printf(" %s: %s\n", "Real method", caps.real ? "yes" : "no"); - printf(" %s: %s\n", "Fake method", caps.fake ? "yes" : "no"); - printf(" %s: %s\n", "Auto restore", caps.auto_restore ? "yes" : "no"); - printf("\n"); - } -} + libgamma_method_capabilities_t caps; + int method; + for (method = 0; method < LIBGAMMA_METHOD_COUNT; method++) { + if (libgamma_is_method_available(method)) { + /* Print adjustment method name and get the + adjustment method's capabilities. */ + printf("Capabilities of %s:\n", method_name(method)); + libgamma_method_capabilities(&caps, method); + /* Print capabilities. The CRTC information + capabilities is printed hexadecimal. See + the `LIBGAMMA_CRTC_INFO_*` definitions in + `src/lib/libgamma-method.h` for what each + bit represents. */ + printf(" %s: %X\n", "CRTC information", caps.crtc_information); + printf(" %s: %s\n", "Default site known", caps.default_site_known ? "yes" : "no"); + printf(" %s: %s\n", "Multiple sites", caps.multiple_sites ? "yes" : "no"); + printf(" %s: %s\n", "Multiple partitions", caps.multiple_partitions ? "yes" : "no"); + printf(" %s: %s\n", "Multiple crtcs", caps.multiple_crtcs ? "yes" : "no"); + printf(" %s: %s\n", "Graphics cards", caps.partitions_are_graphics_cards ? "yes" : "no"); + printf(" %s: %s\n", "Site restore", caps.site_restore ? "yes" : "no"); + printf(" %s: %s\n", "Partition restore", caps.partition_restore ? "yes" : "no"); + printf(" %s: %s\n", "CRTC restore", caps.crtc_restore ? "yes" : "no"); + printf(" %s: %s\n", "Identical gamma sizes", caps.identical_gamma_sizes ? "yes" : "no"); + printf(" %s: %s\n", "Fixed gamma size", caps.fixed_gamma_size ? "yes" : "no"); + printf(" %s: %s\n", "Fixed gamma depth", caps.fixed_gamma_depth ? "yes" : "no"); + printf(" %s: %s\n", "Real method", caps.real ? "yes" : "no"); + printf(" %s: %s\n", "Fake method", caps.fake ? "yes" : "no"); + printf(" %s: %s\n", "Auto restore", caps.auto_restore ? "yes" : "no"); + printf("\n"); + } + } +} diff --git a/src/test/methods.h b/src/test/methods.h index 2e13b8d..e7f53ab 100644 --- a/src/test/methods.h +++ b/src/test/methods.h @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #ifndef LIBGAMMA_TEST_METHODS_H #define LIBGAMMA_TEST_METHODS_H @@ -32,32 +16,32 @@ /** * Get the name representation of an - * adjustment method by its identifier. + * adjustment method by its identifier * - * @param method The identifier of the adjustment method. - * @return The name of the adjustment method, should not be `free`:d. + * @param method The identifier of the adjustment method + * @return The name of the adjustment method, should not be `free`:d */ -const char* method_name(int method) __attribute__((const)); +const char *method_name(int method) __attribute__((const)); /** - * Print a list of adjustment methods. + * Print a list of adjustment methods * - * @param description Precursory text for the list. - * @param methods An array allocated to fit all adjustment methods. - * @param operation See the `operation` parameter for `libgamma_list_methods`. + * @param description Precursory text for the list + * @param methods An array allocated to fit all adjustment methods + * @param operation See the `operation` parameter for `libgamma_list_methods` */ -void list_methods(const char* description, int* methods, int operation); +void list_methods(const char *description, int *methods, int operation); /** * Print all lists, of adjustments methods, that - * are made available by `libgamma_list_methods`. + * are made available by `libgamma_list_methods` */ void list_methods_lists(void); /** * Test the availability (determined * at compile-time) of all adjustment - * methods and one that does not exist. + * methods and one that does not exist */ void method_availability(void); @@ -65,16 +49,15 @@ void method_availability(void); * List the default site and the environment * variable, if any, that determines the * default site, for all availiable adjustment - * methods. + * methods */ void list_default_sites(void); /** * Print the capabilities of all availiable - * adjustment methods. + * adjustment methods */ void method_capabilities(void); #endif - diff --git a/src/test/ramps.c b/src/test/ramps.c index ee3aa7d..5303195 100644 --- a/src/test/ramps.c +++ b/src/test/ramps.c @@ -1,115 +1,104 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #include "ramps.h" /** - * Test mapping function from [0, 1] float encoding value to [0, 2⁸ − 1] integer output value. + * Test 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. + * @param encoding [0, 1] float encoding value + * @return [0, 2⁸ − 1] integer output value */ -uint8_t invert_ramps8(float encoding) +uint8_t +invert_ramps8(float encoding) { - double i_encoding = (double)(1.f - encoding); - double f_output = ((double)UINT8_MAX) * i_encoding; - uint8_t output = (uint8_t)f_output; - if ((i_encoding < (double)(0.25f)) && (output > UINT8_MAX / 2)) - output = 0; - if ((i_encoding > (double)(0.75f)) && (output < UINT8_MAX / 2)) - output = UINT8_MAX; - return output; + double i_encoding = (double)(1.f - encoding); + double f_output = (double)UINT8_MAX * i_encoding; + uint8_t output = (uint8_t)f_output; + if (i_encoding < (double)0.25f && output > UINT8_MAX / 2) + output = 0; + if (i_encoding > (double)0.75f && output < UINT8_MAX / 2) + output = UINT8_MAX; + return output; } /** - * Test mapping function from [0, 1] float encoding value to [0, 2¹⁶ − 1] integer output value. + * Test 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. + * @param encoding [0, 1] float encoding value + * @return [0, 2¹⁶ − 1] integer output value */ -uint16_t invert_ramps16(float encoding) +uint16_t +invert_ramps16(float encoding) { - double i_encoding = (double)(1.f - encoding); - double f_output = ((double)UINT16_MAX) * i_encoding; - uint16_t output = (uint16_t)f_output; - if ((i_encoding < (double)(0.25f)) && (output > UINT16_MAX / 2)) - output = 0; - if ((i_encoding > (double)(0.75f)) && (output < UINT16_MAX / 2)) - output = UINT16_MAX; - return output; + double i_encoding = (double)(1.f - encoding); + double f_output = (double)UINT16_MAX * i_encoding; + uint16_t output = (uint16_t)f_output; + if (i_encoding < (double)0.25f && output > UINT16_MAX / 2) + output = 0; + if (i_encoding > (double)0.75f && output < UINT16_MAX / 2) + output = UINT16_MAX; + return output; } /** - * Test mapping function from [0, 1] float encoding value to [0, 2³² − 1] integer output value. + * Test 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. + * @param encoding [0, 1] float encoding value + * @return [0, 2³² − 1] integer output value */ -uint32_t invert_ramps32(float encoding) +uint32_t +invert_ramps32(float encoding) { - double i_encoding = (double)(1.f - encoding); - double f_output = ((double)UINT32_MAX) * i_encoding; - uint32_t output = (uint32_t)f_output; - if ((i_encoding < (double)(0.25f)) && (output > UINT32_MAX / 2)) - output = 0; - if ((i_encoding > (double)(0.75f)) && (output < UINT32_MAX / 2)) - output = UINT32_MAX; - return output; + double i_encoding = (double)(1.f - encoding); + double f_output = (double)UINT32_MAX * i_encoding; + uint32_t output = (uint32_t)f_output; + if (i_encoding < (double)0.25f && output > UINT32_MAX / 2) + output = 0; + if (i_encoding > (double)0.75f && output < UINT32_MAX / 2) + output = UINT32_MAX; + return output; } /** - * Test mapping function from [0, 1] float encoding value to [0, 2⁶⁴ − 1] integer output value. + * Test 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. + * @param encoding [0, 1] float encoding value + * @return [0, 2⁶⁴ − 1] integer output value */ -uint64_t invert_ramps64(float encoding) +uint64_t +invert_ramps64(float encoding) { - double i_encoding = (double)(1.f - encoding); - double f_output = ((double)UINT64_MAX) * i_encoding; - uint64_t output = (uint64_t)f_output; - if ((i_encoding < (double)(0.25f)) && (output > UINT64_MAX / 2)) - output = 0; - if ((i_encoding > (double)(0.75f)) && (output < UINT64_MAX / 2)) - output = UINT64_MAX; - return output; + double i_encoding = (double)(1.f - encoding); + double f_output = (double)UINT64_MAX * i_encoding; + uint64_t output = (uint64_t)f_output; + if (i_encoding < (double)0.25f && output > UINT64_MAX / 2) + output = 0; + if (i_encoding > (double)0.75f && output < UINT64_MAX / 2) + output = UINT64_MAX; + return output; } /** - * Test mapping function from [0, 1] float encoding value to [0, 1] float output value. + * Test 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. + * @param encoding [0, 1] float encoding value + * @return [0, 1] float output value */ -float invert_rampsf(float encoding) +float +invert_rampsf(float encoding) { - return 1.f - encoding; + return 1.f - encoding; } /** * Test mapping function from [0, 1] double precision float encoding - * value to [0, 1] double precision float output value. + * value to [0, 1] double precision float output value * - * @param encoding [0, 1] float encoding value. - * @return [0, 1] float output value. + * @param encoding [0, 1] float encoding value + * @return [0, 1] float output value */ -double invert_rampsd(double encoding) +double +invert_rampsd(double encoding) { - return ((double)1.f) - encoding; + return (double)1.f - encoding; } - diff --git a/src/test/ramps.h b/src/test/ramps.h index c16bbc8..13c31b1 100644 --- a/src/test/ramps.h +++ b/src/test/ramps.h @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #ifndef LIBGAMMA_TEST_RAMPS_H #define LIBGAMMA_TEST_RAMPS_H @@ -49,54 +33,53 @@ /** - * Test mapping function from [0, 1] float encoding value to [0, 2⁸ − 1] integer output value. + * Test 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. + * @param encoding [0, 1] float encoding value + * @return [0, 2⁸ − 1] integer output value */ uint8_t invert_ramps8(float encoding) __attribute__((const)); /** - * Test mapping function from [0, 1] float encoding value to [0, 2¹⁶ − 1] integer output value. + * Test 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. + * @param encoding [0, 1] float encoding value + * @return [0, 2¹⁶ − 1] integer output value */ uint16_t invert_ramps16(float encoding) __attribute__((const)); /** - * Test mapping function from [0, 1] float encoding value to [0, 2³² − 1] integer output value. + * Test 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. + * @param encoding [0, 1] float encoding value + * @return [0, 2³² − 1] integer output value */ uint32_t invert_ramps32(float encoding) __attribute__((const)); /** - * Test mapping function from [0, 1] float encoding value to [0, 2⁶⁴ − 1] integer output value. + * Test 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. + * @param encoding [0, 1] float encoding value + * @return [0, 2⁶⁴ − 1] integer output value */ uint64_t invert_ramps64(float encoding) __attribute__((const)); /** - * Test mapping function from [0, 1] float encoding value to [0, 1] float output value. + * Test 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. + * @param encoding [0, 1] float encoding value + * @return [0, 1] float output value */ float invert_rampsf(float encoding) __attribute__((const)); /** * Test mapping function from [0, 1] double precision float encoding - * value to [0, 1] double precision float output value. + * value to [0, 1] double precision float output value * - * @param encoding [0, 1] float encoding value. - * @return [0, 1] float output value. + * @param encoding [0, 1] float encoding value + * @return [0, 1] float output value */ double invert_rampsd(double encoding) __attribute__((const)); #endif - diff --git a/src/test/test.c b/src/test/test.c index d387a3c..8cf0579 100644 --- a/src/test/test.c +++ b/src/test/test.c @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #include "test.h" @@ -22,156 +6,152 @@ * Test `libgamma` * * @return Non-zero on machine detectable error, this library - * may still be faulty if zero is returned. + * may still be faulty if zero is returned */ -int main(void) +int +main(void) { - libgamma_site_state_t* restrict site_state = malloc(sizeof(libgamma_site_state_t)); - libgamma_partition_state_t* restrict part_state = malloc(sizeof(libgamma_partition_state_t)); - libgamma_crtc_state_t* restrict crtc_state = malloc(sizeof(libgamma_crtc_state_t)); - libgamma_crtc_information_t info; -#define X(R) \ - libgamma_gamma_##R##_t old_##R, R; \ - libgamma_gamma_##R##_fun* f_##R = invert_##R; - LIST_RAMPS + libgamma_site_state_t *restrict site_state = malloc(sizeof(libgamma_site_state_t)); + libgamma_partition_state_t *restrict part_state = malloc(sizeof(libgamma_partition_state_t)); + libgamma_crtc_state_t *restrict crtc_state = malloc(sizeof(libgamma_crtc_state_t)); + libgamma_crtc_information_t info; +#define X(R)\ + libgamma_gamma_##R##_t old_##R, R;\ + libgamma_gamma_##R##_fun* f_##R = invert_##R; + LIST_RAMPS #undef X - size_t i, n; - int r, rr = 0; - - /* Test miscellaneous parts of the library. */ - list_methods_lists(); - method_availability(); - list_default_sites(); - method_capabilities(); - error_test(); - - /* Select monitor for tests over CRTC:s, partitions and sites. */ - if (select_monitor(site_state, part_state, crtc_state)) - return 1; - - /* Test CRTC information functions. */ - crtc_information(crtc_state); - - /* Get the sizes of the gamma ramps for the selected CRTC. */ - libgamma_get_crtc_information(&info, crtc_state, LIBGAMMA_CRTC_INFO_GAMMA_SIZE); - - /* Create gamma ramps for each depth. */ -#define X(R) \ - old_##R.red_size = info.red_gamma_size; \ - old_##R.green_size = info.green_gamma_size; \ - old_##R.blue_size = info.blue_gamma_size; \ - R = old_##R; \ - libgamma_gamma_##R##_initialise(&old_##R); \ - libgamma_gamma_##R##_initialise(&R); - LIST_RAMPS + size_t i, n; + int r, rr = 0; + + /* Test miscellaneous parts of the library */ + list_methods_lists(); + method_availability(); + list_default_sites(); + method_capabilities(); + error_test(); + + /* Select monitor for tests over CRTC:s, partitions and sites */ + if (select_monitor(site_state, part_state, crtc_state)) + return 1; + + /* Test CRTC information functions. */ + crtc_information(crtc_state); + + /* Get the sizes of the gamma ramps for the selected CRTC */ + libgamma_get_crtc_information(&info, crtc_state, LIBGAMMA_CRTC_INFO_GAMMA_SIZE); + + /* Create gamma ramps for each depth */ +#define X(R)\ + old_##R.red_size = info.red_gamma_size;\ + old_##R.green_size = info.green_gamma_size;\ + old_##R.blue_size = info.blue_gamma_size;\ + R = old_##R;\ + libgamma_gamma_##R##_initialise(&old_##R);\ + libgamma_gamma_##R##_initialise(&R); + LIST_RAMPS #undef X - - /* Fill gamma ramps, for each depth, with the CRTC:s current ramps. */ -#define X(R) \ - if ((rr |= r = libgamma_crtc_get_gamma_##R(crtc_state, &old_##R))) \ - { \ - libgamma_perror("libgamma_crtc_get_gamma_" #R, r); \ - goto done; \ - } \ - if ((rr |= r = libgamma_crtc_get_gamma_##R(crtc_state, &R))) \ - { \ - libgamma_perror("libgamma_crtc_get_gamma_" #R, r); \ - goto done; \ - } - LIST_RAMPS + + /* Fill gamma ramps, for each depth, with the CRTC:s current ramps */ +#define X(R)\ + if ((rr |= r = libgamma_crtc_get_gamma_##R(crtc_state, &old_##R))) {\ + libgamma_perror("libgamma_crtc_get_gamma_" #R, r);\ + goto done;\ + }\ + if ((rr |= r = libgamma_crtc_get_gamma_##R(crtc_state, &R))) {\ + libgamma_perror("libgamma_crtc_get_gamma_" #R, r);\ + goto done;\ + } + LIST_RAMPS #undef X - /* Test function assisted gamma ramps setting. */ -#define X(R) \ - /* Dim the monitor for one second and the restore it. */ \ - printf("Inverting monitor output for 1 second... (" #R ")\n"); \ - if ((rr |= r = libgamma_crtc_set_gamma_##R##_f(crtc_state, \ - f_##R, f_##R, f_##R))) \ - libgamma_perror("libgamma_crtc_set_gamma_" #R "_f", r); \ - sleep(1); \ - if ((rr |= r = libgamma_crtc_set_gamma_##R(crtc_state, old_##R))) \ - libgamma_perror("libgamma_crtc_set_gamma_" #R, r); \ - printf("Done!\n"); \ - /* Sleep for one second, we have more depths to test. */ \ - printf("Sleeping for 1 second...\n"); \ - sleep(1); - LIST_RAMPS + /* Test function assisted gamma ramps setting */ +#define X(R)\ + /* Dim the monitor for one second and the restore it. */\ + printf("Inverting monitor output for 1 second... (" #R ")\n");\ + if ((rr |= r = libgamma_crtc_set_gamma_##R##_f(crtc_state, f_##R, f_##R, f_##R)))\ + libgamma_perror("libgamma_crtc_set_gamma_" #R "_f", r);\ + sleep(1);\ + if ((rr |= r = libgamma_crtc_set_gamma_##R(crtc_state, old_##R)))\ + libgamma_perror("libgamma_crtc_set_gamma_" #R, r);\ + printf("Done!\n");\ + /* Sleep for one second, we have more depths to test */\ + printf("Sleeping for 1 second...\n");\ + sleep(1); + LIST_RAMPS #undef X - - /* Test getting and setting gamma ramps. */ -#define X(R) \ - /* Get the grand size of the gamma ramps. */ \ - n = R.red_size; \ - n = n > R.green_size ? n : R.green_size; \ - n = n > R.blue_size ? n : R.blue_size; \ - /* Print the current gamma ramps. */ \ - printf("Current gamma ramps (" #R "):\n"); \ - for (i = 0; i < n; i++) \ - { \ - if (i < R.red_size) Y(R.red, "1"); else printf(" "); \ - if (i < R.green_size) Y(R.green, "2"); else printf(" "); \ - if (i < R.blue_size) Y(R.blue, "4"); else printf(" "); \ - printf("\n"); \ - } \ - printf("\n"); \ - /* Adjust the gamma ramps for dimming the monitor. */ \ - for (i = 0; i < R.red_size + R.green_size + R.blue_size; i++) \ - R.red[i] /= 2; \ - /* Dim the monitor for one second and the restore it. */ \ - printf("Dimming monitor for 1 second...\n"); \ - if ((rr |= r = libgamma_crtc_set_gamma_##R(crtc_state, R))) \ - libgamma_perror("libgamma_crtc_set_gamma_" #R, r); \ - sleep(1); \ - if ((rr |= r = libgamma_crtc_set_gamma_##R(crtc_state, old_##R))) \ - libgamma_perror("libgamma_crtc_set_gamma_" #R, r); \ - printf("Done!\n"); \ - /* Sleep for one second, we have more depths to test. */ \ - printf("Sleeping for 1 second...\n"); \ - sleep(1); + + /* Test getting and setting gamma ramps */ +#define X(R)\ + /* Get the grand size of the gamma ramps */\ + n = R.red_size;\ + n = n > R.green_size ? n : R.green_size;\ + n = n > R.blue_size ? n : R.blue_size;\ + /* Print the current gamma ramps */\ + printf("Current gamma ramps (" #R "):\n");\ + for (i = 0; i < n; i++) {\ + if (i < R.red_size) Y(R.red, "1"); else printf(" ");\ + if (i < R.green_size) Y(R.green, "2"); else printf(" ");\ + if (i < R.blue_size) Y(R.blue, "4"); else printf(" ");\ + printf("\n");\ + }\ + printf("\n");\ + /* Adjust the gamma ramps for dimming the monitor */\ + for (i = 0; i < R.red_size + R.green_size + R.blue_size; i++)\ + R.red[i] /= 2;\ + /* Dim the monitor for one second and the restore it */\ + printf("Dimming monitor for 1 second...\n");\ + if ((rr |= r = libgamma_crtc_set_gamma_##R(crtc_state, R)))\ + libgamma_perror("libgamma_crtc_set_gamma_" #R, r);\ + sleep(1);\ + if ((rr |= r = libgamma_crtc_set_gamma_##R(crtc_state, old_##R)))\ + libgamma_perror("libgamma_crtc_set_gamma_" #R, r);\ + printf("Done!\n");\ + /* Sleep for one second, we have more depths to test */\ + printf("Sleeping for 1 second...\n");\ + sleep(1); #define Y(R, C) printf(" \033[3" C "m%1.8lf\033[00m", (double)(R[i])) - LIST_FLOAT_RAMPS + LIST_FLOAT_RAMPS #undef Y #define Y(R, C) printf(" \033[3" C "m%16llX\033[00m", (uint64_t)(R[i])) - LIST_INTEGER_RAMPS + LIST_INTEGER_RAMPS #undef Y #undef X + + /* Test order of gamma ramps */ + memcpy(ramps16.red, old_ramps16.red, ramps16.red_size * sizeof(uint16_t)); + memset(ramps16.green, 0, ramps16.green_size * sizeof(uint16_t)); + memset(ramps16.blue, 0, ramps16.blue_size * sizeof(uint16_t)); + printf("Making the monitor red-only for 1 second...\n"); + if ((rr |= r = libgamma_crtc_set_gamma_ramps16(crtc_state, ramps16))) + libgamma_perror("libgamma_crtc_set_gamma_ramps16", r); + sleep(1); + memset(ramps16.red, 0, ramps16.red_size * sizeof(uint16_t)); + memcpy(ramps16.green, old_ramps16.green, ramps16.green_size * sizeof(uint16_t)); + printf("Making the monitor green-only for 1 second...\n"); + if ((rr |= r = libgamma_crtc_set_gamma_ramps16(crtc_state, ramps16))) + libgamma_perror("libgamma_crtc_set_gamma_ramps16", r); + sleep(1); + memset(ramps16.green, 0, ramps16.green_size * sizeof(uint16_t)); + memcpy(ramps16.blue, old_ramps16.blue, ramps16.blue_size * sizeof(uint16_t)); + printf("Making the monitor green-only for 1 second...\n"); + if ((rr |= r = libgamma_crtc_set_gamma_ramps16(crtc_state, ramps16))) + libgamma_perror("libgamma_crtc_set_gamma_ramps16", r); + sleep(1); + if ((rr |= r = libgamma_crtc_set_gamma_ramps64(crtc_state, old_ramps64))) + libgamma_perror("libgamma_crtc_set_gamma_ramps64", r); + printf("Done!\n"); + + /* TODO Test gamma ramp restore functions */ - /* Test order of gamma ramps. */ - memcpy(ramps16.red, old_ramps16.red, ramps16.red_size * sizeof(uint16_t)); - memset(ramps16.green, 0, ramps16.green_size * sizeof(uint16_t)); - memset(ramps16.blue, 0, ramps16.blue_size * sizeof(uint16_t)); - printf("Making the monitor red-only for 1 second...\n"); - if ((rr |= r = libgamma_crtc_set_gamma_ramps16(crtc_state, ramps16))) - libgamma_perror("libgamma_crtc_set_gamma_ramps16", r); - sleep(1); - memset(ramps16.red, 0, ramps16.red_size * sizeof(uint16_t)); - memcpy(ramps16.green, old_ramps16.green, ramps16.green_size * sizeof(uint16_t)); - printf("Making the monitor green-only for 1 second...\n"); - if ((rr |= r = libgamma_crtc_set_gamma_ramps16(crtc_state, ramps16))) - libgamma_perror("libgamma_crtc_set_gamma_ramps16", r); - sleep(1); - memset(ramps16.green, 0, ramps16.green_size * sizeof(uint16_t)); - memcpy(ramps16.blue, old_ramps16.blue, ramps16.blue_size * sizeof(uint16_t)); - printf("Making the monitor green-only for 1 second...\n"); - if ((rr |= r = libgamma_crtc_set_gamma_ramps16(crtc_state, ramps16))) - libgamma_perror("libgamma_crtc_set_gamma_ramps16", r); - sleep(1); - if ((rr |= r = libgamma_crtc_set_gamma_ramps64(crtc_state, old_ramps64))) - libgamma_perror("libgamma_crtc_set_gamma_ramps64", r); - printf("Done!\n"); - - /* TODO Test gamma ramp restore functions. */ - - done: - /* Release resouces. */ -#define X(R) \ - libgamma_gamma_##R##_destroy(&R); \ - libgamma_gamma_##R##_destroy(&old_##R); - LIST_RAMPS +done: + /* Release resouces */ +#define X(R)\ + libgamma_gamma_##R##_destroy(&R);\ + libgamma_gamma_##R##_destroy(&old_##R); + LIST_RAMPS #undef X - libgamma_crtc_free(crtc_state); - libgamma_partition_free(part_state); - libgamma_site_free(site_state); - return rr; + libgamma_crtc_free(crtc_state); + libgamma_partition_free(part_state); + libgamma_site_free(site_state); + return rr; } - diff --git a/src/test/test.h b/src/test/test.h index 0c22fe0..f8fa9f5 100644 --- a/src/test/test.h +++ b/src/test/test.h @@ -1,24 +1,7 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #ifndef LIBGAMMA_TEST_TEST_H #define LIBGAMMA_TEST_TEST_H - #include "update-warnings.h" #include "methods.h" #include "errors.h" @@ -34,6 +17,4 @@ #include <stdint.h> #include <string.h> - #endif - diff --git a/src/test/update-warnings.h b/src/test/update-warnings.h index 01888cb..4a1b027 100644 --- a/src/test/update-warnings.h +++ b/src/test/update-warnings.h @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #ifndef LIBGAMMA_TEST_UPDATE_WARNINGS_H #define LIBGAMMA_TEST_UPDATE_WARNINGS_H @@ -47,23 +31,22 @@ # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wcpp" # if LIBGAMMA_ERROR_MIN < -46 -# warning New error codes have been added to libgamma. +# warning New error codes have been added to libgamma # endif # if LIBGAMMA_METHOD_COUNT > 6 # warning New adjust methods has been added to libgamma # endif # if LIBGAMMA_CONNECTOR_TYPE_COUNT > 20 -# warning New connector types have been added to libgamma. +# warning New connector types have been added to libgamma # endif # if LIBGAMMA_SUBPIXEL_ORDER_COUNT > 6 -# warning New subpixel orders have been added to libgamma. +# warning New subpixel orders have been added to libgamma # endif # if LIBGAMMA_CRTC_INFO_COUNT > 13 -# warning New CRTC information fields have been added to libgamma. +# warning New CRTC information fields have been added to libgamma # endif # pragma GCC diagnostic pop #endif #endif - diff --git a/src/test/user.c b/src/test/user.c index 4f2b6d5..7a1fd78 100644 --- a/src/test/user.c +++ b/src/test/user.c @@ -1,161 +1,143 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #include "user.h" #ifdef __GCC__ # include <string.h> #else -/* Hack to circumvent that the funcions are defined multiples. */ +/* Hack to circumvent that the funcions are defined multiples */ # define strlen strlen_ # define memcpy memcpy_ # define strchr strchr_ -static size_t strlen_(const char* str) -{ - size_t n = 0; - while (str[n]) - n++; - return n; +static size_t +strlen_(const char *str) { + size_t n = 0; + while (str[n]) + n++; + return n; } -static void* memcpy_(void* dest, const void* src, size_t n) +static void * +memcpy_(void *restrict dest, const void *restrict src, size_t n) { - char* restrict d = dest; - const char* restrict s = src; - size_t i; - for (i = 0; i < n; i++) - d[i] = s[i]; - return dest; + char *restrict d = dest; + const char *restrict s = src; + size_t i; + for (i = 0; i < n; i++) + d[i] = s[i]; + return dest; } -static char* strchr_(const char* s, int c) +static char *strchr_(const char *s, int c) { - char find = (char)c; - while (*s) - if (*s == find) - return s; - else - s++; - return NULL; + char find = (char)c; + for (; *s; s++) + if (*s == find) + return s; + return NULL; } #endif /** - * Let the user select adjustment method, site, partition and CRTC. + * Let the user select adjustment method, site, partition and CRTC * - * @param site_state Output slot for the site. - * @param part_state Output slot for the partition. - * @param crtc_state Output slot for the CRTC. - * @return Zero on and only on success. + * @param site_state Output slot for the site + * @param part_state Output slot for the partition + * @param crtc_state Output slot for the CRTC + * @return Zero on and only on success */ -int select_monitor(libgamma_site_state_t* restrict site_state, - libgamma_partition_state_t* restrict part_state, - libgamma_crtc_state_t* restrict crtc_state) +int +select_monitor(libgamma_site_state_t *restrict site_state, + libgamma_partition_state_t *restrict part_state, + libgamma_crtc_state_t *restrict crtc_state) { - int method; - char* site; - char* tmp; - char buf[256]; - int r; - - - /* -- Adjustment method -- */ - - /* Let the user select adjustment method. */ - printf("Select adjustment method:\n"); - for (method = 0; method < LIBGAMMA_METHOD_COUNT; method++) - printf(" %i: %s\n", method, method_name(method)); - printf("> "); - fflush(stdout); - fgets(buf, sizeof(buf) / sizeof(char), stdin); - method = atoi(buf); - - - /* -- Site -- */ - - /* Let the user select site. */ - printf("Select site: "); - fflush(stdout); - fgets(buf, sizeof(buf) / sizeof(char), stdin); - tmp = strchr(buf, '\n'); - if (tmp != NULL) - *tmp = '\0'; - if (buf[0] == '\0') - site = NULL; - else - { - site = malloc((strlen(buf) + 1) * sizeof(char)); - memcpy(site, buf, strlen(buf) + 1); - } - - /* Initialise site state. */ - if ((r = libgamma_site_initialise(site_state, method, site))) - { - free(site); - return libgamma_perror("error", r), 1; - } - - - /* -- Partition -- */ - - /* Check that the site has at least one partition. */ - if (site_state->partitions_available == 0) - { - libgamma_site_free(site_state); - return printf("No partitions found\n"), 1; - } - - /* Let the user select partition. */ - printf("Select partition [0, %lu]: ", site_state->partitions_available - 1); - fflush(stdout); - fgets(buf, sizeof(buf) / sizeof(char), stdin); - - /* Initialise partition state. */ - if ((r = libgamma_partition_initialise(part_state, site_state, (size_t)atoll(buf)))) - { - libgamma_site_free(site_state); - return libgamma_perror("error", r), 1; - } - - - /* -- CRTC -- */ - - /* Check that the partition has at least one CRTC. */ - if (part_state->crtcs_available == 0) - { - libgamma_partition_free(part_state); - libgamma_site_free(site_state); - return printf("No CRTC:s found\n"), 1; - } - - /* Let the user select CRTC. */ - printf("Select CRTC [0, %lu]: ", part_state->crtcs_available - 1); - fflush(stdout); - fgets(buf, sizeof(buf) / sizeof(char), stdin); - - /* Initialise CRTC state. */ - if ((r = libgamma_crtc_initialise(crtc_state, part_state, (size_t)atoll(buf)))) - { - libgamma_partition_free(part_state); - libgamma_site_free(site_state); - return libgamma_perror("error", r), 1; - } - - printf("\n"); - return 0; -} + int method; + char *site; + char *tmp; + char buf[256]; + int r; + + + /* -- Adjustment method -- */ + + /* Let the user select adjustment method */ + printf("Select adjustment method:\n"); + for (method = 0; method < LIBGAMMA_METHOD_COUNT; method++) + printf(" %i: %s\n", method, method_name(method)); + printf("> "); + fflush(stdout); + fgets(buf, sizeof(buf) / sizeof(char), stdin); + method = atoi(buf); + + + /* -- Site -- */ + + /* Let the user select site */ + printf("Select site: "); + fflush(stdout); + fgets(buf, sizeof(buf) / sizeof(char), stdin); + tmp = strchr(buf, '\n'); + if (tmp) + *tmp = '\0'; + if (!buf[0]) { + site = NULL; + } else { + site = malloc((strlen(buf) + 1) * sizeof(char)); + memcpy(site, buf, strlen(buf) + 1); + } + /* Initialise site state */ + if ((r = libgamma_site_initialise(site_state, method, site))) { + free(site); + libgamma_perror("error", r); + return 1; + } + + + /* -- Partition -- */ + + /* Check that the site has at least one partition */ + if (!site_state->partitions_available) { + libgamma_site_free(site_state); + printf("No partitions found\n"); + return 1; + } + + /* Let the user select partition */ + printf("Select partition [0, %lu]: ", site_state->partitions_available - 1); + fflush(stdout); + fgets(buf, sizeof(buf) / sizeof(char), stdin); + + /* Initialise partition state */ + if ((r = libgamma_partition_initialise(part_state, site_state, (size_t)atoll(buf)))) { + libgamma_site_free(site_state); + libgamma_perror("error", r); + return 1; + } + + + /* -- CRTC -- */ + + /* Check that the partition has at least one CRTC */ + if (!part_state->crtcs_available) { + libgamma_partition_free(part_state); + libgamma_site_free(site_state); + printf("No CRTC:s found\n"); + return 1; + } + + /* Let the user select CRTC */ + printf("Select CRTC [0, %lu]: ", part_state->crtcs_available - 1); + fflush(stdout); + fgets(buf, sizeof(buf) / sizeof(char), stdin); + + /* Initialise CRTC state. */ + if ((r = libgamma_crtc_initialise(crtc_state, part_state, (size_t)atoll(buf)))) { + libgamma_partition_free(part_state); + libgamma_site_free(site_state); + libgamma_perror("error", r); + return 1; + } + + printf("\n"); + return 0; +} diff --git a/src/test/user.h b/src/test/user.h index e96142d..5a0c868 100644 --- a/src/test/user.h +++ b/src/test/user.h @@ -1,20 +1,4 @@ -/** - * libgamma -- Display server abstraction layer for gamma ramp adjustments - * Copyright (C) 2014, 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ +/* See LICENSE file for copyright and license details. */ #ifndef LIBGAMMA_TEST_USER_H #define LIBGAMMA_TEST_USER_H @@ -29,17 +13,16 @@ /** - * Let the user select adjustment method, site, partition and CRTC. + * Let the user select adjustment method, site, partition and CRTC * - * @param site_state Output slot for the site. - * @param part_state Output slot for the partition. - * @param crtc_state Output slot for the CRTC. - * @return Zero on and only on success. + * @param site_state Output slot for the site + * @param part_state Output slot for the partition + * @param crtc_state Output slot for the CRTC + * @return Zero on and only on success */ -int select_monitor(libgamma_site_state_t* restrict site_state, - libgamma_partition_state_t* restrict part_state, - libgamma_crtc_state_t* restrict crtc_state); +int select_monitor(libgamma_site_state_t *restrict site_state, + libgamma_partition_state_t *restrict part_state, + libgamma_crtc_state_t *restrict crtc_state); #endif - |