diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/fake-quartz-cg.c | 314 | ||||
-rw-r--r-- | src/fake-quartz-cg.h | 70 | ||||
-rw-r--r-- | src/fake-w32-gdi.c | 6 | ||||
-rw-r--r-- | src/fake-w32-gdi.h | 2 |
4 files changed, 391 insertions, 1 deletions
diff --git a/src/fake-quartz-cg.c b/src/fake-quartz-cg.c new file mode 100644 index 0000000..b07558b --- /dev/null +++ b/src/fake-quartz-cg.c @@ -0,0 +1,314 @@ +/** + * libgamma — Display server abstraction layer for gamma ramp adjustments + * Copyright © 2014 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/>. + */ +#ifndef FAKE_GAMMA_METHOD_QUARTZ_CORE_GRAPHICS +# error Compiling fake-quartz-cg.c without FAKE_GAMMA_METHOD_QUARTZ_CORE_GRAPHICS +#endif + +/* This file very sloppily translates Mac OS X Quartz calls to X RandR calls. + * It should by no means be used, without additional modification, as a + * part of a compatibility layer. The purpose of this file is only to make + * it possible to test for logical errors in Max OS X specific code on + * a GNU/Linux system under X. */ + + +#include "fake-quartz-cg.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + + +#ifndef HAVE_GAMMA_METHOD_X_RANDR + +CGError +CGGetOnlineDisplayList(uint32_t max_size, CGDirectDisplayID* displays_out, uint32_t* count_out) +{ + uint32_t i; + for (i = 0; (i < max_size) && (i < 2); i++) + displays_out[i] = (CGDirectDisplayID)i; + *count_out = i; + + return kCGErrorSuccess; +} + +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; + + if (gamma_size != 256) + { + fprintf(stderr, "Gamma size should be 256.\n"); + abort(); + } + + return kCGErrorSuccess; +} + +CGError +CGGetDisplayTransferByTable(CGDirectDisplayID display, uint32_t gamma_size, CGGammaValue* red, + CGGammaValue* green, CGGammaValue* blue, uint32_t* gamma_size_out) +{ + long i; + (void) display; + + if (gamma_size != 256) + { + fprintf(stderr, "Gamma size should be 256.\n"); + abort(); + } + + *gamma_size_out = 256; + + for (i = 0; i < 256; i++) + red[i] = green[i] = blue[i] = (CGGammaValue)i / 255; + + return kCGErrorSuccess; +} + +void +CGDisplayRestoreColorSyncSettings(void) +{ + /* Do nothing. */ +} + +uint32_t +CGDisplayGammaTableCapacity(CGDirectDisplayID display) +{ + (void) display; + return 256; +} + +void +close_fake_quartz(void) +{ + /* Do nothing. */ +} + + +#else + +#include <xcb/xcb.h> +#include <xcb/randr.h> + + + +static xcb_connection_t* connection = NULL; +static xcb_randr_get_screen_resources_current_reply_t* res_reply = NULL; +static uint32_t crtc_count = 0; +static xcb_randr_crtc_t* crtcs = NULL; +static uint16_t* original_ramps = NULL; + + + +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Waggregate-return" + +CGError CGGetOnlineDisplayList(uint32_t max_size, CGDirectDisplayID* displays_out, uint32_t* count_out) +{ + uint32_t i; + + 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* gamma_reply; + + connection = xcb_connect(NULL, NULL); + + iter = xcb_setup_roots_iterator(xcb_get_setup(connection)); + 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; + } + + crtc_count = (uint32_t)(res_reply->num_crtcs); + crtcs = xcb_randr_get_screen_resources_current_crtcs(res_reply); + + original_ramps = malloc(crtc_count * 3 * 256 * sizeof(uint16_t)); + if (original_ramps == NULL) + { + perror("malloc"); + xcb_disconnect(connection); + crtc_count = 0; + return ~kCGErrorSuccess; + } + + for (i = 0; i < crtc_count; i++) + { + 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; + } + +#define __DEST(C) original_ramps + (C + 3 * i) * 256 +#define __SRC(C) xcb_randr_get_crtc_gamma_##C(gamma_reply) + memcpy(__DEST(0), __SRC(red), 256 * sizeof(uint16_t)); + memcpy(__DEST(1), __SRC(green), 256 * sizeof(uint16_t)); + memcpy(__DEST(2), __SRC(blue), 256 * sizeof(uint16_t)); +#undef __SRC +#undef __DEST + + free(gamma_reply); + } + } + + for (i = 0; (i < max_size) && (i < crtc_count); i++) + *(displays_out + i) = (CGDirectDisplayID)i; + + *count_out = i; + return kCGErrorSuccess; +} + + +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; + + if (gamma_size != 256) + { + fprintf(stderr, "Gamma size should be 256.\n"); + abort(); + } + + for (i = 0; i < 256; i++) + { + v = (int32_t)(red[i] * UINT16_MAX); + r_int[i] = (uint16_t)(v < 0 ? 0 : v > UINT16_MAX ? UINT16_MAX : v); + + v = (int32_t)(green[i] * UINT16_MAX); + g_int[i] = (uint16_t)(v < 0 ? 0 : v > UINT16_MAX ? UINT16_MAX : v); + + v = (int32_t)(blue[i] * UINT16_MAX); + b_int[i] = (uint16_t)(v < 0 ? 0 : v > UINT16_MAX ? UINT16_MAX : v); + } + + gamma_cookie = xcb_randr_set_crtc_gamma_checked(connection, crtcs[display], + (uint16_t)gamma_size, r_int, g_int, b_int); + return xcb_request_check(connection, gamma_cookie) == NULL ? kCGErrorSuccess : ~kCGErrorSuccess; +} + + +CGError CGGetDisplayTransferByTable(CGDirectDisplayID display, uint32_t gamma_size, CGGammaValue* red, + CGGammaValue* green, CGGammaValue* blue, uint32_t* gamma_size_out) +{ + xcb_randr_get_crtc_gamma_cookie_t gamma_cookie; + xcb_randr_get_crtc_gamma_reply_t* gamma_reply; + xcb_generic_error_t* error; + uint16_t* r_int; + uint16_t* g_int; + uint16_t* b_int; + long i; + + if (gamma_size != 256) + { + fprintf(stderr, "Gamma size should be 256.\n"); + abort(); + } + + *gamma_size_out = 256; + + 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; + } + + 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); + + 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; +} + + +void CGDisplayRestoreColorSyncSettings(void) +{ + xcb_generic_error_t* error; + xcb_void_cookie_t gamma_cookie; + uint32_t i; + + for (i = 0; i < crtc_count; i++) + { + 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); + } +} + + +uint32_t CGDisplayGammaTableCapacity(CGDirectDisplayID display) +{ + (void) display; + return 256; +} + +# pragma GCC diagnostic pop + + +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; +} + +#endif + diff --git a/src/fake-quartz-cg.h b/src/fake-quartz-cg.h new file mode 100644 index 0000000..f1d8e14 --- /dev/null +++ b/src/fake-quartz-cg.h @@ -0,0 +1,70 @@ +/** + * libgamma — Display server abstraction layer for gamma ramp adjustments + * Copyright © 2014 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/>. + */ +#ifndef LIBGAMMA_FAKE_QUARTZ_CG_H +#define LIBGAMMA_FAKE_QUARTZ_CG_H + +#ifndef FAKE_GAMMA_METHOD_QUARTZ_CORE_GRAPHICS +# error Including fake-quartz-cg.h without FAKE_GAMMA_METHOD_QUARTZ_CORE_GRAPHICS +#endif + + +/* This header file contains some capabilities of + * <CoreGraphics/CGDirectDisplay.h> and <CoreGraphics/CGError.h>, + * and can be used modify gamma ramps without Mac OS X and Quartz + * but with its API. + * + * The content of this file is based on the documentation found on: + * + * https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/Quartz_Services_Ref/Reference/reference.html + * + * https://developer.apple.com/library/mac/documentation/CoreGraphics/Reference/CoreGraphicsConstantsRef/Reference/reference.html#//apple_ref/c/tdef/CGError */ + + +#include <stdint.h> + + +typedef int32_t CGError; +#define kCGErrorSuccess 0 + +typedef float CGGammaValue; +typedef uint32_t CGDirectDisplayID; + + +CGError CGGetOnlineDisplayList(uint32_t max_size, CGDirectDisplayID* displays_out, uint32_t* count_out); + +CGError CGSetDisplayTransferByTable(CGDirectDisplayID display, uint32_t gamma_size, const CGGammaValue* red, + const CGGammaValue* green, const CGGammaValue* blue); + +CGError CGGetDisplayTransferByTable(CGDirectDisplayID display, uint32_t gamma_size, CGGammaValue* red, + CGGammaValue* green, CGGammaValue* blue, uint32_t* gamma_size_out); + +void CGDisplayRestoreColorSyncSettings(void); + +uint32_t CGDisplayGammaTableCapacity(CGDirectDisplayID display) __attribute__((const)); + + +/* The follow part most only be used when this module is used, + * it cannot be used when the real CoreGraphics is used. + * CoreGraphics does not have this function, it is added so + * that there is a way to cleanly close the X connection + * and free resources needed by this module. */ +void close_fake_quartz_cg(void); + + +#endif + diff --git a/src/fake-w32-gdi.c b/src/fake-w32-gdi.c index 048b64d..66fa91f 100644 --- a/src/fake-w32-gdi.c +++ b/src/fake-w32-gdi.c @@ -19,6 +19,12 @@ # error Compiling fake-w32-gdi.c without FAKE_GAMMA_METHOD_W32_GDI #endif +/* This file very sloppily translates Windows GDI calls to X RandR calls. + * It should by no means be used, without additional modification, as a + * part of a compatibility layer. The purpose of this file is only to make + * it possible to test for logical errors in Windows specific code on + * a GNU/Linux system under X. */ + #include "fake-w32-gdi.h" diff --git a/src/fake-w32-gdi.h b/src/fake-w32-gdi.h index 0648b76..85fcfc9 100644 --- a/src/fake-w32-gdi.h +++ b/src/fake-w32-gdi.h @@ -18,7 +18,7 @@ #ifndef LIBGAMMA_FAKE_W32_GDI_H #define LIBGAMMA_FAKE_W32_GDI_H -#ifndef HAVE_GAMMA_METHOD_W32_GDI +#ifndef FAKE_GAMMA_METHOD_W32_GDI # error Including fake-w32-gdi.h without FAKE_GAMMA_METHOD_W32_GDI #endif |