From 0ce0d8d6e0c420ccafa79e0203b928c3559a4311 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 5 Mar 2021 00:43:38 +0100 Subject: Split source files, merge public header files, eliminite use gpp, rewrite makefile MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- .gitignore | 32 +- DEPENDENCIES | 50 +- Makefile | 484 +++--- TODO | 4 - common.h | 352 +++++ config.mk | 6 + configure | 211 --- debug.mk | 8 + fake-quartz-cg.c | 447 ++++++ fake-quartz-cg.h | 120 ++ fake-w32-gdi.c | 479 ++++++ fake-w32-gdi.h | 248 +++ gamma-dummy.c.gpp | 993 ++++++++++++ gamma-dummy.h | 236 +++ gamma-linux-drm.c | 902 +++++++++++ gamma-linux-drm.h | 132 ++ gamma-quartz-cg.c | 359 +++++ gamma-quartz-cg.h | 133 ++ gamma-w32-gdi.c | 360 +++++ gamma-w32-gdi.h | 132 ++ gamma-x-randr.c | 985 ++++++++++++ gamma-x-randr.h | 132 ++ gamma-x-vidmode.c | 312 ++++ gamma-x-vidmode.h | 133 ++ get_ramps.h | 30 + libgamma.h | 2474 ++++++++++++++++++++++++++++++ libgamma_behex_edid.c | 13 + libgamma_behex_edid_lowercase.c | 33 + libgamma_behex_edid_uppercase.c | 33 + libgamma_connector_type_count.c | 9 + libgamma_const_of_connector_type.c | 25 + libgamma_const_of_method.c | 24 + libgamma_const_of_subpixel_order.c | 25 + libgamma_crtc_destroy.c | 23 + libgamma_crtc_free.c | 11 + libgamma_crtc_get_gamma_ramps16.c | 21 + libgamma_crtc_get_gamma_ramps32.c | 21 + libgamma_crtc_get_gamma_ramps64.c | 21 + libgamma_crtc_get_gamma_ramps8.c | 21 + libgamma_crtc_get_gamma_rampsd.c | 21 + libgamma_crtc_get_gamma_rampsf.c | 21 + libgamma_crtc_info_count.c | 12 + libgamma_crtc_information_destroy.c | 15 + libgamma_crtc_information_free.c | 11 + libgamma_crtc_initialise.c | 29 + libgamma_crtc_restore.c | 24 + libgamma_crtc_set_gamma_ramps16.c | 21 + libgamma_crtc_set_gamma_ramps16_f.c | 25 + libgamma_crtc_set_gamma_ramps32.c | 21 + libgamma_crtc_set_gamma_ramps32_f.c | 25 + libgamma_crtc_set_gamma_ramps64.c | 21 + libgamma_crtc_set_gamma_ramps64_f.c | 25 + libgamma_crtc_set_gamma_ramps8.c | 21 + libgamma_crtc_set_gamma_ramps8_f.c | 25 + libgamma_crtc_set_gamma_rampsd.c | 21 + libgamma_crtc_set_gamma_rampsd_f.c | 25 + libgamma_crtc_set_gamma_rampsf.c | 21 + libgamma_crtc_set_gamma_rampsf_f.c | 25 + libgamma_error_min.c | 10 + libgamma_gamma_ramps16_destroy.c | 16 + libgamma_gamma_ramps16_free.c | 18 + libgamma_gamma_ramps16_initialise.c | 28 + libgamma_gamma_ramps32_destroy.c | 16 + libgamma_gamma_ramps32_free.c | 18 + libgamma_gamma_ramps32_initialise.c | 23 + libgamma_gamma_ramps64_destroy.c | 16 + libgamma_gamma_ramps64_free.c | 18 + libgamma_gamma_ramps64_initialise.c | 23 + libgamma_gamma_ramps8_destroy.c | 16 + libgamma_gamma_ramps8_free.c | 18 + libgamma_gamma_ramps8_initialise.c | 23 + libgamma_gamma_rampsd_destroy.c | 16 + libgamma_gamma_rampsd_free.c | 18 + libgamma_gamma_rampsd_initialise.c | 23 + libgamma_gamma_rampsf_destroy.c | 16 + libgamma_gamma_rampsf_free.c | 18 + libgamma_gamma_rampsf_initialise.c | 23 + libgamma_get_crtc_information.c | 29 + libgamma_group_gid.c | 37 + libgamma_group_name.c | 44 + libgamma_internal_allocated_any_ramp.c | 60 + libgamma_internal_parse_edid.c | 99 ++ libgamma_internal_translate_from_64.c | 44 + libgamma_internal_translate_to_64.c | 184 +++ libgamma_internal_translated_ramp_get_.c | 60 + libgamma_internal_translated_ramp_set_.c | 57 + libgamma_is_method_available.c | 24 + libgamma_list_methods.c | 135 ++ libgamma_method_capabilities.c | 28 + libgamma_method_count.c | 13 + libgamma_method_default_site.c | 31 + libgamma_method_default_site_variable.c | 25 + libgamma_name_of_connector_type.c | 28 + libgamma_name_of_error.c | 23 + libgamma_name_of_method.c | 23 + libgamma_name_of_subpixel_order.c | 27 + libgamma_partition_destroy.c | 23 + libgamma_partition_free.c | 11 + libgamma_partition_initialise.c | 29 + libgamma_partition_restore.c | 24 + libgamma_perror.c | 40 + libgamma_site_destroy.c | 24 + libgamma_site_free.c | 11 + libgamma_site_initialise.c | 34 + libgamma_site_restore.c | 24 + libgamma_strerror.c | 32 + libgamma_strerror_r.c | 61 + libgamma_subpixel_order_count.c | 9 + libgamma_unhex_edid.c | 55 + libgamma_value_of_connector_type.c | 29 + libgamma_value_of_error.c | 23 + libgamma_value_of_method.c | 28 + libgamma_value_of_subpixel_order.c | 29 + mk/linux.mk | 8 + mk/macos.mk | 6 + mk/method-dummy=no.mk | 0 mk/method-dummy=yes.mk | 3 + mk/method-linux-drm=no.mk | 0 mk/method-linux-drm=yes.mk | 5 + mk/method-quartz-cg=fake.mk | 3 + mk/method-quartz-cg=no.mk | 0 mk/method-quartz-cg=yes.mk | 8 + mk/method-w32-gdi=fake.mk | 3 + mk/method-w32-gdi=no.mk | 0 mk/method-w32-gdi=yes.mk | 3 + mk/method-x-randr=no.mk | 0 mk/method-x-randr=yes.mk | 5 + mk/method-x-vidmode=no.mk | 0 mk/method-x-vidmode=yes.mk | 5 + mk/windows.mk | 6 + set_ramps.h | 25 + set_ramps_fun.h | 50 + src/lib/edid.c | 105 -- src/lib/edid.h | 22 - src/lib/fake-quartz-cg.c | 447 ------ src/lib/fake-quartz-cg.h | 120 -- src/lib/fake-w32-gdi.c | 479 ------ src/lib/fake-w32-gdi.h | 248 --- src/lib/gamma-dummy.c.gpp | 993 ------------ src/lib/gamma-dummy.h | 243 --- src/lib/gamma-helper.c | 372 ----- src/lib/gamma-helper.h | 144 -- src/lib/gamma-linux-drm.c | 902 ----------- src/lib/gamma-linux-drm.h | 139 -- src/lib/gamma-quartz-cg.c | 359 ----- src/lib/gamma-quartz-cg.h | 140 -- src/lib/gamma-w32-gdi.c | 360 ----- src/lib/gamma-w32-gdi.h | 139 -- src/lib/gamma-x-randr.c | 985 ------------ src/lib/gamma-x-randr.h | 139 -- src/lib/gamma-x-vidmode.c | 312 ---- src/lib/gamma-x-vidmode.h | 140 -- src/lib/libgamma-error.c.gpp | 178 --- src/lib/libgamma-error.h | 379 ----- src/lib/libgamma-facade.c.gpp | 1097 ------------- src/lib/libgamma-facade.h | 537 ------- src/lib/libgamma-method.c | 324 ---- src/lib/libgamma-method.h | 1323 ---------------- src/lib/libgamma.h | 11 - 159 files changed, 11809 insertions(+), 11215 deletions(-) create mode 100644 common.h create mode 100644 config.mk delete mode 100755 configure create mode 100644 debug.mk create mode 100644 fake-quartz-cg.c create mode 100644 fake-quartz-cg.h create mode 100644 fake-w32-gdi.c create mode 100644 fake-w32-gdi.h create mode 100644 gamma-dummy.c.gpp create mode 100644 gamma-dummy.h create mode 100644 gamma-linux-drm.c create mode 100644 gamma-linux-drm.h create mode 100644 gamma-quartz-cg.c create mode 100644 gamma-quartz-cg.h create mode 100644 gamma-w32-gdi.c create mode 100644 gamma-w32-gdi.h create mode 100644 gamma-x-randr.c create mode 100644 gamma-x-randr.h create mode 100644 gamma-x-vidmode.c create mode 100644 gamma-x-vidmode.h create mode 100644 get_ramps.h create mode 100644 libgamma.h create mode 100644 libgamma_behex_edid.c create mode 100644 libgamma_behex_edid_lowercase.c create mode 100644 libgamma_behex_edid_uppercase.c create mode 100644 libgamma_connector_type_count.c create mode 100644 libgamma_const_of_connector_type.c create mode 100644 libgamma_const_of_method.c create mode 100644 libgamma_const_of_subpixel_order.c create mode 100644 libgamma_crtc_destroy.c create mode 100644 libgamma_crtc_free.c create mode 100644 libgamma_crtc_get_gamma_ramps16.c create mode 100644 libgamma_crtc_get_gamma_ramps32.c create mode 100644 libgamma_crtc_get_gamma_ramps64.c create mode 100644 libgamma_crtc_get_gamma_ramps8.c create mode 100644 libgamma_crtc_get_gamma_rampsd.c create mode 100644 libgamma_crtc_get_gamma_rampsf.c create mode 100644 libgamma_crtc_info_count.c create mode 100644 libgamma_crtc_information_destroy.c create mode 100644 libgamma_crtc_information_free.c create mode 100644 libgamma_crtc_initialise.c create mode 100644 libgamma_crtc_restore.c create mode 100644 libgamma_crtc_set_gamma_ramps16.c create mode 100644 libgamma_crtc_set_gamma_ramps16_f.c create mode 100644 libgamma_crtc_set_gamma_ramps32.c create mode 100644 libgamma_crtc_set_gamma_ramps32_f.c create mode 100644 libgamma_crtc_set_gamma_ramps64.c create mode 100644 libgamma_crtc_set_gamma_ramps64_f.c create mode 100644 libgamma_crtc_set_gamma_ramps8.c create mode 100644 libgamma_crtc_set_gamma_ramps8_f.c create mode 100644 libgamma_crtc_set_gamma_rampsd.c create mode 100644 libgamma_crtc_set_gamma_rampsd_f.c create mode 100644 libgamma_crtc_set_gamma_rampsf.c create mode 100644 libgamma_crtc_set_gamma_rampsf_f.c create mode 100644 libgamma_error_min.c create mode 100644 libgamma_gamma_ramps16_destroy.c create mode 100644 libgamma_gamma_ramps16_free.c create mode 100644 libgamma_gamma_ramps16_initialise.c create mode 100644 libgamma_gamma_ramps32_destroy.c create mode 100644 libgamma_gamma_ramps32_free.c create mode 100644 libgamma_gamma_ramps32_initialise.c create mode 100644 libgamma_gamma_ramps64_destroy.c create mode 100644 libgamma_gamma_ramps64_free.c create mode 100644 libgamma_gamma_ramps64_initialise.c create mode 100644 libgamma_gamma_ramps8_destroy.c create mode 100644 libgamma_gamma_ramps8_free.c create mode 100644 libgamma_gamma_ramps8_initialise.c create mode 100644 libgamma_gamma_rampsd_destroy.c create mode 100644 libgamma_gamma_rampsd_free.c create mode 100644 libgamma_gamma_rampsd_initialise.c create mode 100644 libgamma_gamma_rampsf_destroy.c create mode 100644 libgamma_gamma_rampsf_free.c create mode 100644 libgamma_gamma_rampsf_initialise.c create mode 100644 libgamma_get_crtc_information.c create mode 100644 libgamma_group_gid.c create mode 100644 libgamma_group_name.c create mode 100644 libgamma_internal_allocated_any_ramp.c create mode 100644 libgamma_internal_parse_edid.c create mode 100644 libgamma_internal_translate_from_64.c create mode 100644 libgamma_internal_translate_to_64.c create mode 100644 libgamma_internal_translated_ramp_get_.c create mode 100644 libgamma_internal_translated_ramp_set_.c create mode 100644 libgamma_is_method_available.c create mode 100644 libgamma_list_methods.c create mode 100644 libgamma_method_capabilities.c create mode 100644 libgamma_method_count.c create mode 100644 libgamma_method_default_site.c create mode 100644 libgamma_method_default_site_variable.c create mode 100644 libgamma_name_of_connector_type.c create mode 100644 libgamma_name_of_error.c create mode 100644 libgamma_name_of_method.c create mode 100644 libgamma_name_of_subpixel_order.c create mode 100644 libgamma_partition_destroy.c create mode 100644 libgamma_partition_free.c create mode 100644 libgamma_partition_initialise.c create mode 100644 libgamma_partition_restore.c create mode 100644 libgamma_perror.c create mode 100644 libgamma_site_destroy.c create mode 100644 libgamma_site_free.c create mode 100644 libgamma_site_initialise.c create mode 100644 libgamma_site_restore.c create mode 100644 libgamma_strerror.c create mode 100644 libgamma_strerror_r.c create mode 100644 libgamma_subpixel_order_count.c create mode 100644 libgamma_unhex_edid.c create mode 100644 libgamma_value_of_connector_type.c create mode 100644 libgamma_value_of_error.c create mode 100644 libgamma_value_of_method.c create mode 100644 libgamma_value_of_subpixel_order.c create mode 100644 mk/linux.mk create mode 100644 mk/macos.mk create mode 100644 mk/method-dummy=no.mk create mode 100644 mk/method-dummy=yes.mk create mode 100644 mk/method-linux-drm=no.mk create mode 100644 mk/method-linux-drm=yes.mk create mode 100644 mk/method-quartz-cg=fake.mk create mode 100644 mk/method-quartz-cg=no.mk create mode 100644 mk/method-quartz-cg=yes.mk create mode 100644 mk/method-w32-gdi=fake.mk create mode 100644 mk/method-w32-gdi=no.mk create mode 100644 mk/method-w32-gdi=yes.mk create mode 100644 mk/method-x-randr=no.mk create mode 100644 mk/method-x-randr=yes.mk create mode 100644 mk/method-x-vidmode=no.mk create mode 100644 mk/method-x-vidmode=yes.mk create mode 100644 mk/windows.mk create mode 100644 set_ramps.h create mode 100644 set_ramps_fun.h delete mode 100644 src/lib/edid.c delete mode 100644 src/lib/edid.h delete mode 100644 src/lib/fake-quartz-cg.c delete mode 100644 src/lib/fake-quartz-cg.h delete mode 100644 src/lib/fake-w32-gdi.c delete mode 100644 src/lib/fake-w32-gdi.h delete mode 100644 src/lib/gamma-dummy.c.gpp delete mode 100644 src/lib/gamma-dummy.h delete mode 100644 src/lib/gamma-helper.c delete mode 100644 src/lib/gamma-helper.h delete mode 100644 src/lib/gamma-linux-drm.c delete mode 100644 src/lib/gamma-linux-drm.h delete mode 100644 src/lib/gamma-quartz-cg.c delete mode 100644 src/lib/gamma-quartz-cg.h delete mode 100644 src/lib/gamma-w32-gdi.c delete mode 100644 src/lib/gamma-w32-gdi.h delete mode 100644 src/lib/gamma-x-randr.c delete mode 100644 src/lib/gamma-x-randr.h delete mode 100644 src/lib/gamma-x-vidmode.c delete mode 100644 src/lib/gamma-x-vidmode.h delete mode 100644 src/lib/libgamma-error.c.gpp delete mode 100644 src/lib/libgamma-error.h delete mode 100644 src/lib/libgamma-facade.c.gpp delete mode 100644 src/lib/libgamma-facade.h delete mode 100644 src/lib/libgamma-method.c delete mode 100644 src/lib/libgamma-method.h delete mode 100644 src/lib/libgamma.h diff --git a/.gitignore b/.gitignore index 270192c..0c969eb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,25 +1,11 @@ -_/ -bin/ -obj/ -\#*\# -.* -!.git* +*\#* *~ -*.bak -*.swp -*.swo -*.su -*.gch -*.out *.o -/config.mk -/src/lib/libgamma-config.h -*.info -*.pdf -*.dvi -*.ps -*.tar -*.gz -*.bz2 -*.xz - +*.lo +*.su +*.a +*.so +*.so.* +*.dll +*.dylib +config.h diff --git a/DEPENDENCIES b/DEPENDENCIES index 6a62f7d..c63ba29 100644 --- a/DEPENDENCIES +++ b/DEPENDENCIES @@ -10,26 +10,18 @@ RUNTIME DEPENDENCIES: compile-time and are not utilisable if not selected at compile-time. -CONFIGURE DEPENDENCIES: - - sh - coreutils - sed - - BUILD DEPENDENCIES: make - grep - sed - coreutils - c99 gcc is preferred + sh + printf + test + mv + rm + cat + c11 + ar pkg-config - general-preprocessor (gpp) - python Both Python 2 and Python 3 will work - texinfo Optional: for info, pdf, ps and dvi manuals - texlive-core Optional: for pdf, ps and dvi manuals - bash Optional: for info, pdf, ps and dvi manuals And all runtime dependencies that are mandatory or has been opted for. @@ -37,25 +29,7 @@ BUILD DEPENDENCIES: INSTALL DEPENDENCIES: make - coreutils - - -DEPENDENCIES SOURCES: - - sh bash can provide sh - make https://www.gnu.org/software/make - grep https://www.gnu.org/software/grep - sed https://www.gnu.org/software/sed - coreutils https://www.gnu.org/software/coreutils - c99 https://gcc.gnu.org - pkg-config http://pkgconfig.freedesktop.org - general-preprocessor (gpp) https://github.com/maandree/gpp - python https://www.python.org/ - texinfo https://www.gnu.org/software/texinfo/ - texlive-core https://tug.org/texlive/ - bash https://www.gnu.org/software/bash - libc https://www.gnu.org/software/libc - libxcb http://xcb.freedesktop.org/ - libx11 (xlib) http://xorg.freedesktop.org/ - libxxf86vm http://xorg.freedesktop.org/ - libdrm http://dri.freedesktop.org/ + sh + mkdir + cp + ln diff --git a/Makefile b/Makefile index 1e99d29..6b8746d 100644 --- a/Makefile +++ b/Makefile @@ -1,312 +1,192 @@ -# Copying and distribution of this file, with or without modification, -# are permitted in any medium without royalty provided the copyright -# notice and this notice are preserved. This file is offered as-is, -# without any warranty. +.POSIX: - -# The package path prefix, if you want to install to another root, set DESTDIR to that root. -PREFIX ?= /usr -# The library path excluding prefix. -LIB ?= /lib -# The library header path excluding prefix. -INCLUDE ?= /include -# The resource path excluding prefix. -DATA ?= /share -# The library path including prefix. -LIBDIR ?= $(PREFIX)$(LIB) -# The library header including prefix. -INCLUDEDIR ?= $(PREFIX)$(INCLUDE) -# The resource path including prefix. -DATADIR ?= $(PREFIX)$(DATA) -# The generic documentation path including prefix. -DOCDIR ?= $(DATADIR)/doc -# The info manual documentation path including prefix. -INFODIR ?= $(DATADIR)/info -# The license base path including prefix. -LICENSEDIR ?= $(DATADIR)/licenses - -# The name of the package as it should be installed. -PKGNAME ?= libgamma - -# General-preprocess command. (https://github.com/maandree/gpp) -GPP ?= gpp - -# C compiler, GNU C Compiler by default -CC ?= gcc -CC_BASE ?= $(shell echo $(CC) | cut -d ' ' -f 1) - - -# Enabled warnings. -WARN = -Wall -Wextra -pedantic -Wformat=2 -Winit-self -Wmissing-include-dirs \ - -Wfloat-equal -Wshadow -Wmissing-prototypes -Wmissing-declarations \ - -Wredundant-decls -Wnested-externs -Winline -Wno-variadic-macros \ - -Wswitch-default -Wconversion -Wcast-align -Wstrict-overflow \ - -Wdeclaration-after-statement -Wundef -Wcast-qual -Wbad-function-cast \ - -Wwrite-strings -Waggregate-return -Wpacked -Wstrict-prototypes \ - -Wold-style-definition - -ifeq ($(CC_BASE),gcc) -WARN += -Wdouble-promotion -Wtrampolines -Wsign-conversion -Wsync-nand \ - -Wlogical-op -Wvector-operation-performance \ - -Wunsuffixed-float-constants -Wsuggest-attribute=const \ - -Wsuggest-attribute=noreturn -Wsuggest-attribute=pure \ - -Wsuggest-attribute=format -Wnormalized=nfkc \ - -Wunsafe-loop-optimizations -endif - - -# The C standard used in the code. -STD = c99 - -# Library linking flags for the linker. -LIBS_LD = -# Library linking flags for the C compiler. -LIBS_C = - -# Object files for the library. -LIBOBJ = libgamma-facade libgamma-method libgamma-error gamma-helper edid - -# Header files for the library are parsed for the info manual. -HEADERS_INFO = libgamma-error libgamma-facade libgamma-method - -# Header files for the library, as installed. -HEADERS = libgamma libgamma-config $(HEADERS_INFO) - -# Object files for the test. -TESTOBJ = test methods errors crtcinfo user ramps - -# The version of the library. LIB_MAJOR = 0 -LIB_MINOR = 7 +LIB_MINOR = 8 LIB_VERSION = $(LIB_MAJOR).$(LIB_MINOR) -# Change by .config.mk to reflect what is used in the OS, linux uses so: libgamma.so -SO = so - -# These three below are changed by .config.mk if required -SHARED = -shared -LDSO = -Wl,-soname,libgamma.$(SO).$(LIB_MAJOR) -PIC = -fPIC - -# Include configurations from `./configure`. -include .config.mk - -# Optimisation level (and debug flags.) -ifeq ($(DEBUG),y) -ifeq ($(CC_BASE),gcc) -OPTIMISE = -Og -g3 -else -OPTIMISE = -g -endif -else -ifeq ($(CC_BASE),gcc) -OPTIMISE = -Ofast -else -OPTIMISE = -O -endif -endif - -# C compiler debug flags. -DEBUG_FLAGS = -ifeq ($(DEBUG),y) -DEBUG_FLAGS += -D'DEBUG' -endif - -# Options for the C compiler for the test. -TEST_FLAGS = $(OPTIMISE) $(WARN) -std=$(STD) -fstrict-aliasing -fstrict-overflow -fno-builtin - -ifeq ($(CC_BASE),gcc) -TEST_FLAGS += -fstack-usage -ftree-vrp -fipa-pure-const -funsafe-loop-optimizations -endif - - -# Options for the C compiler for the library. -LIB_FLAGS = $(TEST_FLAGS) $(DEBUG_FLAGS) $(DEFINITIONS) -DLIBGAMMA_CONFIG_H - -ifeq ($(CC_BASE),gcc) -TEST_FLAGS += -D__GCC__ -LIB_FLAGS += -D__GCC__ -endif - -ifeq ($(HAVE_INT128),y) -LIB_FLAGS += -DHAVE_INT128 -endif - - - -# Build rules. - -.PHONY: default -default: lib test info - -.PHONY: all -all: lib test doc - - -.PHONY: lib -lib: bin/libgamma.$(SO).$(LIB_VERSION) bin/libgamma.$(SO).$(LIB_MAJOR) bin/libgamma.$(SO) - -bin/libgamma.$(SO).$(LIB_VERSION): $(foreach O,$(LIBOBJ),obj/lib/$(O).o) - mkdir -p $(shell dirname $@) - $(CC) $(LIB_FLAGS) $(LIBS_LD) $(SHARED) $(LDSO) -o $@ $^ $(LDFLAGS) - -bin/libgamma.$(SO).$(LIB_MAJOR): - mkdir -p $(shell dirname $@) - ln -sf libgamma.$(SO).$(LIB_VERSION) $@ - -bin/libgamma.$(SO): - mkdir -p $(shell dirname $@) - ln -sf libgamma.$(SO).$(LIB_VERSION) $@ - -obj/lib/%.o: src/lib/%.c src/lib/*.h - mkdir -p $(shell dirname $@) - $(CC) $(LIB_FLAGS) $(LIBS_C) $(PIC) -s -c -o $@ $< $(CPPFLAGS) $(CFLAGS) -obj/lib/%.o: obj/lib/%.c src/lib/*.h - $(CC) $(LIB_FLAGS) $(LIBS_C) $(PIC) -iquote"$$(dirname "$<" | sed -e 's:^obj:src:')" -c -o $@ $< $(CPPFLAGS) $(CFLAGS) +X_RANDR_METHOD = no +X_VIDMODE_METHOD = no +LINUX_DRM_METHOD = no +W32_GDI_METHOD = no +QUARTZ_CG_METHOD = no +DUMMY_METHOD = yes + + +CONFIGFILE = config.mk +include $(CONFIGFILE) + +OS = linux +# Linux: linux +# Mac OS: macos +# Windows: windows +include mk/$(OS).mk + + +HDR_METHODS = +CPPFLAGS_METHODS = +METHODS_PARAMS = + +QUARTZ_CORE_GRAPHICS_METHOD = $(QUARTZ_CG_METHOD) +include mk/method-x-randr=$(X_RANDR_METHOD).mk +include mk/method-x-vidmode=$(X_VIDMODE_METHOD).mk +include mk/method-linux-drm=$(LINUX_DRM_METHOD).mk +include mk/method-w32-gdi=$(W32_GDI_METHOD).mk +include mk/method-quartz-cg=$(QUARTZ_CORE_GRAPHICS_METHOD).mk +include mk/method-dummy=$(DUMMY_METHOD).mk + + +OBJ_PUBLIC =\ + libgamma_behex_edid.o\ + libgamma_behex_edid_lowercase.o\ + libgamma_behex_edid_uppercase.o\ + libgamma_connector_type_count.o\ + libgamma_const_of_connector_type.o\ + libgamma_const_of_method.o\ + libgamma_const_of_subpixel_order.o\ + libgamma_crtc_destroy.o\ + libgamma_crtc_free.o\ + libgamma_crtc_get_gamma_ramps16.o\ + libgamma_crtc_get_gamma_ramps32.o\ + libgamma_crtc_get_gamma_ramps64.o\ + libgamma_crtc_get_gamma_ramps8.o\ + libgamma_crtc_get_gamma_rampsd.o\ + libgamma_crtc_get_gamma_rampsf.o\ + libgamma_crtc_info_count.o\ + libgamma_crtc_information_destroy.o\ + libgamma_crtc_information_free.o\ + libgamma_crtc_initialise.o\ + libgamma_crtc_restore.o\ + libgamma_crtc_set_gamma_ramps16.o\ + libgamma_crtc_set_gamma_ramps16_f.o\ + libgamma_crtc_set_gamma_ramps32.o\ + libgamma_crtc_set_gamma_ramps32_f.o\ + libgamma_crtc_set_gamma_ramps64.o\ + libgamma_crtc_set_gamma_ramps64_f.o\ + libgamma_crtc_set_gamma_ramps8.o\ + libgamma_crtc_set_gamma_ramps8_f.o\ + libgamma_crtc_set_gamma_rampsd.o\ + libgamma_crtc_set_gamma_rampsd_f.o\ + libgamma_crtc_set_gamma_rampsf.o\ + libgamma_crtc_set_gamma_rampsf_f.o\ + libgamma_error_min.o\ + libgamma_gamma_ramps16_destroy.o\ + libgamma_gamma_ramps16_free.o\ + libgamma_gamma_ramps16_initialise.o\ + libgamma_gamma_ramps32_destroy.o\ + libgamma_gamma_ramps32_free.o\ + libgamma_gamma_ramps32_initialise.o\ + libgamma_gamma_ramps64_destroy.o\ + libgamma_gamma_ramps64_free.o\ + libgamma_gamma_ramps64_initialise.o\ + libgamma_gamma_ramps8_destroy.o\ + libgamma_gamma_ramps8_free.o\ + libgamma_gamma_ramps8_initialise.o\ + libgamma_gamma_rampsd_destroy.o\ + libgamma_gamma_rampsd_free.o\ + libgamma_gamma_rampsd_initialise.o\ + libgamma_gamma_rampsf_destroy.o\ + libgamma_gamma_rampsf_free.o\ + libgamma_gamma_rampsf_initialise.o\ + libgamma_get_crtc_information.o\ + libgamma_group_gid.o\ + libgamma_group_name.o\ + libgamma_is_method_available.o\ + libgamma_list_methods.o\ + libgamma_method_capabilities.o\ + libgamma_method_count.o\ + libgamma_method_default_site.o\ + libgamma_method_default_site_variable.o\ + libgamma_name_of_connector_type.o\ + libgamma_name_of_error.o\ + libgamma_name_of_method.o\ + libgamma_name_of_subpixel_order.o\ + libgamma_partition_destroy.o\ + libgamma_partition_free.o\ + libgamma_partition_initialise.o\ + libgamma_partition_restore.o\ + libgamma_perror.o\ + libgamma_site_destroy.o\ + libgamma_site_free.o\ + libgamma_site_initialise.o\ + libgamma_site_restore.o\ + libgamma_strerror.o\ + libgamma_strerror_r.o\ + libgamma_subpixel_order_count.o\ + libgamma_unhex_edid.o\ + libgamma_value_of_connector_type.o\ + libgamma_value_of_error.o\ + libgamma_value_of_method.o\ + libgamma_value_of_subpixel_order.o + +OBJ_INTERNAL =\ + libgamma_internal_allocated_any_ramp.o\ + libgamma_internal_parse_edid.o\ + libgamma_internal_translated_ramp_get_.o\ + libgamma_internal_translated_ramp_set_.o\ + libgamma_internal_translate_from_64.o\ + libgamma_internal_translate_to_64.o\ + +OBJ = $(OBJ_PUBLIC) $(OBJ_INTERNAL) +LOBJ = $(OBJ:.o=.lo) + +HDR =\ + common.h\ + config.h\ + get_ramps.h\ + libgamma.h\ + set_ramps.h\ + set_ramps_fun.h\ + $(HDR_METHODS) + + +all: libgamma.a libgamma.$(LIBEXT) +$(OBJ): $(@:.o=.c) $(HDR) +$(LOBJ): $(@:.lo=.c) $(HDR) + +config.h: FORCE + printf '/* This file is auto-generated.mk */\n' > $@~ + printf '#include "%s"\n' $(HDR_METHODS) >> $@~ + printf '#define LIST_AVAILABLE_METHODS(_)' >> $@~ + printf '\\\n\t_(%s, %s, %s, %s)' $(METHODS_PARAMS) >> $@~ + printf '\n' >> $@~ + if ! test -f $@ || ! test "$$(cat < $@)" = "$$(cat < $@~)"; then mv -- $@~ $@; fi + +libgamma.a: $(OBJ) + -rm -f -- $@ + $(AR) -rc $@ $(OBJ) + $(AR) -s $@ + +libgamma.$(LIBEXT): $(LOBJ) + $(CC) $(LIBFLAGS) $(LDFLAGS_METHODS) -o $@ $(LOBJ) $(LDFLAGS) + +.c.o: + $(CC) -c -o $@ $< $(CFLAGS) $(CFLAGS_METHODS) $(CPPFLAGS) $(CPPFLAGS_METHODS) + +.c.lo: + $(CC) -fPIC -c -o $@ $< $(CFLAGS) $(CFLAGS_METHODS) $(CPPFLAGS) $(CPPFLAGS_METHODS) + +install: libgamma.a libgamma.$(LIBEXT) + mkdir -p -- "$(DESTDIR)$(PREFIX)/lib/" + mkdir -p -- "$(DESTDIR)$(PREFIX)/include/" + cp -- libgamma.$(LIBEXT) "$(DESTDIR)$(PREFIX)/lib/libgamma.$(LIBMINOREXT)" + ln -sf -- libgamma.$(LIBMINOREXT) "$(DESTDIR)$(PREFIX)/lib/libgamma.$(LIBMAJOREXT)" + ln -sf -- libgamma.$(LIBMAJOREXT) "$(DESTDIR)$(PREFIX)/lib/libgamma.$(LIBEXT)" + cp -- libgamma.a "$(DESTDIR)$(PREFIX)/lib/" + cp -- libgamma.h "$(DESTDIR)$(PREFIX)/include/" -obj/%: src/%.gpp src/extract/libgamma-*-extract - mkdir -p $(shell dirname $@) - $(GPP) --symbol '$$' --input $< --output $@ - - -.PHONY: test -test: bin/test - -bin/test: $(foreach O,$(TESTOBJ),obj/test/$(O).o) bin/libgamma.$(SO).$(LIB_VERSION) bin/libgamma.$(SO) - mkdir -p $(shell dirname $@) - $(CC) $(TEST_FLAGS) $(LIBS_LD) -Lbin -lgamma -o $@ $(foreach O,$(TESTOBJ),obj/test/$(O).o) $(LDFLAGS) - -obj/test/%.o: src/test/%.c src/test/*.h src/lib/libgamma*.h - mkdir -p $(shell dirname $@) - $(CC) $(TEST_FLAGS) -Isrc/lib -c -o $@ $< $(CPPFLAGS) $(CFLAGS) - - -.PHONY: doc -doc: info pdf dvi ps - -obj/libgamma.texinfo: info/libgamma.texinfo $(foreach H,$(HEADERS_INFO),src/lib/$(H).h) \ - src/extract/libgamma-*-extract info/texise info/behead - mkdir -p obj - $(GPP) --symbol '%' --input $< --output $@ - -obj/%.texinfo: info/%.texinfo - mkdir -p obj - cp $< $@ - -.PHONY: info -info: libgamma.info -%.info: obj/%.texinfo obj/fdl.texinfo - makeinfo $< - -.PHONY: pdf -pdf: libgamma.pdf -%.pdf: obj/%.texinfo obj/fdl.texinfo - @mkdir -p obj/pdf - cd obj/pdf ; yes X | texi2pdf ../../$< - mv obj/pdf/$@ $@ - -.PHONY: dvi -dvi: libgamma.dvi -%.dvi: obj/%.texinfo obj/fdl.texinfo - @mkdir -p obj/dvi - cd obj/dvi ; yes X | $(TEXI2DVI) ../../$< - mv obj/dvi/$@ $@ - -.PHONY: ps -ps: libgamma.ps -%.ps: obj/%.texinfo obj/fdl.texinfo - @mkdir -p obj/ps - cd obj/ps ; yes X | texi2pdf --ps ../../$< - mv obj/ps/$@ $@ - - -# Install rules. - -.PHONY: install -install: install-base install-info - -.PHONY: install -install-all: install-base install-doc - -.PHONY: install-base -install-base: install-lib install-include install-copyright - - -.PHONY: install-lib -install-lib: bin/libgamma.$(SO).$(LIB_VERSION) - install -dm755 -- "$(DESTDIR)$(LIBDIR)" - install -m755 $< -- "$(DESTDIR)$(LIBDIR)/libgamma.$(SO).$(LIB_VERSION)" - ln -sf libgamma.$(SO).$(LIB_VERSION) -- "$(DESTDIR)$(LIBDIR)/libgamma.$(SO).$(LIB_MAJOR)" - ln -sf libgamma.$(SO).$(LIB_VERSION) -- "$(DESTDIR)$(LIBDIR)/libgamma.$(SO)" - -.PHONY: install-include -install-include: - install -dm755 -- "$(DESTDIR)$(INCLUDEDIR)" - install -m644 $(foreach H,$(HEADERS),src/lib/$(H).h) -- "$(DESTDIR)$(INCLUDEDIR)" - - -.PHONY: install-copyright -install-copyright: install-copying install-license - -.PHONY: install-copying -install-copying: - install -dm755 -- "$(DESTDIR)$(LICENSEDIR)/$(PKGNAME)" - install -m644 COPYING -- "$(DESTDIR)$(LICENSEDIR)/$(PKGNAME)/COPYING" - -.PHONY: install-license -install-license: - install -dm755 -- "$(DESTDIR)$(LICENSEDIR)/$(PKGNAME)" - install -m644 LICENSE -- "$(DESTDIR)$(LICENSEDIR)/$(PKGNAME)/LICENSE" - - -.PHONY: install-doc -install-doc: install-info install-pdf install-ps install-dvi - -.PHONY: install-info -install-info: libgamma.info - install -dm755 -- "$(DESTDIR)$(INFODIR)" - install -m644 $< -- "$(DESTDIR)$(INFODIR)/$(PKGNAME).info" - -.PHONY: install-pdf -install-pdf: libgamma.pdf - install -dm755 -- "$(DESTDIR)$(DOCDIR)" - install -m644 $< -- "$(DESTDIR)$(DOCDIR)/$(PKGNAME).pdf" - -.PHONY: install-ps -install-ps: libgamma.ps - install -dm755 -- "$(DESTDIR)$(DOCDIR)" - install -m644 $< -- "$(DESTDIR)$(DOCDIR)/$(PKGNAME).ps" - -.PHONY: install-dvi -install-dvi: libgamma.dvi - install -dm755 -- "$(DESTDIR)$(DOCDIR)" - install -m644 $< -- "$(DESTDIR)$(DOCDIR)/$(PKGNAME).dvi" - - -# Uninstall rules. - -.PHONY: uninstall uninstall: - -rm -- "$(DESTDIR)$(LIBDIR)/libgamma.$(SO).$(LIB_VERSION)" - -rm -- "$(DESTDIR)$(LIBDIR)/libgamma.$(SO).$(LIB_MAJOR)" - -rm -- "$(DESTDIR)$(LIBDIR)/libgamma.$(SO)" - -rm -- $(foreach H,$(HEADERS),"$(DESTDIR)$(INCLUDEDIR)/$(H).h") - -rm -- "$(DESTDIR)$(LICENSEDIR)/$(PKGNAME)/COPYING" - -rm -- "$(DESTDIR)$(LICENSEDIR)/$(PKGNAME)/LICENSE" - -rmdir -- "$(DESTDIR)$(LICENSEDIR)/$(PKGNAME)" - -rm -- "$(DESTDIR)$(INFODIR)/$(PKGNAME).info" - -rm -- "$(DESTDIR)$(DOCDIR)/$(PKGNAME).pdf" - -rm -- "$(DESTDIR)$(DOCDIR)/$(PKGNAME).ps" - -rm -- "$(DESTDIR)$(DOCDIR)/$(PKGNAME).dvi" - + -rm -f -- "$(DESTDIR)$(PREFIX)/lib/libgamma.$(LIBMAJOREXT)" + -rm -f -- "$(DESTDIR)$(PREFIX)/lib/libgamma.$(LIBMINOREXT)" + -rm -f -- "$(DESTDIR)$(PREFIX)/lib/libgamma.$(LIBEXT)" + -rm -f -- "$(DESTDIR)$(PREFIX)/lib/libgamma.a" + -rm -f -- "$(DESTDIR)$(PREFIX)/include/libgamma.h" -# Clean rules. - -.PHONY: clean clean: - -rm -rf obj bin libgamma.info libgamma.pdf libgamma.ps libgamma.dvi + -rm -f -- *.o *.lo *.su *.a *.$(LIBEXT) + +.SUFFIXES: +.SUFFIXES: .lo .o .c -.PHONY: distclean -distclean: clean - -rm -f .config.mk src/lib/libgamma-config.h +FORCE: +.PHONY: all install uninstall clean FORCE diff --git a/TODO b/TODO index 974b98c..81f2af2 100644 --- a/TODO +++ b/TODO @@ -6,10 +6,6 @@ Important functionality: non-RGB, and monochrome/greyscale). This information is obtainable from the monitor's EDID. -libgamma-intl - An extension with support for text description - of constants and internationalisation. - Unsupported display servers: GNU Hurd TTY Does it have gamma ramp support? The BSD TTY:s Do they have gamma ramp support? diff --git a/common.h b/common.h new file mode 100644 index 0000000..a6584f1 --- /dev/null +++ b/common.h @@ -0,0 +1,352 @@ +/* See LICENSE file for copyright and license details. */ +#include "libgamma.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "config.h" + + +#if !defined(HAVE_INT128) && defined(__GNUC__) +# define HAVE_INT128 +#endif + + + +#define LIST_ERRORS(_)\ + _(LIBGAMMA_ERRNO_SET, NULL)\ + _(LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD, "No such adjustment method")\ + _(LIBGAMMA_NO_SUCH_SITE, "No such site")\ + _(LIBGAMMA_NO_SUCH_PARTITION, "No such partition")\ + _(LIBGAMMA_NO_SUCH_CRTC, "No such CRTC")\ + _(LIBGAMMA_IMPOSSIBLE_AMOUNT, "Impossible amount")\ + _(LIBGAMMA_CONNECTOR_DISABLED, "Connector is disabled")\ + _(LIBGAMMA_OPEN_CRTC_FAILED, "Could not open CRTC")\ + _(LIBGAMMA_CRTC_INFO_NOT_SUPPORTED, "CRTC information is not supported")\ + _(LIBGAMMA_GAMMA_RAMP_READ_FAILED, "Could not read readm gamma ramp")\ + _(LIBGAMMA_GAMMA_RAMP_WRITE_FAILED, "Could not write to gamma ramp")\ + _(LIBGAMMA_GAMMA_RAMP_SIZE_CHANGED, "Gamma ramp size changed")\ + _(LIBGAMMA_MIXED_GAMMA_RAMP_SIZE, "Mixed gamma ramp size")\ + _(LIBGAMMA_WRONG_GAMMA_RAMP_SIZE, "Wrong gamma ramp size")\ + _(LIBGAMMA_SINGLETON_GAMMA_RAMP, "Single-stop gamma ramp")\ + _(LIBGAMMA_LIST_CRTCS_FAILED, "Could not retrieve list of CRTCs")\ + _(LIBGAMMA_ACQUIRING_MODE_RESOURCES_FAILED, "Could not acquire mode resources")\ + _(LIBGAMMA_NEGATIVE_PARTITION_COUNT, "Number of partitions is negative")\ + _(LIBGAMMA_NEGATIVE_CRTC_COUNT, "Number of CRTCs is negative")\ + _(LIBGAMMA_DEVICE_RESTRICTED, "Device is restricted to root")\ + _(LIBGAMMA_DEVICE_ACCESS_FAILED, "Failed to access device")\ + _(LIBGAMMA_DEVICE_REQUIRE_GROUP, "Device requires group membership")\ + _(LIBGAMMA_GRAPHICS_CARD_REMOVED, "Graphics card was removed")\ + _(LIBGAMMA_STATE_UNKNOWN, "Unknown state")\ + _(LIBGAMMA_CONNECTOR_UNKNOWN, "Unknwon connector")\ + _(LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED, "Connector type not recognised")\ + _(LIBGAMMA_SUBPIXEL_ORDER_NOT_RECOGNISED, "Subpixel order not recognised")\ + _(LIBGAMMA_EDID_LENGTH_UNSUPPORTED, "Unsupported EDID length")\ + _(LIBGAMMA_EDID_WRONG_MAGIC_NUMBER, "Incorrect magic number in EDID")\ + _(LIBGAMMA_EDID_REVISION_UNSUPPORTED, "Unsupported EDID revision")\ + _(LIBGAMMA_GAMMA_NOT_SPECIFIED, "No gamma information specified")\ + _(LIBGAMMA_EDID_CHECKSUM_ERROR, "Incorrect checksum in EDID")\ + _(LIBGAMMA_GAMMA_NOT_SPECIFIED_AND_EDID_CHECKSUM_ERROR, "No gamma information specified and incorrect checksum in EDID")\ + _(LIBGAMMA_GAMMA_RAMPS_SIZE_QUERY_FAILED, "Could not retrieve gamma ramp size")\ + _(LIBGAMMA_OPEN_PARTITION_FAILED, "Could not open partition")\ + _(LIBGAMMA_OPEN_SITE_FAILED, "Could not open site")\ + _(LIBGAMMA_PROTOCOL_VERSION_QUERY_FAILED, "Could not retreive protocol version")\ + _(LIBGAMMA_PROTOCOL_VERSION_NOT_SUPPORTED, "Protocol version not supported")\ + _(LIBGAMMA_LIST_PARTITIONS_FAILED, "Could not retrieve list of partitions")\ + _(LIBGAMMA_NULL_PARTITION, "Null partition retrieved")\ + _(LIBGAMMA_NOT_CONNECTED, "Not connected to subsystem")\ + _(LIBGAMMA_REPLY_VALUE_EXTRACTION_FAILED, "Could not extract value from reply")\ + _(LIBGAMMA_EDID_NOT_FOUND, "No EDID found")\ + _(LIBGAMMA_LIST_PROPERTIES_FAILED, "Could not list properties")\ + _(LIBGAMMA_PROPERTY_VALUE_QUERY_FAILED, "Could not query the value of a property")\ + _(LIBGAMMA_OUTPUT_INFORMATION_QUERY_FAILED, "Could not query information for output") + +#define X(A, B) -1 +# if (LIST_ERRORS(X)) != LIBGAMMA_ERROR_MIN +# error There is a mismatch between LIST_ERRORS and LIBGAMMA_ERROR_MIN +# endif +#undef X + + + +#ifdef HAVE_LIBGAMMA_METHOD_DUMMY +# define HAVE_METHOD_DUMMY 1 +#else +# define HAVE_METHOD_DUMMY 0 +#endif + +#ifdef HAVE_LIBGAMMA_METHOD_X_RANDR +# define HAVE_METHOD_X_RANDR 1 +#else +# define HAVE_METHOD_X_RANDR 0 +#endif + +#ifdef HAVE_LIBGAMMA_METHOD_X_VIDMODE +# define HAVE_METHOD_X_VIDMODE 1 +#else +# define HAVE_METHOD_X_VIDMODE 0 +#endif + +#ifdef HAVE_LIBGAMMA_METHOD_LINUX_DRM +# define HAVE_METHOD_LINUX_DRM 1 +#else +# define HAVE_METHOD_LINUX_DRM 0 +#endif + +#ifdef HAVE_LIBGAMMA_METHOD_W32_GDI +# define HAVE_METHOD_W32_GDI 1 +#else +# define HAVE_METHOD_W32_GDI 0 +#endif + +#ifdef HAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS +# define HAVE_METHOD_QUARTZ_CORE_GRAPHICS 1 +#else +# define HAVE_METHOD_QUARTZ_CORE_GRAPHICS 0 +#endif + +#define LIST_METHODS(_)\ + _(LIBGAMMA_METHOD_X_RANDR, randr, x_randr, HAVE_METHOD_X_RANDR)\ + _(LIBGAMMA_METHOD_X_VIDMODE, vidmode, x_vidmode, HAVE_METHOD_X_VIDMODE)\ + _(LIBGAMMA_METHOD_LINUX_DRM, drm, linux_drm, HAVE_METHOD_LINUX_DRM)\ + _(LIBGAMMA_METHOD_W32_GDI, w32gdi, w32_gdi, HAVE_METHOD_W32_GDI)\ + _(LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS, quartz, quartz_cg, HAVE_METHOD_QUARTZ_CORE_GRAPHICS)\ + _(LIBGAMMA_METHOD_DUMMY, dummy, dummy, HAVE_METHOD_DUMMY) + +#define X(...) +1 +# if (LIST_METHODS(X)) != LIBGAMMA_METHOD_COUNT +# error There is a mismatch between LIST_METHODS and LIBGAMMA_METHOD_COUNT +# endif +#undef X + + + +#define LIST_CONNECTOR_TYPES(_)\ + _(LIBGAMMA_CONNECTOR_TYPE_Unknown, "Unknown")\ + _(LIBGAMMA_CONNECTOR_TYPE_VGA, "VGA")\ + _(LIBGAMMA_CONNECTOR_TYPE_DVI, "DVI")\ + _(LIBGAMMA_CONNECTOR_TYPE_DVII, "DVI-I")\ + _(LIBGAMMA_CONNECTOR_TYPE_DVID, "DVI-D")\ + _(LIBGAMMA_CONNECTOR_TYPE_DVIA, "DVI-A")\ + _(LIBGAMMA_CONNECTOR_TYPE_Composite, "Composite")\ + _(LIBGAMMA_CONNECTOR_TYPE_SVIDEO, "S-video")\ + _(LIBGAMMA_CONNECTOR_TYPE_LVDS, "LVDS")\ + _(LIBGAMMA_CONNECTOR_TYPE_Component, "Component")\ + _(LIBGAMMA_CONNECTOR_TYPE_9PinDIN, "9 pin DIN")\ + _(LIBGAMMA_CONNECTOR_TYPE_DisplayPort, "DisplayPort")\ + _(LIBGAMMA_CONNECTOR_TYPE_HDMI, "HDMI")\ + _(LIBGAMMA_CONNECTOR_TYPE_HDMIA, "HDMI-A")\ + _(LIBGAMMA_CONNECTOR_TYPE_HDMIB, "HDMI-B")\ + _(LIBGAMMA_CONNECTOR_TYPE_TV, "TV")\ + _(LIBGAMMA_CONNECTOR_TYPE_eDP, "eDP")\ + _(LIBGAMMA_CONNECTOR_TYPE_VIRTUAL, "Virtual")\ + _(LIBGAMMA_CONNECTOR_TYPE_DSI, "DSI")\ + _(LIBGAMMA_CONNECTOR_TYPE_LFP, "LFP") + +#define X(A, B) +1 +# if (LIST_CONNECTOR_TYPES(X)) != LIBGAMMA_CONNECTOR_TYPE_COUNT +# error There is a mismatch between LIST_CONNECTOR_TYPES and LIBGAMMA_CONNECTOR_TYPE_COUNT +# endif +#undef X + + + +#define LIST_SUBPIXEL_ORDERS(_)\ + _(LIBGAMMA_SUBPIXEL_ORDER_UNKNOWN, "Unknown")\ + _(LIBGAMMA_SUBPIXEL_ORDER_NONE, "None")\ + _(LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_RGB, "Horizontal RGB")\ + _(LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_BGR, "Horizontal BGR")\ + _(LIBGAMMA_SUBPIXEL_ORDER_VERTICAL_RGB, "Vertical RGB")\ + _(LIBGAMMA_SUBPIXEL_ORDER_VERTICAL_BGR, "Vertical BGR") + +#define X(A, B) +1 +# if (LIST_SUBPIXEL_ORDERS(X)) != LIBGAMMA_SUBPIXEL_ORDER_COUNT +# error There is a mismatch between LIST_SUBPIXEL_ORDERS and LIBGAMMA_SUBPIXEL_ORDER_COUNT +# endif +#undef X + + + +/** + * Gamma ramp structure union for different depths + */ +typedef union 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; + +} gamma_ramps_any_t; + + +/** + * A function for reading the gamma ramps from a CRTC + * + * @param this The CRTC state + * @param ramps The store for the gamma ramps + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +typedef int get_ramps_any_fun(libgamma_crtc_state_t *restrict, gamma_ramps_any_t *restrict); + +/** + * A function for writing the gamma ramps to a CRTC + * + * @param this The CRTC state + * @param ramps The gamma ramps + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +typedef int set_ramps_any_fun(libgamma_crtc_state_t *restrict, gamma_ramps_any_t); + + + +/** + * Get the current gamma ramps for a CRTC, re-encoding version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @param depth_user The depth of the gamma ramps that are provided by the user, + * `-1` for `float`, `-2` for `double` + * @param depth_system The depth of the gamma ramps as required by the adjustment method, + * `-1` for `float`, `-2` for `double` + * @param fun Function that is to be used read the ramps, its parameters have + * the same function as those of this function with the same names, + * and the return value too is identical + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +#define libgamma_internal_translated_ramp_get(this, ramps, depth_user, depth_system, fun)\ + libgamma_internal_translated_ramp_get_((this), (ramps), (depth_user), (depth_system), (get_ramps_any_fun *)(fun)) + +/** + * Set the gamma ramps for a CRTC, re-encoding version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @param depth_user The depth of the gamma ramps that are provided by the user, + * `-1` for `float`, `-2` for `double` + * @param depth_system The depth of the gamma ramps as required by the adjustment method, + * `-1` for `float`, `-2` for `double` + * @param fun Function that is to be used write the ramps, its parameters have + * the same function as those of this function with the same names, + * and the return value too is identical + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +#define libgamma_internal_translated_ramp_set(this, ramps, depth_user, depth_system, fun)\ + libgamma_internal_translated_ramp_set_((this), (ramps), (depth_user), (depth_system), (set_ramps_any_fun *)fun) + +/** + * Get the current gamma ramps for a CRTC, re-encoding version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @param depth_user The depth of the gamma ramps that are provided by the user, + * `-1` for `float`, `-2` for `double` + * @param depth_system The depth of the gamma ramps as required by the adjustment method, + * `-1` for `float`, `-2` for `double` + * @param fun Function that is to be used read the ramps, its parameters have + * the same function as those of this function with the same names, + * and the return value too is identical + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_internal_translated_ramp_get_(libgamma_crtc_state_t *restrict, gamma_ramps_any_t *restrict, + signed, signed, get_ramps_any_fun *); + +/** + * Set the gamma ramps for a CRTC, re-encoding version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @param depth_user The depth of the gamma ramps that are provided by the user, + * `-1` for `float`, `-2` for `double` + * @param depth_system The depth of the gamma ramps as required by the adjustment method, + * `-1` for `float`, `-2` for `double` + * @param fun Function that is to be used write the ramps, its parameters have + * the same function as those of this function with the same names, + * and the return value too is identical + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_internal_translated_ramp_set_(libgamma_crtc_state_t *restrict, gamma_ramps_any_t, + signed, signed, set_ramps_any_fun *); + +/** + * 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 + */ +void libgamma_internal_translate_to_64(signed, size_t, uint64_t *restrict, gamma_ramps_any_t); + +/** + * Undo the actions of `libgamma_internal_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 + */ +void libgamma_internal_translate_from_64(signed, size_t, gamma_ramps_any_t, uint64_t *restrict); + +/** + * Allocate and initalise a gamma ramp with any depth + * + * @param ramps_sys Output gamma ramps + * @param ramps The gamma ramps whose sizes should be duplicated + * @param depth The depth of the gamma ramps to allocate, + * `-1` for `float`, `-2` for `double` + * @param elements Output reference for the grand size of the gamma ramps + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_internal_allocated_any_ramp(gamma_ramps_any_t *restrict, gamma_ramps_any_t, signed, size_t *restrict); + + +/** + * Parse the EDID of a monitor + * + * @param this Instance of a data structure to fill with the information about the EDID; + * it must contain the EDID and its length + * @param fields OR:ed identifiers for the information about the EDID that should be parsed; + * fields that do not have to do with EDID are ignored + * @return Non-zero on error + */ +int libgamma_internal_parse_edid(libgamma_crtc_information_t *restrict, int32_t); diff --git a/config.mk b/config.mk new file mode 100644 index 0000000..cc02d6e --- /dev/null +++ b/config.mk @@ -0,0 +1,6 @@ +PREFIX = /usr +MANPREFIX = $(PREFIX)/share/man + +CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 +CFLAGS = -std=c11 -O2 +LDFLAGS = -s diff --git a/configure b/configure deleted file mode 100755 index b662f13..0000000 --- a/configure +++ /dev/null @@ -1,211 +0,0 @@ -#!/bin/sh - -# Copying and distribution of this file, with or without modification, -# are permitted in any medium without royalty provided the copyright -# notice and this notice are preserved. This file is offered as-is, -# without any warranty. - - -have_debug='No, enable with --debug' -have_dummy='No, enable with --enable-dummy' -have_randr='No, enable with --enable-randr' -have_vidmode='No, enable with --enable-vidmode' -have_drm='No, enable with --enable-drm' -have_w32gdi='No, enable with --enable-w32gdi[=fake]' -have_quartz='No, enable with --enable-quartz[=fake]' - -enable_debug=0 -enable_dummy=0 -enable_randr=0 -enable_vidmode=0 -enable_drm=0 -enable_w32gdi=0 -enable_quartz=0 -fake_w32gdi=0 -fake_quartz=0 - -os=common - - -for arg in "$@"; do - case "${arg}" in - (--linux=developer|--developer) enable_debug=1 - enable_dummy=1 - enable_vidmode=1 - enable_randr=1 - enable_drm=1 - fake_w32gdi=1 - fake_quartz=1 - os=gnu - ;; - (--linux) enable_vidmode=1 - enable_randr=1 - enable_drm=1 - os=gnu - ;; - (--*bsd=developer) enable_debug=1 - enable_dummy=1 - enable_vidmode=1 - enable_randr=1 - fake_w32gdi=1 - fake_quartz=1 - ;; - (--*bsd) enable_vidmode=1 - enable_randr=1 - ;; - (--windows=developer) enable_debug=1 - enable_dummy=1 - enable_w32gdi=1 - os=w32 - ;; - (--windows) enable_w32gdi=1 - os=w32 - ;; - (--mac-os-x=developer) enable_debug=1 - enable_dummy=1 - enable_quartz=1 - os=osx - ;; - (--mac-os-x) enable_quartz=1 - os=osx - ;; - (--debug) enable_debug=1 ;; - (--enable-dummy) enable_dummy=1 ;; - (--enable-randr) enable_randr=1 ;; - (--enable-vidmode) enable_vidmode=1 ;; - (--enable-drm) enable_drm=1 ;; - (--enable-w32gdi) enable_w32gdi=1 ;; - (--enable-quartz) enable_quartz=1 ;; - (--enable-w32gdi=fake) fake_w32gdi=1 ;; - (--enable-quartz=fake) fake_quartz=1 ;; - (*) - echo "$0: unrecognised option: ${arg}" >&2 - exit 1 - ;; - esac -done - - -exec 3> "$(dirname "$0")/.config.mk" -exec 4> "$(dirname "$0")/src/lib/libgamma-config.h" -echo 'DEFINITIONS =' >&3 - -echo '/**' >&4 -cat "$(dirname "$0")/COPYING" | sed -e 's:^: \* :' >&4 -echo ' */' >&4 -echo '#ifndef LIBGAMMA_CONFIG_H' >&4 -echo '#define LIBGAMMA_CONFIG_H' >&4 -echo >&4 -echo >&4 - -if [ ${fake_w32gdi} = 1 ]; then - enable_w32gdi=1 -fi -if [ ${fake_quartz} = 1 ]; then - enable_quartz=1 -fi -if [ ${enable_debug} = 1 ]; then - echo "DEBUG = y" >&3 - echo 'DEBUG_FLAGS += -DDEBUG' >&3 - have_debug='Yes' -fi -if [ ${enable_dummy} = 1 ]; then - echo 'LIBOBJ += gamma-dummy' >&3 - echo 'DEFINITIONS += -DHAVE_LIBGAMMA_METHOD_DUMMY' >&3 - echo '#define HAVE_LIBGAMMA_METHOD_DUMMY' >&4 - have_dummy='Yes' -fi -if [ ${enable_randr} = 1 ]; then - echo 'LIBOBJ += gamma-x-randr' >&3 - echo 'DEFINITIONS += -DHAVE_LIBGAMMA_METHOD_X_RANDR' >&3 - echo 'LIBS_LD += $$(pkg-config --libs xcb xcb-randr)' >&3 - echo 'LIBS_C += $$(pkg-config --cflags xcb xcb-randr)' >&3 - echo '#define HAVE_LIBGAMMA_METHOD_X_RANDR' >&4 - have_randr='Yes' -fi -if [ ${enable_vidmode} = 1 ]; then - echo 'LIBOBJ += gamma-x-vidmode' >&3 - echo 'DEFINITIONS += -DHAVE_LIBGAMMA_METHOD_X_VIDMODE' >&3 - echo 'LIBS_LD += $$(pkg-config --libs x11 xxf86vm)' >&3 - echo 'LIBS_C += $$(pkg-config --cflags x11 xxf86vm)' >&3 - echo '#define HAVE_LIBGAMMA_METHOD_X_VIDMODE' >&4 - have_vidmode='Yes' -fi -if [ ${enable_drm} = 1 ]; then - echo 'LIBOBJ += gamma-linux-drm' >&3 - echo 'DEFINITIONS += -DHAVE_LIBGAMMA_METHOD_LINUX_DRM' >&3 - echo 'LIBS_LD += $$(pkg-config --libs libdrm)' >&3 - echo 'LIBS_C += $$(pkg-config --cflags libdrm)' >&3 - echo '#define HAVE_LIBGAMMA_METHOD_LINUX_DRM' >&4 - have_drm='Yes' -fi -if [ ${enable_w32gdi} = 1 ]; then - echo 'LIBOBJ += gamma-w32-gdi' >&3 - echo 'DEFINITIONS += -DHAVE_LIBGAMMA_METHOD_W32_GDI' >&3 - echo '#define HAVE_LIBGAMMA_METHOD_W32_GDI' >&4 - have_w32gdi='Yes' -fi -if [ ${enable_quartz} = 1 ]; then - echo 'LIBOBJ += gamma-quartz-cg' >&3 - echo 'DEFINITIONS += -DHAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS' >&3 - if [ ${fake_w32gdi} = 0 ]; then - F_ApplicationServices="/System/Library/Frameworks/ApplicationServices.framework" - I_ApplicationServices="${F_ApplicationServices}/Versions/A/Frameworks/CoreGraphics.framework/Versions/A/Headers" - echo "LIBS_LD += -I${I_ApplicationServices} -F${F_ApplicationServices} -framework ApplicationServices" >&3 - fi - echo '#define HAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS' >&4 - have_quartz='Yes' -fi -if [ ${fake_w32gdi} = 1 ]; then - echo 'LIBOBJ += fake-w32-gdi' >&3 - echo 'DEFINITIONS += -DFAKE_LIBGAMMA_METHOD_W32_GDI' >&3 - echo '#define FAKE_LIBGAMMA_METHOD_W32_GDI' >&4 - if [ ${enable_randr} = 1 ]; then - have_w32gdi='Yes, fake via the RandR protocol for X' - else - have_w32gdi='Yes, fake via dummy method, `/dev/null`-style' - fi -fi -if [ ${fake_quartz} = 1 ]; then - echo 'LIBOBJ += fake-quartz-cg' >&3 - echo 'DEFINITIONS += -DFAKE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS' >&3 - echo '#define FAKE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS' >&4 - if [ ${enable_randr} = 1 ]; then - have_quartz='Yes, fake via the RandR protocol for X' - else - have_quartz='Yes, fake via dummy method, `/dev/null`-style' - fi -fi -if [ ${os} = w32 ]; then - echo 'SO = dll' >&3 - echo 'PIC = ' >&3 - echo 'SHARED = -mdll' >&3 -elif [ ${os} = osx ]; then - echo 'SO = dylib' >&3 - echo 'SHARED = -dynamiclib' >&3 - echo 'LDSO = ' >&3 -elif [ ${os} = gnu ]; then - echo 'HAVE_INT128 = y' >&3 -fi - -echo >&4 -echo >&4 -echo '#endif' >&4 -echo >&4 - -exec 4<&- -exec 3<&- - - -echo 'libgamma has now been configured.' -echo -echo " Debug mode: ${have_debug}" -echo " Dummy method: ${have_dummy}" -echo " X.org via RandR: ${have_randr}" -echo " X.org via VidMode: ${have_vidmode}" -echo " Linux DRM: ${have_drm}" -echo " Windows GDI: ${have_w32gdi}" -echo " Quartz via CoreGraphics: ${have_quartz}" -echo -echo 'Compile with `make`.' - diff --git a/debug.mk b/debug.mk new file mode 100644 index 0000000..f9ba3db --- /dev/null +++ b/debug.mk @@ -0,0 +1,8 @@ +include config.mk + +CFLAGS = -std=c11 -Og -g +LDFLAGS = + +W32_GDI_METHOD = fake +QUARTZ_CG_METHOD = fake +DUMMY_METHOD = yes diff --git a/fake-quartz-cg.c b/fake-quartz-cg.c new file mode 100644 index 0000000..75f1fb2 --- /dev/null +++ b/fake-quartz-cg.c @@ -0,0 +1,447 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef FAKE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS +# error Compiling fake-quartz-cg.c without FAKE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS +#endif + +/* This file very sloppily translates Mac OS X Quartz calls to X RandR calls. + * It should by no means be used, without additional modification, as a + * part of a compatibility layer. The purpose of this file is only to make + * it possible to test for logical errors in Max OS X specific code on + * a GNU/Linux system under X. */ + + +#include "fake-quartz-cg.h" + +#include +#include +#include + + +#ifndef HAVE_LIBGAMMA_METHOD_X_RANDR +/* Use dummy translation */ + + +/** + * Get a list of all online displays on the system + * + * @param max_size The number of elements allocated for `displays_out` + * @param displays_out List ot fill with the ID for each online display on the system + * @param count_out Output parameter for the number of elements stored in `displays_out` + * if `displays_out` is too small to fit all display ID:s, + * `*count_out` will be `max_size` + * @return `kCGErrorSuccess` on success, and error number of failure + */ +CGError +CGGetOnlineDisplayList(uint32_t max_size, CGDirectDisplayID *restrict displays_out, uint32_t *restrict count_out) +{ + /* Pretend that we have 2 CRTC:s */ + uint32_t i; + for (i = 0; i < max_size && i < 2; i++) + displays_out[i] = (CGDirectDisplayID)i; + *count_out = i; + return kCGErrorSuccess; +} + + +/** + * Set the gamma ramps for a display + * + * @param display The ID of the display + * @param gamma_size The number of stops in gamma ramps + * @param red The gamma ramp for the red channel + * @param green The gamma ramp for the green channel + * @param blue The gamma ramp for the blue channel + * @return `kCGErrorSuccess` on success, and error number of failure + */ +CGError +CGSetDisplayTransferByTable(CGDirectDisplayID display, uint32_t gamma_size, const CGGammaValue *red, + const CGGammaValue *green, const CGGammaValue *blue) +{ + (void) display; + (void) red; + (void) green; + (void) blue; + + /* We pretend that our gamma ramps are of size 256 */ + if (gamma_size != 256) { + fprintf(stderr, "Gamma size should be 256\n"); + abort(); + } + return kCGErrorSuccess; +} + + +/** + * Get the current gamma ramps for a display + * + * @param display The ID of the display + * @param gamma_size The number of stops you have allocated for the gamma ramps + * @param red Table allocated for the gamma ramp for the red channel + * @param green Table allocated for the gamma ramp for the green channel + * @param blue Table allocated for the gamma ramp for the blue channel + * @param gamma_size_out Output parameter for the actual number of stops in the gamma ramps + * @return `kCGErrorSuccess` on success, and error number of failure + */ +CGError +CGGetDisplayTransferByTable(CGDirectDisplayID display, uint32_t gamma_size, CGGammaValue *restrict red, + CGGammaValue *restrict green, CGGammaValue *restrict blue, uint32_t *restrict gamma_size_out) +{ + long i; + + (void) display; + + /* We pretend that our gamma ramps are of size 256 */ + if (gamma_size != 256) { + fprintf(stderr, "Gamma size should be 256\n"); + abort(); + } + + /* We pretend that our gamma ramps are of size 256 */ + *gamma_size_out = 256; + + /* Pretend that our gamma ramps are identity mappings */ + for (i = 0; i < 256; i++) + red[i] = green[i] = blue[i] = (CGGammaValue)i / 255; + + return kCGErrorSuccess; +} + + +/** + * Restore each display's gamma ramps to the settings in ColorSync + */ +void +CGDisplayRestoreColorSyncSettings(void) +{ + /* Do nothing */ +} + + +/** + * Get the number of stops in the gamma ramps for a display + * + * @param display The ID of the display + * @return The number of stops in the gamma ramps + */ +uint32_t +CGDisplayGammaTableCapacity(CGDirectDisplayID display) +{ + /* We pretend that our gamma ramps are of size 256 */ + (void) display; + return 256; +} + + +/** + * Release resources used by the backend + */ +void +close_fake_quartz(void) +{ + /* Do nothing */ +} + + +#else +/* Use translation to X RandR */ + + +#include +#include + + + +/** + * Connection to the X RandR display + */ +static xcb_connection_t *restrict connection = NULL; + +/** + * Resouces for the screen + * + * We only have one screen, again this + * is a very sloppy compatibility layer + */ +static xcb_randr_get_screen_resources_current_reply_t *restrict res_reply = NULL; + +/** + * The number of available CRTC:s + */ +static uint32_t crtc_count = 0; + +/** + * List of X RandR CRTC:s + */ +static xcb_randr_crtc_t *restrict crtcs = NULL; + +/** + * The original gamma ramps, used to emulate gamma ramp restoration to system settings + */ +static uint16_t *restrict original_ramps = NULL; + + + +/* xcb violates the rule to never return struct:s */ +#ifdef __GNUC__ +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Waggregate-return" +#endif + + +/** + * Get a list of all online displays on the system + * + * @param max_size The number of elements allocated for `displays_out` + * @param displays_out List ot fill with the ID for each online display on the system + * @param count_out Output parameter for the number of elements stored in `displays_out`, + * if `displays_out` is too small to fit all display ID:s, + * `*count_out` will be `max_size` + * @return `kCGErrorSuccess` on success, and error number of failure + */ +CGError +CGGetOnlineDisplayList(uint32_t max_size, CGDirectDisplayID *restrict displays_out, uint32_t *restrict count_out) +{ + xcb_generic_error_t *error; + xcb_screen_iterator_t iter; + xcb_randr_get_screen_resources_current_cookie_t res_cookie; + xcb_randr_get_crtc_gamma_cookie_t gamma_cookie; + xcb_randr_get_crtc_gamma_reply_t *restrict gamma_reply; + uint32_t i; + + /* Connect to the display and get screen data if not already done so */ + if (!connection) { + /* Connect to the display */ + connection = xcb_connect(NULL, NULL); + /* Get the first screen */ + iter = xcb_setup_roots_iterator(xcb_get_setup(connection)); + /* Get the resources of the screen */ + res_cookie = xcb_randr_get_screen_resources_current(connection, iter.data->root); + res_reply = xcb_randr_get_screen_resources_current_reply(connection, res_cookie, &error); + if (error) { + fprintf(stderr, "Failed to open X connection\n"); + xcb_disconnect(connection); + crtc_count = 0; + return ~kCGErrorSuccess; + } + + /* Get the number of CRTC:s */ + crtc_count = (uint32_t)(res_reply->num_crtcs); + /* Get the CRTC ID:s */ + crtcs = xcb_randr_get_screen_resources_current_crtcs(res_reply); + + /* Allocate memory where we store the gamma ramps as + * they looked when this adjustment method was first + * used. This is used to emulate the functionality of + * `CGDisplayRestoreColorSyncSettings` which restore the + * all gamma ramps on the system to the system settnigs. + */ + original_ramps = malloc(crtc_count * 3 * 256 * sizeof(uint16_t)); + if (!original_ramps) { + perror("malloc"); + xcb_disconnect(connection); + crtc_count = 0; + return ~kCGErrorSuccess; + } + + /* Fill the gamma ramps we just allocated */ + for (i = 0; i < crtc_count; i++) { + /* Read current gamma ramps */ + gamma_cookie = xcb_randr_get_crtc_gamma(connection, crtcs[i]); + gamma_reply = xcb_randr_get_crtc_gamma_reply(connection, gamma_cookie, &error); + if (error) { + fprintf(stderr, "Failed to read gamma ramps.\n"); + xcb_disconnect(connection); + crtc_count = 0; + return ~kCGErrorSuccess; + } + + /* Copy over the gamma ramps to the memory area we have allocated */ +#define __DEST(C) original_ramps + (C + 3 * i) * 256 +#define __SRC(C) xcb_randr_get_crtc_gamma_##C(gamma_reply) + memcpy(__DEST(0), __SRC(red), 256 * sizeof(uint16_t)); + memcpy(__DEST(1), __SRC(green), 256 * sizeof(uint16_t)); + memcpy(__DEST(2), __SRC(blue), 256 * sizeof(uint16_t)); +#undef __SRC +#undef __DEST + + /* Release resouces */ + free(gamma_reply); + } + } + + /* Return CRTC ID:s */ + for (i = 0; i < max_size && i < crtc_count; i++) + displays_out[i] = (CGDirectDisplayID)i; + + /* Return the number of CRTC ID:s we returned */ + *count_out = i; + return kCGErrorSuccess; +} + + +/** + * Set the gamma ramps for a display + * + * @param display The ID of the display + * @param gamma_size The number of stops in gamma ramps + * @param red The gamma ramp for the red channel + * @param green The gamma ramp for the green channel + * @param blue The gamma ramp for the blue channel + * @return `kCGErrorSuccess` on success, and error number of failure + */ +CGError +CGSetDisplayTransferByTable(CGDirectDisplayID display, uint32_t gamma_size, const CGGammaValue *red, + const CGGammaValue *green, const CGGammaValue *blue) +{ + xcb_void_cookie_t gamma_cookie; + uint16_t r_int[256], g_int[256], b_int[256]; + int32_t v; + long i; + + /* This is a sloppy compatibility layer that assumes the gamma ramp size is 256 */ + if (gamma_size != 256) { + fprintf(stderr, "Gamma size should be 256.\n"); + abort(); + } + + /* Translate the gamma ramps from float (CoreGraphics) to 16-bit unsigned integer (X RandR) */ + for (i = 0; i < 256; i++) { + /* Red channel */ + v = (int32_t)(red[i] * UINT16_MAX); + r_int[i] = (uint16_t)(v < 0 ? 0 : v > UINT16_MAX ? UINT16_MAX : v); + + /* Green channel */ + v = (int32_t)(green[i] * UINT16_MAX); + g_int[i] = (uint16_t)(v < 0 ? 0 : v > UINT16_MAX ? UINT16_MAX : v); + + /* Blue channel */ + v = (int32_t)(blue[i] * UINT16_MAX); + b_int[i] = (uint16_t)(v < 0 ? 0 : v > UINT16_MAX ? UINT16_MAX : v); + } + + /* Apply gamma ramps */ + gamma_cookie = xcb_randr_set_crtc_gamma_checked(connection, crtcs[display], (uint16_t)gamma_size, r_int, g_int, b_int); + /* Check for errors */ + return !xcb_request_check(connection, gamma_cookie) ? kCGErrorSuccess : ~kCGErrorSuccess; +} + + +/** + * Get the current gamma ramps for a display + * + * @param display The ID of the display + * @param gamma_size The number of stops you have allocated for the gamma ramps + * @param red Table allocated for the gamma ramp for the red channel + * @param green Table allocated for the gamma ramp for the green channel + * @param blue Table allocated for the gamma ramp for the blue channel + * @param gamma_size_out Output parameter for the actual number of stops in the gamma ramps + * @return `kCGErrorSuccess` on success, and error number of failure + */ +CGError +CGGetDisplayTransferByTable(CGDirectDisplayID display, uint32_t gamma_size, CGGammaValue *restrict red, + CGGammaValue *restrict green, CGGammaValue *restrict blue, uint32_t *restrict gamma_size_out) +{ + xcb_randr_get_crtc_gamma_cookie_t gamma_cookie; + xcb_randr_get_crtc_gamma_reply_t *restrict gamma_reply; + xcb_generic_error_t *error; + uint16_t *restrict r_int; + uint16_t *restrict g_int; + uint16_t *restrict b_int; + long i; + + /* This is a sloppy compatibility layer that assumes the gamma ramp size is 256 */ + if (gamma_size != 256) { + fprintf(stderr, "Gamma size should be 256\n"); + abort(); + } + + /* The gamma ramp size should be returned to the caller */ + *gamma_size_out = 256; + + /* Read current gamma ramps */ + gamma_cookie = xcb_randr_get_crtc_gamma(connection, crtcs[display]); + gamma_reply = xcb_randr_get_crtc_gamma_reply(connection, gamma_cookie, &error); + if (error) { + fprintf(stderr, "Failed to write gamma ramps\n"); + return ~kCGErrorSuccess; + } + + /* Get gamma ramp values */ + r_int = xcb_randr_get_crtc_gamma_red(gamma_reply); + g_int = xcb_randr_get_crtc_gamma_green(gamma_reply); + b_int = xcb_randr_get_crtc_gamma_blue(gamma_reply); + + /* Translate gamma ramps to float format, that is what CoreGraphics uses */ + for (i = 0; i < 256; i++) { + red[i] = (CGGammaValue)r_int[i] / UINT16_MAX; + green[i] = (CGGammaValue)g_int[i] / UINT16_MAX; + blue[i] = (CGGammaValue)b_int[i] / UINT16_MAX; + } + + free(gamma_reply); + return kCGErrorSuccess; +} + + +/** + * Restore each display's gamma ramps to the settings in ColorSync + */ +void +CGDisplayRestoreColorSyncSettings(void) +{ + xcb_generic_error_t *error; + xcb_void_cookie_t gamma_cookie; + uint32_t i; + + /* Restore the gamma ramps on each monitor to + * the ramps that we used when we started */ + for (i = 0; i < crtc_count; i++) { + /* We assume that our gamma ramps are of the size + * 256 (this is a sloppy compatibility layer) */ + gamma_cookie = xcb_randr_set_crtc_gamma_checked(connection, crtcs[i], 256, + &original_ramps[(0 + 3 * i) * 256], + &original_ramps[(1 + 3 * i) * 256], + &original_ramps[(2 + 3 * i) * 256]); + if ((error = xcb_request_check(connection, gamma_cookie))) + fprintf(stderr, "Quartz gamma reset emulation with RandR returned %i\n", error->error_code); + } +} + + +/** + * Get the number of stops in the gamma ramps for a display + * + * @param display The ID of the display + * @return The number of stops in the gamma ramps + */ +uint32_t +CGDisplayGammaTableCapacity(CGDirectDisplayID display) +{ + /* We assume that our gamma ramps are of the size + * 256 (this is a sloppy compatibility layer) */ + (void) display; + return 256; +} + + +#ifdef __GNUC__ +# pragma GCC diagnostic pop +#endif + + +/** + * Release resources used by the backend + */ +void close_fake_quartz_cg(void) +{ + free(res_reply); + res_reply = NULL; + if (connection) { + xcb_disconnect(connection); + connection = NULL; + } + free(original_ramps); + original_ramps = NULL; +} + +#endif diff --git a/fake-quartz-cg.h b/fake-quartz-cg.h new file mode 100644 index 0000000..361a73c --- /dev/null +++ b/fake-quartz-cg.h @@ -0,0 +1,120 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef LIBGAMMA_FAKE_QUARTZ_CG_H +#define LIBGAMMA_FAKE_QUARTZ_CG_H + +#ifndef FAKE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS +# error Including fake-quartz-cg.h without FAKE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS +#endif + + +#ifndef __GNUC__ +# define __attribute__(x) +#endif + + +/* This header file contains some capabilities of + * and , + * 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 + + +/** + * Numerical `typedef` for errors that occur in CoreGraphics calls + */ +typedef int32_t CGError; + +/** + * The call was successful + */ +#define kCGErrorSuccess 0 + +/** + * The data type that is used for the values in the gamma ramps + */ +typedef float CGGammaValue; + +/** + * The data type for display ID:s + */ +typedef uint32_t CGDirectDisplayID; + + +/** + * Get a list of all online displays on the system + * + * @param max_size The number of elements allocated for `displays_out` + * @param displays_out List ot fill with the ID for each online display on the system + * @param count_out Output parameter for the number of elements stored in `displays_out` + * if `displays_out` is too small to fit all display ID:s, + * `*count_out` will be `max_size` + * @return `kCGErrorSuccess` on success, and error number of failure + */ +CGError CGGetOnlineDisplayList(uint32_t, CGDirectDisplayID *restrict, uint32_t *restrict); + +/** + * Set the gamma ramps for a display + * + * @param display The ID of the display + * @param gamma_size The number of stops in gamma ramps + * @param red The gamma ramp for the red channel + * @param green The gamma ramp for the green channel + * @param blue The gamma ramp for the blue channel + * @return `kCGErrorSuccess` on success, and error number of failure + */ +CGError CGSetDisplayTransferByTable(CGDirectDisplayID, uint32_t, const CGGammaValue *, const CGGammaValue *, const CGGammaValue *); + +/** + * Get the current gamma ramps for a display + * + * @param display The ID of the display + * @param gamma_size The number of stops you have allocated for the gamma ramps + * @param red Table allocated for the gamma ramp for the red channel + * @param green Table allocated for the gamma ramp for the green channel + * @param blue Table allocated for the gamma ramp for the blue channel + * @param gamma_size_out Output parameter for the actual number of stops in the gamma ramps + * @return `kCGErrorSuccess` on success, and error number of failure + */ +CGError CGGetDisplayTransferByTable(CGDirectDisplayID, uint32_t, CGGammaValue *restrict, + CGGammaValue *restrict, CGGammaValue *restrict, uint32_t *restrict); + +/** + * Restore each display's gamma ramps to the settings in ColorSync + */ +void CGDisplayRestoreColorSyncSettings(void); + +/** + * Get the number of stops in the gamma ramps for a display + * + * @param display The ID of the display + * @return The number of stops in the gamma ramps + */ +uint32_t CGDisplayGammaTableCapacity(CGDirectDisplayID) __attribute__((const)); + + +/* The follow part most only be used when this module is used, + * it cannot be used when the real CoreGraphics is used. + * CoreGraphics does not have this function, it is added so + * that there is a way to cleanly close the X connection + * and free resources needed by this module. */ + +/** + * Release resources used by the backend + */ +void close_fake_quartz_cg(void); + + + +#ifndef __GNUC__ +# undef __attribute__ +#endif + +#endif diff --git a/fake-w32-gdi.c b/fake-w32-gdi.c new file mode 100644 index 0000000..68847ca --- /dev/null +++ b/fake-w32-gdi.c @@ -0,0 +1,479 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef HAVE_LIBGAMMA_METHOD_W32_GDI +# error Compiling fake-w32-gdi.c without FAKE_LIBGAMMA_METHOD_W32_GDI +#endif + +/* This file very sloppily translates Windows GDI calls to X RandR calls. + * It should by no means be used, without additional modification, as a + * part of a compatibility layer. The purpose of this file is only to make + * it possible to test for logical errors in Windows specific code on + * a GNU/Linux system under X. */ + + +#include "fake-w32-gdi.h" + +#include +#include +#include + + +#ifndef HAVE_LIBGAMMA_METHOD_X_RANDR +/* Use dummy translation */ + + + +/** + * Get the device context for a window or the entire screen + * + * @param hWnd The windows, `NULL` for the entire screen + * @return The device context + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144871(v=vs.85).aspx + */ +HDC +GetDC(HWND hWnd) +{ + /* Just a non-NULL value */ + (void) hWnd; + return (HDC *)16; +} + + +/** + * Free a device context + * + * @param hWnd The window whose device context is `hDC`, `NULL` for the entire screen + * @param hDC The device context to free + * @return Whether the call was successful + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd162920(v=vs.85).aspx + */ +int +ReleaseDC(HWND hWnd, HDC hDC) +{ + /* Always successful */ + (void) hWnd; + (void) hDC; + return 1; +} + + + +/** + * Get information (capabilities) for a device context + * + * @param hDC The device context + * @param nIndex The information to retrieve, may be `COLORMGMTCAPS` + * @return The details of the queried information, can return `CM_GAMMA_RAMP` + * if `nIndex` is `COLORMGMTCAPS` + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877(v=vs.85).aspx + */ +int +GetDeviceCaps(HDC hDC, int nIndex) +{ + /* We have gamma ramps if the user asks for colour management capabilities */ + (void) hDC; + return CM_GAMMA_RAMP + nIndex - COLORMGMTCAPS; +} + + +/** + * Set the gamma ramps for a device + * + * @param hDC The device context + * @param lpRamp The gamma ramps joined in the order: red, green, blue + * This is a `WORD*` casted to `void*` + * @return Whether the call was successful + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd372194(v=vs.85).aspx + */ +BOOL +SetDeviceGammaRamp(HDC hDC, LPVOID restrict lpRamp) +{ + /* Always successful */ + (void) hDC; + (void) lpRamp; + return TRUE; +} + + +/** + * Get the current gamma ramps for a device + * + * @param hDC The device context + * @param lpRamp The output for the gamma ramps joined in the order: red, green, blue + * This is a `WORD *` casted to `void *` + * @return Whether the call was successful + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd316946(v=vs.85).aspx + */ +BOOL +GetDeviceGammaRamp(HDC hDC, LPVOID restrict lpRamp) +{ + /* Always successful */ + (void) hDC; + (void) lpRamp; + return TRUE; +} + + + +/** + * Get the context for a device + * + * @param lpszDriver The driver or a display device, use "DISPLAY" if you want a display + * @param lpszDevice The name of the device. If you want a display use can use the member + * name `DeviceName` in the third parameter, an output parameter, of + * `EnumDisplayDevices` + * @param lpszOutput We will always use `NULL` here + * @param lpInitData We will always use `NULL` here; this should actually by a `const DEVMODE *` + * @return The context for the device + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183490(v=vs.85).aspx + */ +HDC +CreateDC(LPCTSTR restrict lpszDriver, LPCTSTR restrict lpszDevice, LPCTSTR restrict lpszOutput, const void *restrict lpInitData) +{ + /* `NULL` if not asking for a CRTC, otherwise a non-NULL value */ + (void) lpszOutput; + (void) lpInitData; + (void) lpszDevice; + return !strcmp(lpszDriver, "DISPLAY") ? (HDC *)16 : NULL; +} + + +/** + * Get a display device by its name or index + * + * @param lpDevice The name of the device, use `NULL` to base the call on `iDevNum` instead + * @param iDevNum The index of the device + * @param lpDisplayDevice Output for the found device + * @param dwFlags Flags, we will always use zero + * @return Whether the call was successful; zero is also returned if `iDevNum` + * is greater than the largest device index on the system + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd162609(v=vs.85).aspx + */ +BOOL +EnumDisplayDevices(LPCTSTR lpDevice, restrict DWORD iDevNum, PDISPLAY_DEVICE restrict lpDisplayDevice, DWORD dwFlags) +{ + (void) dwFlags; + /* Check correctness of `lpDevice` */ + if (lpDevice) { + fprintf(stderr, "lpDevice (argument 1) for EnumDisplayDevices should be NULL\n"); + abort(); + return FALSE; + } + /* Pretend that we have two CRTC:s */ + if (iDevNum >= 2) + return FALSE; + /* Check correctness of `lpDisplayDevice` */ + if (lpDisplayDevice->cb != sizeof(DISPLAY_DEVICE)) { + fprintf(stderr, "lpDisplayDevice->cb for EnumDisplayDevices is not sizeof(DISPLAY_DEVICE)\n"); + abort(); + return FALSE; + } + /* Store an arbitrary name for the monitor */ + strcmp(lpDisplayDevice->DeviceName, "some monitor"); + /* The connector is always enabled */ + lpDisplayDevice->StateFlags = DISPLAY_DEVICE_ACTIVE; + return TRUE; +} + + +#else +/* Use translation to X RandR */ + + +#include +#include + + +/** + * The gamma ramp size that devices will + * always have in Windows GDI + */ +#define GAMMA_RAMP_SIZE 256 + + +/** + * Connection to the X RandR display + */ +static xcb_connection_t *restrict connection = NULL; + +/** + * Resouces for the screen + * + * We only have one screen, again this + * is a very sloppy compatibility layer + */ +static xcb_randr_get_screen_resources_current_reply_t *restrict res_reply = NULL; + +/** + * The number of available CRTC:s, -1 if not known yet + */ +static ssize_t crtc_count = -1; + +/** + * List of X RandR CRTC:s + */ +static xcb_randr_crtc_t *restrict crtcs = NULL; + +/** + * The number of opened CRTC:s + */ +static size_t dc_count = 0; + + + +/** + * Get the device context for a window or the entire screen + * + * @param hWnd The windows, `NULL` for the entire screen + * @return The device context + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144871(v=vs.85).aspx + */ +HDC +GetDC(HWND hWnd) +{ + /* Return the primary CRTC */ + (void) hWnd; + return CreateDC(TEXT("DISPLAY"), "0", NULL, NULL); +} + + +/** + * Free a device context + * + * @param hWnd The window whose device context is `hDC`, `NULL` for the entire screen + * @param hDC The device context to free + * @return Whether the call was successful + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd162920(v=vs.85).aspx + */ +int +ReleaseDC(HWND hWnd, HDC hDC) +{ + /* Disconnect from the RandR display when all monitors have been closed */ + (void) hWnd; + (void) hDC; + if (!dc_count--) { + if (connection) { + xcb_disconnect(connection); + connection = NULL; + } + free(res_reply); + res_reply = NULL; + } + return 1; +} + + + +/** + * Get information (capabilities) for a device context + * + * @param hDC The device context + * @param nIndex The information to retrieve, may be `COLORMGMTCAPS` + * @return The details of the queried information, can return `CM_GAMMA_RAMP` + * if `nIndex` is `COLORMGMTCAPS` + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877(v=vs.85).aspx + */ +int +GetDeviceCaps(HDC hDC, int nIndex) +{ + /* We have gamma ramps if the user asks for colour management capabilities */ + (void) hDC; + return CM_GAMMA_RAMP + nIndex - COLORMGMTCAPS; +} + + +/* xcb violates the rule to never return `struct`:s */ +#ifdef __GNUC__ +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Waggregate-return" +#endif + + +/** + * Set the gamma ramps for a device + * + * @param hDC The device context + * @param lpRamp The gamma ramps joined in the order: red, green, blue + * This is a `WORD *` casted to `void *` + * @return Whether the call was successful + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd372194(v=vs.85).aspx + */ +BOOL +SetDeviceGammaRamp(HDC hDC, LPVOID restrict lpRamp) +{ + /* We assume that our gamma ramps are of the same size + * as used by Windows GDI (we are so sloppy) */ + xcb_void_cookie_t gamma_cookie = + xcb_randr_set_crtc_gamma_checked(connection, *(xcb_randr_crtc_t *)hDC, GAMMA_RAMP_SIZE, + &((uint16_t *)lpRamp)[0 * GAMMA_RAMP_SIZE], + &((uint16_t *)lpRamp)[1 * GAMMA_RAMP_SIZE], + &((uint16_t *)lpRamp)[2 * GAMMA_RAMP_SIZE)]; + xcb_generic_error_t *error = xcb_request_check(connection, gamma_cookie); + return !error ? TRUE : FALSE; +} + + +/** + * Get the current gamma ramps for a device + * + * @param hDC The device context + * @param lpRamp The output for the gamma ramps joined in the order: red, green, blue + * This is a `WORD *` casted to `void *` + * @return Whether the call was successful + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd316946(v=vs.85).aspx + */ +BOOL +GetDeviceGammaRamp(HDC hDC, LPVOID restrict lpRamp) +{ + xcb_randr_get_crtc_gamma_cookie_t gamma_cookie; + xcb_randr_get_crtc_gamma_reply_t *restrict gamma_reply; + xcb_generic_error_t *error; + + /* Read current gamma ramps */ + gamma_cookie = xcb_randr_get_crtc_gamma(connection, *(xcb_randr_crtc_t *)hDC); + gamma_reply = xcb_randr_get_crtc_gamma_reply(connection, gamma_cookie, &error); + if (error) + return FALSE; + +#define DEST_RAMP(I) (&((uint16_t *)lpRamp)[(I) * GAMMA_RAMP_SIZE]) +#define SRC_RAMP(C) (xcb_randr_get_crtc_gamma_##C(gamma_reply)) + + /* Copy the ramps into the output parameter (coalesced) */ + memcpy(DEST_RAMP(0), SRC_RAMP(red), GAMMA_RAMP_SIZE * sizeof(uint16_t)); + memcpy(DEST_RAMP(1), SRC_RAMP(green), GAMMA_RAMP_SIZE * sizeof(uint16_t)); + memcpy(DEST_RAMP(2), SRC_RAMP(blue), GAMMA_RAMP_SIZE * sizeof(uint16_t)); + +#undef SRC_RAMP +#undef DEST_RAMP + + free(gamma_reply); + return TRUE; +} + + + +/** + * Get the context for a device + * + * @param lpszDriver The driver or a display device, use "DISPLAY" if you want a display + * @param lpszDevice The name of the device. If you want a display use can use the member + * name `DeviceName` in the third parameter, an output parameter, of + * `EnumDisplayDevices` + * @param lpszOutput We will always use `NULL` here + * @param lpInitData We will always use `NULL` here; this should actually by a `const DEVMODE *` + * @return The context for the device + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183490(v=vs.85).aspx + */ +HDC +CreateDC(LPCTSTR restrict lpszDriver, LPCTSTR restrict lpszDevice, LPCTSTR restrict lpszOutput, const void *restrict lpInitData) +{ + int crtc_index = atoi(lpszDevice); + xcb_generic_error_t *error; + xcb_screen_iterator_t iter; + xcb_randr_get_screen_resources_current_cookie_t res_cookie; + + (void) lpszOutput; + (void) lpInitData; + + /* Check correctness of input */ + if (strcmp(lpszDriver, "DISPLAY")) + return NULL; + + /* Connect to the display and get screen data if not already done so */ + if (!dc_count) { + /* Connect to the display */ + connection = xcb_connect(NULL, NULL); + /* Get the first screen */ + iter = xcb_setup_roots_iterator(xcb_get_setup(connection)); + /* Get the resources of the screen */ + res_cookie = xcb_randr_get_screen_resources_current(connection, iter.data->root); + res_reply = xcb_randr_get_screen_resources_current_reply(connection, res_cookie, &error); + if (error) { + fprintf(stderr, "Failed to open X connection\n"); + xcb_disconnect(connection); + crtc_count = -1; + return NULL; + } + + /* Get the number of CRTC:s */ + crtc_count = res_reply->num_crtcs; + /* Get the CRTC ID:s */ + crtcs = xcb_randr_get_screen_resources_current_crtcs(res_reply); + } + + /* Was the index too high */ + if (crtc_index >= crtc_count) { + /* Disconnect and release resouces and mark that + we do not know the number of available CRTC:s + if we have not opened any monitors yet */ + if (!dc_count) { + xcb_disconnect(connection); + free(res_reply); + res_reply = NULL; + crtc_count = -1; + } + return NULL; + } + + /* We have opened a new CRTC */ + dc_count++; + /* Return the ID of the CRTC */ + return &crtcs[crtc_index]; +} + + +#ifdef __GNUC__ +# pragma GCC diagnostic pop +#endif + + +/** + * Get a display device by its name or index + * + * @param lpDevice The name of the device, use `NULL` to base the call on `iDevNum` instead + * @param iDevNum The index of the device + * @param lpDisplayDevice Output for the found device + * @param dwFlags Flags, we will always use zero + * @return Whether the call was successful; zero is also returned if `iDevNum` + * is greater than the largest device index on the system + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd162609(v=vs.85).aspx + */ +BOOL +EnumDisplayDevices(LPCTSTR restrict lpDevice, DWORD iDevNum, PDISPLAY_DEVICE restrict lpDisplayDevice, DWORD dwFlags) +{ + size_t count = (size_t)crtc_count; + + (void) dwFlags; + + /* Check the correctness of the input */ + if (lpDevice) { + fprintf(stderr, "lpDevice (argument 1) for EnumDisplayDevices should be NULL\n"); + abort(); + return FALSE; + } + /* Do we know how many CRTC:s are available? */ + if (crtc_count < 0) { + /* If not open the first CRTC so that will be figured out */ + if (!GetDC(NULL)) + return FALSE; + count = (size_t)crtc_count; + /* Close the primary monitor that we just closed */ + ReleaseDC(NULL, NULL); + } + /* Check that the request CRTC exists */ + if (iDevNum >= count) + return FALSE; + /* Check the correctness of the input */ + if (lpDisplayDevice->cb != sizeof(DISPLAY_DEVICE)) { + fprintf(stderr, "lpDisplayDevice->cb for EnumDisplayDevices is not sizeof(DISPLAY_DEVICE)\n"); + abort(); + return FALSE; + } + /* Store name for the CRTC */ + sprintf(lpDisplayDevice->DeviceName, "%i", iDevNum); + /* The connector that the CRTC belongs to is enabled */ + lpDisplayDevice->StateFlags = DISPLAY_DEVICE_ACTIVE; + return TRUE; +} + + +#endif diff --git a/fake-w32-gdi.h b/fake-w32-gdi.h new file mode 100644 index 0000000..bcc2fa9 --- /dev/null +++ b/fake-w32-gdi.h @@ -0,0 +1,248 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef LIBGAMMA_FAKE_W32_GDI_H +#define LIBGAMMA_FAKE_W32_GDI_H + +#ifndef FAKE_LIBGAMMA_METHOD_W32_GDI +# error Including fake-w32-gdi.h without FAKE_LIBGAMMA_METHOD_W32_GDI +#endif + + +#ifndef __GNUC__ +# define __attribute__(x) +#endif + +#include + + +/** + * One of the Windows `typedef`:s for unsigned 16-bit integer + * This is the one used in these functions + * + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#WORD + */ +typedef uint16_t WORD; + +/** + * One of the Windows `typedef`:s for unsigned 32-bit integer + * This is the one used in these functions + * + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#DWORD + */ +typedef uint32_t DWORD; + +/** + * One of the Windows `typedef` for booleanic values, the `int` variant + * + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#BOOL + */ +typedef int BOOL; + +/** + * Windows `typedef` for a device context (the 'h' stands for 'handle') + * + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#HDC + */ +typedef void *HDC; + +/** + * Windows `typedef` for a window (the 'h' stands for 'handle') + * + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#HWND + */ +typedef void *HWND; + +/** + * One of the Windows `typedef`:s for `void *`, a generic pointer + * + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#LPVOID + */ +typedef void *LPVOID; + +/** + * A silly Windows `typedef` for a `const char *`, a constant string + * + * It is also defined as NUL-terminated, but so are all C-strings + * and it makes no difference. However if `UNICODE` were to be defined + * it would be `const wchar_t *`. + * + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#LPCTSTR + */ +typedef const char *LPCTSTR; + +/** + * Even more silly. A Windows `typedef` for either `wchar_t` or + * `char`, depending on whether `UNICODE` is defined (we will + * assume `UNICODE` is not definied because that is just silly) + * + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#TCHAR + */ +typedef char TCHAR; + +/** + * Apperently we need `TRUE` to be defined to understand code, + * at least in Windows... + * + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#BOOL + */ +#define TRUE 1 + +/** + * Apperently we need `FALSE` to be defined to understand code, + * at least in Windows... + * + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#BOOL + */ +#define FALSE 0 + + + +/** + * Get the device context for a window or the entire screen + * + * @param hWnd The window, `NULL` for the entire screen + * @return The device context + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144871(v=vs.85).aspx + */ +HDC GetDC(HWND); + +/** + * Free a device context + * + * @param hWnd The window whose device context is `hDC`, `NULL` for the entire screen + * @param hDC The device context to free + * @return Whether the call was successful + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd162920(v=vs.85).aspx + */ +int ReleaseDC(HWND, HDC); + + +/** + * Get information (capabilities) for a device context + * + * @param hDC The device context + * @param nIndex The information to retrieve, may be `COLORMGMTCAPS` + * @return The details of the queried information, can return `CM_GAMMA_RAMP` + * if `nIndex` is `COLORMGMTCAPS` + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877(v=vs.85).aspx + */ +int GetDeviceCaps(HDC, int) __attribute__((const)); + +/** + * Colour management capabilities + * + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877(v=vs.85).aspx + */ +#define COLORMGMTCAPS 1 + +/** + * Colour management capabilities: has gamma ramps + * + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877(v=vs.85).aspx + */ +#define CM_GAMMA_RAMP 1 + + +/** + * Set the gamma ramps for a device + * + * @param hDC The device context + * @param lpRamp The gamma ramps joined in the order: red, green, blue; + * this is a `WORD *` casted to `void *` + * @return Whether the call was successful + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd372194(v=vs.85).aspx + */ +BOOL SetDeviceGammaRamp(HDC, LPVOID restrict); + +/** + * Get the current gamma ramps for a device + * + * @param hDC The device context + * @param lpRamp The output for the gamma ramps joined in the order: red, green, blue + * This is a `WORD *` casted to `void *` + * @return Whether the call was successful + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd316946(v=vs.85).aspx + */ +BOOL GetDeviceGammaRamp(HDC, LPVOID restrict); + + +/** + * Get the context for a device + * + * @param lpszDriver The driver or a display device, use "DISPLAY" if you want a display + * @param lpszDevice The name of the device. If you want a display use can use the member + * name `DeviceName` in the third parameter, an output parameter, of + * `EnumDisplayDevices`. + * @param lpszOutput We will always use `NULL` here + * @param lpInitData We will always use `NULL` here; this should actually by a `const DEVMODE *` + * @return The context for the device + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183490(v=vs.85).aspx + */ +HDC CreateDC(LPCTSTR restrict, LPCTSTR restrict, LPCTSTR restrict, const void *restrict); + +/** + * This macro does not seem to have an official documentation that is correct + * + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183490(v=vs.85).aspx + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd374074(v=vs.85).aspx + */ +#define TEXT(X) ((LPCTSTR)(X)) + + +/** + * Information about a display device + * + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183569(v=vs.85).aspx + */ +typedef struct { + /** + * Set this to `sizeof(DISPLAY_DEVICE)` + */ + DWORD cb; + + /** + * The name of the device + */ + TCHAR DeviceName[32]; + + /** + * The status of the device, can include `DISPLAY_DEVICE_ACTIVE` + */ + DWORD StateFlags; + +} DISPLAY_DEVICE; + +/** + * Appearently we are incapable of putting asterisks at the + * end of types names, so istead we preprend them with 'P' + * + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183569(v=vs.85).aspx + */ +typedef DISPLAY_DEVICE *PDISPLAY_DEVICE; + +/** + * The monitor is "on" + * + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183569(v=vs.85).aspx + */ +#define DISPLAY_DEVICE_ACTIVE 1 + +/** + * Get a display device by its name or index + * + * @param lpDevice The name of the device, use `NULL` to base the call on `iDevNum` instead + * @param iDevNum The index of the device + * @param lpDisplayDevice Output for the found device + * @param dwFlags Flags, we will always use zero + * @return Whether the call was successful; zero is also returned if `iDevNum` + * is greater than the largest device index on the system + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd162609(v=vs.85).aspx + */ +BOOL EnumDisplayDevices(LPCTSTR restrict, DWORD, PDISPLAY_DEVICE restrict, DWORD) + + + +#ifndef __GNUC__ +# undef __attribute__ +#endif + +#endif diff --git a/gamma-dummy.c.gpp b/gamma-dummy.c.gpp new file mode 100644 index 0000000..4cc758d --- /dev/null +++ b/gamma-dummy.c.gpp @@ -0,0 +1,993 @@ +/* -*- c -*- */ +/* See LICENSE file for copyright and license details. */ +#ifndef HAVE_LIBGAMMA_METHOD_DUMMY +# error Compiling gamma-dummy.c without HAVE_LIBGAMMA_METHOD_DUMMY +#endif + +#include "gamma-dummy.h" + +#include "libgamma-error.h" +#include "libgamma-method.h" +#include "libgamma-facade.h" +#include "edid.h" +#include "gamma-helper.h" + +#include +#include +#include +#include + + + +/** + * Configuration set for the dummy adjustment method + */ +typedef struct libgamma_dummy_configurations { + /** + * The method's capabilities + * + * Some fields are ignored: + * - real + * - fake + */ + libgamma_method_capabilities_t capabilities; + + /** + * Template for CRTC:s information + * + * Some fields are ignored: + * - width_mm_edid + * - width_mm_edid_error + * - height_mm_edid + * - height_mm_edid_error + * - gamma_red + * - gamma_green + * - gamma_blue + * - gamma_error + */ + libgamma_crtc_information_t crtc_info_template; + + /** + * The adjustment method to use + */ + int real_method; + + /** + * The number of sites on the system + */ + size_t site_count; + + /** + * The number of paritions on a site before it has been configured + */ + size_t default_partition_count; + + /** + * The number of CRTC:s on a paritions before it has been configured + */ + size_t default_crtc_count; + + /** + * Whether the sites should be inherited from the real method + */ + unsigned inherit_sites : 1; + + /** + * Whether the partitions should be inherited from the real method + */ + unsigned inherit_partition_count : 1; + + /** + * Whether the CRTC:s should be inherited from the real method + */ + unsigned inherit_crtc_count : 1; + + /** + * When a site has been created, stall until the partition count has + * been configured + */ + unsigned stall_for_partition_count : 1; + + /** + * When a parition has been created, stall until the CRTC count has + * been configured + */ + unsigned stall_for_crtc_count : 1; + + /** + * Methods should stall until the system has been configured + * unless $LIBGAMMA_DUMMY_STALL is not true + */ + unsigned stalled_start : 1; + + /** + * Whether to print what is going on in the phony system + */ + unsigned verbose : 1; + +} libgamma_dummy_configurations_t; + + +/** + * Dummy adjustment method internal data for a CRTC + */ +typedef struct libgamma_dummy_crtc { + /** + * The gamma ramp for the red channel + */ + void *restrict gamma_red; + + /** + * The gamma ramp for the green channel + */ + void *restrict gamma_green; + + /** + * The gamma ramp for the blue channel + */ + void *restrict gamma_blue; + + /** + * Information about the CRTC and monitor + * + * Some fields are ignored: + * - width_mm_edid + * - width_mm_edid_error + * - height_mm_edid + * - height_mm_edid_error + * - gamma_red + * - gamma_green + * - gamma_blue + * - gamma_error + */ + libgamma_crtc_information_t info; + + /** + * Partition state that contains this information + */ + libgamma_crtc_state_t *state; + +} libgamma_dummy_crtc_t; + + +/** + * Dummy adjustment method internal data for a partition + */ +typedef struct libgamma_dummy_partition { + /** + * The CRTC:s on the system + */ + libgamma_dummy_crtc_t *crtcs; + + /** + * The number of CRTC:s on the system + */ + size_t crtc_count; + + /** + * Partition state that contains this information + */ + libgamma_partition_state_t *state; + +} libgamma_dummy_partition_t; + + +/** + * Dummy adjustment method internal data for a site + */ +typedef struct libgamma_dummy_site { + /** + * The partitions on the system + */ + libgamma_dummy_partition_t *partitions; + + /** + * The number of partitions on the system + */ + size_t partition_count; + + /** + * Site state that contains this information + */ + libgamma_site_state_t *state; + +} libgamma_dummy_site_t; + + + +/** + * Restore the gamma ramps for a CRTC to the system settings for that CRTC + * and ignore the method's capabilities + * + * @param this The CRTC data + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +static int libgamma_dummy_crtc_restore_forced(libgamma_dummy_crtc_t *restrict data); + + + +/** + * Configurations for the dummy adjustment method. + */ +static libgamma_dummy_configurations_t libgamma_dummy_configurations = { + .capabilities = { + .crtc_information = (1 << LIBGAMMA_CRTC_INFO_COUNT) - 1, + .default_site_known = 1, + .multiple_sites = 1, + .multiple_partitions = 1, + .multiple_crtcs = 1, + .partitions_are_graphics_cards = 1, + .site_restore = 1, + .partition_restore = 1, + .crtc_restore = 1, + .identical_gamma_sizes = 0, + .fixed_gamma_size = 0, + .fixed_gamma_depth = 0 + }, + .crtc_info_template = { + .edid = NULL, + .edid_length = 0, + .edid_error = LIBGAMMA_EDID_NOT_FOUND, + .width_mm = 400, + .width_mm_error = 0, + .height_mm = 300, + .height_mm_error = 0, + .red_gamma_size = 1024, + .green_gamma_size = 2048, + .blue_gamma_size = 512, + .gamma_size_error = 0, + .gamma_depth = 64, + .gamma_depth_error = 0, + .gamma_support = 1, + .gamma_support_error = 0, + .subpixel_order = LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_RGB, + .subpixel_order_error = 0, + .active = 1, + .active_error = 0, + .connector_name = NULL, + .connector_name_error = LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED, + .connector_type = LIBGAMMA_CONNECTOR_TYPE_Unknown, + .connector_type_error = 0 + }, + .real_method = LIBGAMMA_METHOD_DUMMY, + .site_count = 2, + .default_partition_count = 2, + .default_crtc_count = 2, + .inherit_sites = 1, + .inherit_partition_count = 1, + .inherit_crtc_count = 1, + .stall_for_partition_count = 0, + .stall_for_crtc_count = 0, + .stalled_start = 1, + .verbose = 0 +}; + + + +/** + * Return the capabilities of the adjustment method + * + * @param this The data structure to fill with the method's capabilities + */ +void +libgamma_dummy_method_capabilities(libgamma_method_capabilities_t *restrict this) +{ + int real_method = libgamma_dummy_configurations.real_method; + *this = libgamma_dummy_configurations.capabilities; + this->real = real_method != LIBGAMMA_METHOD_DUMMY; + this->fake = this->real; + this->auto_restore = real_method == LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS; +} + + +/** + * Initialise an allocated site state + * + * @param this The site state to initialise + * @param site The site identifier, unless it is `NULL` it must a + * `free`:able. Once the state is destroyed the library + * will attempt to free it. There you should not free + * it yourself, and it must not be a string constant + * or allocate on the stack. Note however that it will + * not be free:d if this function fails. + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_dummy_site_initialise(libgamma_site_state_t *restrict this, char *restrict site) +{ + libgamma_dummy_site_t *data = NULL; + size_t i, sites, crtcs; + + sites = libgamma_dummy_configurations.site_count; + if (!libgamma_dummy_configurations.capabilities.multiple_sites) + sites = !!sites; + this->data = NULL; + + if (site && *site && (atoll(site) < 0 || sites <= (unsigned long long)atoll(site))) + return LIBGAMMA_NO_SUCH_SITE; + + data = malloc(sizeof(libgamma_dummy_site_t)); + if (!data) + goto fail; + + this->data = data; + data->state = this; + + data->partition_count = libgamma_dummy_configurations.default_partition_count; + if (!libgamma_dummy_configurations.capabilities.multiple_partitions) + data->partition_count = !!data->partition_count; + + crtcs = libgamma_dummy_configurations.default_crtc_count; + if (!libgamma_dummy_configurations.capabilities.multiple_crtcs) + crtcs = !!crtcs; + + data->partitions = malloc(data->partition_count * sizeof(libgamma_dummy_partition_t)); + if (!data->partitions) + goto fail; + + for (i = 0; i < data->partition_count; i++) + data->partitions[i].crtc_count = crtcs; + + this->partitions_available = data->partition_count; + + return 0; + +fail: + free(data); + this->data = NULL; + return LIBGAMMA_ERRNO_SET; +} + + +/** + * Release all resources held by a site state + * + * @param this The site state + */ +void +libgamma_dummy_site_destroy(libgamma_site_state_t *restrict this) +{ + libgamma_dummy_site_t *data = this->data; + if (!data) + return; + + free(data->partitions); + free(data); +} + + +/** + * Restore the gamma ramps all CRTC:s with a site to the system settings + * + * @param this The site state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_dummy_site_restore(libgamma_site_state_t *restrict this) +{ + libgamma_dummy_site_t *data = this->data; + size_t i, j; + + if (!libgamma_dummy_configurations.capabilities.site_restore) { + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; + } + + for (j = 0; j < data->partition_count; j++) + for (i = 0; i < data->partitions[j].crtc_count; i++) + if (libgamma_dummy_crtc_restore_forced(data->partitions[j].crtcs + i) < 0) + return -1; + + return 0; +} + + + +/** + * Initialise an allocated partition state + * + * @param this The partition state to initialise + * @param site The site state for the site that the partition belongs to + * @param partition The the index of the partition within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_dummy_partition_initialise(libgamma_partition_state_t *restrict this, + libgamma_site_state_t *restrict site, size_t partition) +{ + libgamma_crtc_information_t template = libgamma_dummy_configurations.crtc_info_template; + libgamma_dummy_site_t *site_data = site->data; + libgamma_dummy_partition_t *data = &site_data->partitions[partition]; + libgamma_dummy_crtc_t *crtc_data; + size_t i, n; + + this->data = NULL; + + if (partition >= site_data->partition_count) + return LIBGAMMA_NO_SUCH_PARTITION; + + this->data = data; + data->state = this; + + data->crtcs = calloc(data->crtc_count, sizeof(libgamma_dummy_crtc_t)); + if (!data->crtcs) + goto fail; + for (i = 0; i < data->crtc_count; i++) { + crtc_data = data->crtcs + i; + crtc_data->info = template; + + /* Duplicate strings */ + if (crtc_data->info.edid) { + crtc_data->info.edid = malloc(crtc_data->info.edid_length * sizeof(char)); + if (!crtc_data->info.edid) + goto fail; + memcpy(crtc_data->info.edid, template.edid, crtc_data->info.edid_length * sizeof(char)); + } + if (crtc_data->info.connector_name) { + n = strlen(crtc_data->info.connector_name); + crtc_data->info.connector_name = malloc((n + 1) * sizeof(char)); + if (crtc_data->info.connector_name == NULL) + goto fail; + memcpy(crtc_data->info.connector_name, template.connector_name, (n + 1) * sizeof(char)); + } + } + + this->crtcs_available = data->crtc_count; + + return 0; + +fail: + for (i = 0; i < data->crtc_count; i++) { + free(data->crtcs[i].info.edid); + free(data->crtcs[i].info.connector_name); + } + free(data->crtcs); + data->crtcs = NULL; + return LIBGAMMA_ERRNO_SET; +} + + +/** + * Release all resources held by a partition state + * + * @param this The partition state + */ +void +libgamma_dummy_partition_destroy(libgamma_partition_state_t *restrict this) +{ + libgamma_dummy_partition_t *data = this->data; + size_t i; + + if (data) { + for (i = 0; i < data->crtc_count; i++) { + free(data->crtcs[i].info.edid); + free(data->crtcs[i].info.connector_name); + } + free(data->crtcs); + data->crtcs = NULL; + } +} + + +/** + * Restore the gamma ramps all CRTC:s with a partition to the system settings + * + * @param this The partition state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_dummy_partition_restore(libgamma_partition_state_t *restrict this) +{ + libgamma_dummy_partition_t *data = this->data; + size_t i; + + if (!libgamma_dummy_configurations.capabilities.partition_restore) { + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; + } + + for (i = 0; i < data->crtc_count; i++) + if (libgamma_dummy_crtc_restore_forced(data->crtcs + i) < 0) + return -1; + + return 0; +} + + + +/** + * Initialise an allocated CRTC state + * + * @param this The CRTC state to initialise + * @param partition The partition state for the partition that the CRTC belongs to + * @param crtc The the index of the CRTC within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_dummy_crtc_initialise(libgamma_crtc_state_t *restrict this, + libgamma_partition_state_t *restrict partition, size_t crtc) +{ + libgamma_dummy_partition_t *partition_data = partition->data; + libgamma_dummy_crtc_t *data = &partition_data->crtcs[crtc]; + size_t stop_size; + + this->data = NULL; + + if (crtc >= partition_data->crtc_count) + return LIBGAMMA_NO_SUCH_CRTC; + + this->data = data; + data->state = this; + + if (data->info.gamma_depth == -1) + stop_size = sizeof(float); + else if (data->info.gamma_depth == -2) + stop_size = sizeof(double); + else + stop_size = (size_t)data->info.gamma_depth / 8; + + data->gamma_red = malloc(data->info.red_gamma_size * stop_size); + if (!data->gamma_red) + goto fail; + data->gamma_green = malloc(data->info.green_gamma_size * stop_size); + if (!data->gamma_green) + goto fail; + data->gamma_blue = malloc(data->info.blue_gamma_size * stop_size); + if (!data->gamma_blue) + goto fail; + + return libgamma_dummy_crtc_restore_forced(data); + +fail: + free(data->gamma_red), data->gamma_red = NULL; + free(data->gamma_green), data->gamma_green = NULL; + free(data->gamma_blue), data->gamma_blue = NULL; + return LIBGAMMA_ERRNO_SET; +} + + +/** + * Release all resources held by a CRTC state + * + * @param this The CRTC state + */ +void +libgamma_dummy_crtc_destroy(libgamma_crtc_state_t *restrict this) +{ + libgamma_dummy_crtc_t *data = this->data; + if (data) { + free(data->gamma_red), data->gamma_red = NULL; + free(data->gamma_green), data->gamma_green = NULL; + free(data->gamma_blue), data->gamma_blue = NULL; + } +} + + +/** + * Restore the gamma ramps for a CRTC to the system settings for that CRTC + * + * @param this The CRTC state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_dummy_crtc_restore(libgamma_crtc_state_t *restrict this) +{ + if (!libgamma_dummy_configurations.capabilities.crtc_restore) { + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; + } + return libgamma_dummy_crtc_restore_forced(this->data); +} + + +/** + * Restore the gamma ramps for a CRTC to the system settings for that CRTC + * and ignore the method's capabilities + * + * @param this The CRTC data + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +static int +libgamma_dummy_crtc_restore_forced(libgamma_dummy_crtc_t *restrict data) +{ + size_t rn = data->info. red_gamma_size; + size_t gn = data->info.green_gamma_size; + size_t bn = data->info. blue_gamma_size; + size_t i; + + if (!data->gamma_red) + return 0; + +#define __reset_ramps(TYPE, MAX)\ + do {\ + TYPE *red = data->gamma_red;\ + TYPE *green = data->gamma_green;\ + TYPE *blue = data->gamma_blue;\ + for (i = 0; i < rn; i++) red [i] = (TYPE)((double)(MAX) * ((double)i / (double)(rn - 1)));\ + for (i = 0; i < gn; i++) green[i] = (TYPE)((double)(MAX) * ((double)i / (double)(gn - 1)));\ + for (i = 0; i < bn; i++) blue [i] = (TYPE)((double)(MAX) * ((double)i / (double)(bn - 1)));\ + } while (0) + + if (data->info.gamma_depth == 8) __reset_ramps(uint8_t, INT8_MAX); + else if (data->info.gamma_depth == 16) __reset_ramps(uint16_t, INT16_MAX); + else if (data->info.gamma_depth == 32) __reset_ramps(uint32_t, INT32_MAX); + else if (data->info.gamma_depth == 64) __reset_ramps(uint64_t, INT64_MAX); + else if (data->info.gamma_depth == -1) __reset_ramps(float, 1); + else __reset_ramps(double, 1); + +#undef __reset_ramps + + return 0; +} + + + +/** + * Read information about a CRTC + * + * @param this Instance of a data structure to fill with the information about the CRTC + * @param crtc The state of the CRTC whose information should be read + * @param fields OR:ed identifiers for the information about the CRTC that should be read + * @return Zero on success, -1 on error. On error refer to the error reports in `this` + */ +int +libgamma_dummy_get_crtc_information(libgamma_crtc_information_t *restrict this, + libgamma_crtc_state_t *restrict crtc, int32_t fields) +{ + libgamma_dummy_crtc_t *restrict data = crtc->data; + int supported = libgamma_dummy_configurations.capabilities.crtc_information; + int e = 0; + size_t n; + + /* Copy over information */ + *this = data->info; + + /* Duplicate strings */ + if (this->edid) { + this->edid = malloc(this->edid_length * sizeof(char)); + if (!this->edid) + this->edid_error = errno; + memcpy(this->edid, data->info.edid, this->edid_length * sizeof(char)); + } + if (this->connector_name) { + n = strlen(this->connector_name); + this->connector_name = malloc((n + 1) * sizeof(char)); + if (!this->connector_name) + this->connector_name_error = errno; + memcpy(this->connector_name, data->info.connector_name, (n + 1) * sizeof(char)); + } + + /* Parse EDID */ + if (this->edid_error) + this->width_mm_edid_error = this->height_mm_edid_error = this->gamma_error = this->edid_error; + else if (fields & (LIBGAMMA_CRTC_INFO_MACRO_EDID ^ LIBGAMMA_CRTC_INFO_EDID)) + e |= libgamma_internal_parse_edid(this, fields); + + /* Test errors */ +#define _E(FIELD, VAR)\ + ((fields & FIELD) ? ((supported & FIELD) ? VAR : (VAR = LIBGAMMA_CRTC_INFO_NOT_SUPPORTED)) : 0) + e |= _E(LIBGAMMA_CRTC_INFO_EDID, this->edid_error); + e |= _E(LIBGAMMA_CRTC_INFO_WIDTH_MM, this->width_mm_error); + e |= _E(LIBGAMMA_CRTC_INFO_HEIGHT_MM, this->height_mm_error); + e |= _E(LIBGAMMA_CRTC_INFO_GAMMA_SIZE, this->gamma_size_error); + e |= _E(LIBGAMMA_CRTC_INFO_GAMMA_DEPTH, this->gamma_depth_error); + e |= _E(LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT, this->gamma_support_error); + e |= _E(LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER, this->subpixel_order_error); + e |= _E(LIBGAMMA_CRTC_INFO_ACTIVE, this->active_error); + e |= _E(LIBGAMMA_CRTC_INFO_CONNECTOR_NAME, this->connector_name_error); + e |= _E(LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE, this->connector_type_error); + + if ((fields & LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID) && !(supported & LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID)) + e |= this->width_mm_edid_error = LIBGAMMA_CRTC_INFO_NOT_SUPPORTED; + if ((fields & LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID) && !(supported & LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID)) + e |= this->height_mm_edid_error = LIBGAMMA_CRTC_INFO_NOT_SUPPORTED; + if ((fields & LIBGAMMA_CRTC_INFO_GAMMA) && !(supported & LIBGAMMA_CRTC_INFO_GAMMA)) + e |= this->gamma_error = LIBGAMMA_CRTC_INFO_NOT_SUPPORTED; +#undef _E + + return e ? -1 : 0; +} + + +/** + * Get the current gamma ramps for a CRTC + * + * @param 1 The data type for the ramp stop elements + * @param 2 The `ramp*` pattern for the ramp structure and function to call. + * @param 3 Either of `bit8`, `bit16`, `bit32`, `bit64`, `float_single`, `float_double`; + * rather self-explanatory + * @param 4 The number of bits in the gamma depth, -1 for single precision float, + * (`float`) and -2 for double percition float (`double`) + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +$>libgamma_dummy_crtc_get_gamma_ramps () +$>{ +int +libgamma_dummy_crtc_get_gamma_${2}(libgamma_crtc_state_t *restrict this, libgamma_gamma_${2}_t *restrict ramps) +{ + libgamma_dummy_crtc_t *data = this->data; + gamma_ramps_any_t ramps_; + ${1}* r_ramp = data->gamma_red; + ${1}* g_ramp = data->gamma_green; + ${1}* b_ramp = data->gamma_blue; + size_t rn = data->info.red_gamma_size; + size_t gn = data->info.green_gamma_size; + size_t bn = data->info.blue_gamma_size; + size_t i; + +#ifdef DEBUG + /* Check gamma ramp sizes */ + if (libgamma_dummy_configurations.capabilities.identical_gamma_sizes) + if (ramps->red_size != ramps->green_size || ramps->red_size != ramps->blue_size) + return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; + if (ramps->red_size != rn || ramps->green_size != gn || ramps->blue_size != bn) + return LIBGAMMA_WRONG_GAMMA_RAMP_SIZE; +#endif + + if (!data->info.gamma_support) + return LIBGAMMA_GAMMA_RAMP_READ_FAILED; + +#define __trans(DEPTH, SUFFIX)\ + do {\ + if (data->info.gamma_depth == DEPTH) {\ + ramps_.${3} = *ramps;\ + return libgamma_internal_translated_ramp_get(this, &ramps_, ${4}, DEPTH, libgamma_crtc_get_gamma_ramps ## SUFFIX);\ + }\ + } while (0) + +$>if [ ! ${4} = 8 ]; then + __trans(8, 8); +$>fi +$>if [ ! ${4} = 16 ]; then + __trans(16, 16); +$>fi +$>if [ ! ${4} = 32 ]; then + __trans(32, 32); +$>fi +$>if [ ! ${4} = 64 ]; then + __trans(64, 64); +$>fi +$>if [ ! ${4} = -1 ]; then + __trans(-1, f); +$>fi +$>if [ ! ${4} = -2 ]; then + __trans(-2, d); +$>fi + +#undef __trans + + for (i = 0; i < rn; i++) ramps->red[i] = r_ramp[i]; + for (i = 0; i < gn; i++) ramps->green[i] = g_ramp[i]; + for (i = 0; i < bn; i++) ramps->blue[i] = b_ramp[i]; + + return 0; +} +$>} + + +/** + * Set the gamma ramps for a CRTC + * + * @param 1 The data type for the ramp stop elements + * @param 2 The `ramp*` pattern for the ramp structure and function to call. + * @param 3 Either of `bit8`, `bit16`, `bit32`, `bit64`, `float_single`, `float_double`; + * rather self-explanatory + * @param 4 The number of bits in the gamma depth, -1 for single precision float, + * (`float`) and -2 for double percition float (`double`) + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +$>libgamma_dummy_crtc_set_gamma_ramps () +$>{ +int +libgamma_dummy_crtc_set_gamma_${2}(libgamma_crtc_state_t *restrict this, libgamma_gamma_${2}_t ramps) +{ + libgamma_dummy_crtc_t *data = this->data; + gamma_ramps_any_t ramps_; + ${1} *r_ramp = data->gamma_red; + ${1} *g_ramp = data->gamma_green; + ${1} *b_ramp = data->gamma_blue; + size_t rn = data->info.red_gamma_size; + size_t gn = data->info.green_gamma_size; + size_t bn = data->info.blue_gamma_size; + size_t i; + +#ifdef DEBUG + /* Check gamma ramp sizes */ + if (libgamma_dummy_configurations.capabilities.identical_gamma_sizes) + if (ramps.red_size != ramps.green_size || ramps.red_size != ramps.blue_size) + return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; + if (ramps.red_size != rn || ramps.green_size != gn || ramps.blue_size != bn) + return LIBGAMMA_WRONG_GAMMA_RAMP_SIZE; +#endif + + if (!data->info.gamma_support) + return LIBGAMMA_GAMMA_RAMP_READ_FAILED; + +#define __trans(DEPTH, SUFFIX)\ + do {\ + if (data->info.gamma_depth == DEPTH) {\ + ramps_.${3} = ramps;\ + return libgamma_internal_translated_ramp_set(this, ramps_, ${4}, DEPTH, libgamma_crtc_set_gamma_ramps ## SUFFIX);\ + }\ + } while (0) + +$>if [ ! ${4} = 8 ]; then + __trans(8, 8); +$>fi +$>if [ ! ${4} = 16 ]; then + __trans(16, 16); +$>fi +$>if [ ! ${4} = 32 ]; then + __trans(32, 32); +$>fi +$>if [ ! ${4} = 64 ]; then + __trans(64, 64); +$>fi +$>if [ ! ${4} = -1 ]; then + __trans(-1, f); +$>fi +$>if [ ! ${4} = -2 ]; then + __trans(-2, d); +$>fi + +#undef __trans + + for (i = 0; i < rn; i++) r_ramp[i] = ramps.red[i]; + for (i = 0; i < gn; i++) g_ramp[i] = ramps.green[i]; + for (i = 0; i < bn; i++) b_ramp[i] = ramps.blue[i]; + + return 0; +} +$>} + + + +/** + * Get the current gamma ramps for a CRTC, 8-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +$>libgamma_dummy_crtc_get_gamma_ramps uint8_t ramps8 bits8 8 + + +/** + * Set the gamma ramps for a CRTC, 8-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +$>libgamma_dummy_crtc_set_gamma_ramps uint8_t ramps8 bits8 8 + + + +/** + * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +$>libgamma_dummy_crtc_get_gamma_ramps uint16_t ramps16 bits16 16 + + +/** + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +$>libgamma_dummy_crtc_set_gamma_ramps uint16_t ramps16 bits16 16 + + + +/** + * Get the current gamma ramps for a CRTC, 32-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +$>libgamma_dummy_crtc_get_gamma_ramps uint32_t ramps32 bits32 32 + + +/** + * Set the gamma ramps for a CRTC, 32-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +$>libgamma_dummy_crtc_set_gamma_ramps uint32_t ramps32 bits32 32 + + + +/** + * Get the current gamma ramps for a CRTC, 64-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +$>libgamma_dummy_crtc_get_gamma_ramps uint64_t ramps64 bits64 64 + + +/** + * Set the gamma ramps for a CRTC, 64-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +$>libgamma_dummy_crtc_set_gamma_ramps uint64_t ramps64 bits64 64 + + + +/** + * Get the current gamma ramps for a CRTC, `float` version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +$>libgamma_dummy_crtc_get_gamma_ramps float rampsf float_single -1 + + +/** + * Set the gamma ramps for a CRTC, `float` version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +$>libgamma_dummy_crtc_set_gamma_ramps float rampsf float_single -1 + + + +/** + * Get the current gamma ramps for a CRTC, `double` version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +$>libgamma_dummy_crtc_get_gamma_ramps double rampsd float_double -2 + + +/** + * Set the gamma ramps for a CRTC, `double` version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +$>libgamma_dummy_crtc_set_gamma_ramps double rampsd float_double -2 diff --git a/gamma-dummy.h b/gamma-dummy.h new file mode 100644 index 0000000..684934e --- /dev/null +++ b/gamma-dummy.h @@ -0,0 +1,236 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef LIBGAMMA_GAMMA_DUMMY_H +#define LIBGAMMA_GAMMA_DUMMY_H + + +/** + * Return the capabilities of the adjustment method + * + * @param this The data structure to fill with the method's capabilities + */ +void libgamma_dummy_method_capabilities(libgamma_method_capabilities_t *restrict); + +/** + * Initialise an allocated site state + * + * @param this The site state to initialise + * @param site The site identifier, unless it is `NULL` it must a + * `free`:able. Once the state is destroyed the library + * will attempt to free it. There you should not free + * it yourself, and it must not be a string constant + * or allocate on the stack. Note however that it will + * not be free:d if this function fails. + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_dummy_site_initialise(libgamma_site_state_t *restrict, char *restrict); + +/** + * Release all resources held by a site state + * + * @param this The site state + */ +void libgamma_dummy_site_destroy(libgamma_site_state_t *restrict); + +/** + * Restore the gamma ramps all CRTC:s with a site to the system settings + * + * @param this The site state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_dummy_site_restore(libgamma_site_state_t *restrict); + + +/** + * Initialise an allocated partition state + * + * @param this The partition state to initialise + * @param site The site state for the site that the partition belongs to + * @param partition The the index of the partition within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_dummy_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); + +/** + * Release all resources held by a partition state + * + * @param this The partition state + */ +void libgamma_dummy_partition_destroy(libgamma_partition_state_t *restrict); + +/** + * Restore the gamma ramps all CRTC:s with a partition to the system settings + * + * @param this The partition state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_dummy_partition_restore(libgamma_partition_state_t *restrict); + + +/** + * Initialise an allocated CRTC state + * + * @param this The CRTC state to initialise + * @param partition The partition state for the partition that the CRTC belongs to + * @param crtc The the index of the CRTC within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_dummy_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); + +/** + * Release all resources held by a CRTC state + * + * @param this The CRTC state + */ +void libgamma_dummy_crtc_destroy(libgamma_crtc_state_t *restrict); + +/** + * Restore the gamma ramps for a CRTC to the system settings for that CRTC + * + * @param this The CRTC state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_dummy_crtc_restore(libgamma_crtc_state_t *restrict); + + +/** + * Read information about a CRTC + * + * @param this Instance of a data structure to fill with the information about the CRTC + * @param crtc The state of the CRTC whose information should be read + * @param fields OR:ed identifiers for the information about the CRTC that should be read + * @return Zero on success, -1 on error; on error refer to the error reports in `this` + */ +int libgamma_dummy_get_crtc_information(libgamma_crtc_information_t *restrict, libgamma_crtc_state_t *restrict, int32_t); + +/** + * Get the current gamma ramps for a CRTC, 8-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_dummy_crtc_get_gamma_ramps8(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps8_t *restrict); + +/** + * Set the gamma ramps for a CRTC, 8-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_dummy_crtc_set_gamma_ramps8(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps8_t); + + +/** + * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_dummy_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t *restrict); + +/** + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_dummy_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t); + + +/** + * Get the current gamma ramps for a CRTC, 32-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_dummy_crtc_get_gamma_ramps32(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps32_t *restrict); + +/** + * Set the gamma ramps for a CRTC, 32-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_dummy_crtc_set_gamma_ramps32(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps32_t); + + +/** + * Get the current gamma ramps for a CRTC, 64-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_dummy_crtc_get_gamma_ramps64(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps64_t *restrict); + +/** + * Set the gamma ramps for a CRTC, 64-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_dummy_crtc_set_gamma_ramps64(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps64_t); + + +/** + * Get the current gamma ramps for a CRTC, `float` version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_dummy_crtc_get_gamma_rampsf(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsf_t *restrict); + +/** + * Set the gamma ramps for a CRTC, `float` version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_dummy_crtc_set_gamma_rampsf(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsf_t); + +/** + * Get the current gamma ramps for a CRTC, `double` version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_dummy_crtc_get_gamma_rampsd(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsd_t *restrict); + +/** + * Set the gamma ramps for a CRTC, `double` version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_dummy_crtc_set_gamma_rampsd(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsd_t); + + +#endif diff --git a/gamma-linux-drm.c b/gamma-linux-drm.c new file mode 100644 index 0000000..25a5861 --- /dev/null +++ b/gamma-linux-drm.c @@ -0,0 +1,902 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef HAVE_LIBGAMMA_METHOD_LINUX_DRM +# error Compiling gamma-linux-drm.c without HAVE_LIBGAMMA_METHOD_LINUX_DRM +#endif + +#define _GNU_SOURCE + +#include "gamma-linux-drm.h" + +#include "libgamma-error.h" +#include "edid.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#ifndef O_CLOEXEC +# define O_CLOEXEC 02000000 +#endif +#ifndef NGROUPS_MAX +# define NGROUPS_MAX 65536 +#endif +#ifndef PATH_MAX +# define PATH_MAX 4096 +#endif + + + +/** + * Graphics card data for the Direct Rendering Manager adjustment method + */ +typedef struct libgamma_drm_card_data { + /** + * File descriptor for the connection to the graphics card + */ + int fd; + + /** + * The graphics card's mode resources + */ + drmModeRes *res; + + /** + * Resources for open connectors + */ + drmModeConnector **connectors; + + /** + * Resources for open encoders + */ + drmModeEncoder **encoders; + +} libgamma_drm_card_data_t; + + + +/** + * Return the capabilities of the adjustment method + * + * @param this The data structure to fill with the method's capabilities + */ +void libgamma_linux_drm_method_capabilities(libgamma_method_capabilities_t *restrict this) +{ + /* Support for all information except gamma ramp support */ + this->crtc_information = LIBGAMMA_CRTC_INFO_MACRO_EDID + | LIBGAMMA_CRTC_INFO_MACRO_VIEWPORT + | LIBGAMMA_CRTC_INFO_MACRO_RAMP + | LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER + | LIBGAMMA_CRTC_INFO_ACTIVE + | LIBGAMMA_CRTC_INFO_MACRO_CONNECTOR; + /* DRM supports multiple partitions and CRTC:s but not sites */ + this->default_site_known = 1; + this->multiple_sites = 0; + this->multiple_partitions = 1; + this->multiple_crtcs = 1; + /* Partitions are graphics cards in DRM */ + this->partitions_are_graphics_cards = 1; + /* Linux does not have system restore capabilities */ + this->site_restore = 0; + this->partition_restore = 0; + this->crtc_restore = 0; + /* Gamma ramp sizes are identical but not fixed */ + this->identical_gamma_sizes = 1; + this->fixed_gamma_size = 0; + /* Gamma ramp depths are fixed */ + this->fixed_gamma_depth = 1; + /* DRM is a real non-faked adjustment method */ + this->real = 1; + this->fake = 0; + /* Gamma ramp adjustments are persistent */ + this->auto_restore = 0; +} + + +/** + * Initialise an allocated site state + * + * @param this The site state to initialise + * @param site The site identifier, unless it is `NULL` it must a + * `free`:able. Once the state is destroyed the library + * will attempt to free it. There you should not free + * it yourself, and it must not be a string constant + * or allocate on the stack. Note however that it will + * not be free:d if this function fails. + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_linux_drm_site_initialise(libgamma_site_state_t *restrict this, char *restrict site) +{ + char pathname[PATH_MAX]; + struct stat _attr; + + if (site) + return LIBGAMMA_NO_SUCH_SITE; + + /* Count the number of available graphics cards by + `stat`:ing their existence in an API filesystem */ + this->partitions_available = 0; + for (;;) { + /* Construct pathname of graphics card device */ + snprintf(pathname, sizeof(pathname) / sizeof(char), + DRM_DEV_NAME, DRM_DIR_NAME, (int)(this->partitions_available)); + /* `stat` the graphics card's existence */ + if (stat(pathname, &_attr)) + break; + /* Move on to next graphics card */ + if (this->partitions_available++ > INT_MAX) + return LIBGAMMA_IMPOSSIBLE_AMOUNT; + } + return 0; +} + + +/** + * Release all resources held by a site state + * + * @param this The site state + */ +void +libgamma_linux_drm_site_destroy(libgamma_site_state_t *restrict this) +{ + (void) this; +} + + +/** + * Restore the gamma ramps all CRTC:s with a site to the system settings + * + * @param this The site state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_linux_drm_site_restore(libgamma_site_state_t *restrict this) +{ + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; +} + + +/** + * Figure out why `open` failed for a graphics card + * + * @param pathname The pathname of the error card + * @return The error code to report + */ +static int +figure_out_card_open_error(const char *pathname) +{ + gid_t supplemental_groups[NGROUPS_MAX]; + struct group *group; + struct stat attr; + int i, n; + + + /* Check which the device exists */ + if (errno == ENXIO || errno == ENODEV) + return LIBGAMMA_NO_SUCH_PARTITION; + + + /* If we did not get access permission, figure out why */ + + if (errno != EACCES) { + /* If we could not figure out what went + * wrong, just return the error we got */ + return LIBGAMMA_ERRNO_SET; + } + +#define __test(R, W) ((attr.st_mode & ((R) | (W))) == ((R) | (W))) + + /* Get permission requirement for the file */ + if (stat(pathname, &attr)) + return errno == EACCES ? LIBGAMMA_NO_SUCH_PARTITION : LIBGAMMA_ERRNO_SET; + + /* Test owner's, group's and others' permissions */ + if ((attr.st_uid == geteuid() && __test(S_IRUSR, S_IWUSR)) || + (attr.st_gid == getegid() && __test(S_IRGRP, S_IWGRP)) || __test(S_IROTH, S_IWOTH)) + return LIBGAMMA_DEVICE_ACCESS_FAILED; + + /* The group should be "video", but perhaps + it is "root" to restrict users */ + if (!attr.st_gid /* root group */ || __test(S_IRGRP, S_IWGRP)) + return LIBGAMMA_DEVICE_RESTRICTED; + + + /* Get the user's supplemental group membership list */ + n = getgroups(NGROUPS_MAX, supplemental_groups); + if (n < 0) + return LIBGAMMA_ERRNO_SET; + + /* Test whether any of the supplemental + group should be satisfactory */ + for (i = 0; i < n; i++) + if (supplemental_groups[i] == attr.st_gid) + break; + + /* If one of the supplemental groups should be satisfactory, + then we do not know anything more than that access failed */ + if (i != n) + return LIBGAMMA_DEVICE_ACCESS_FAILED; + + /* Otherwise, try to get the name of the group that is + required and report the missing group membership */ + { + static __thread char buf[1024]; /* My output of `sysconf(_SC_GETGR_R_SIZE_MAX)`. */ + struct group _grp; + + errno = getgrgid_r(attr.st_gid, &_grp, buf, sizeof(buf) / sizeof(char), &group); + if (errno == ERANGE) + { + /* The lenght of the group's name is absurdly long, degrade to thread-unsafe. */ + errno = 0; + group = getgrgid(attr.st_gid); + } + else if (errno) + return LIBGAMMA_ERRNO_SET; + } + + libgamma_group_gid = attr.st_gid; + libgamma_group_name = group ? group->gr_name : NULL; + return LIBGAMMA_DEVICE_REQUIRE_GROUP; +#undef __test +} + + +/** + * Initialise an allocated partition state + * + * @param this The partition state to initialise + * @param site The site state for the site that the partition belongs to + * @param partition The the index of the partition within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_linux_drm_partition_initialise(libgamma_partition_state_t *restrict this, + libgamma_site_state_t *restrict site, size_t partition) +{ + int rc = 0; + libgamma_drm_card_data_t *restrict data; + char pathname[PATH_MAX]; + + (void) site; + + /* Check for partition index overflow */ + if (partition > INT_MAX) + return LIBGAMMA_NO_SUCH_PARTITION; + + /* Allocate and initialise graphics card data */ + this->data = NULL; + data = malloc(sizeof(libgamma_drm_card_data_t)); + if (!data) + return LIBGAMMA_ERRNO_SET; + data->fd = -1; + data->res = NULL; + data->encoders = NULL; + data->connectors = NULL; + + /* Get the pathname for the graphics card */ + snprintf(pathname, sizeof(pathname) / sizeof(char), DRM_DEV_NAME, DRM_DIR_NAME, (int)partition); + + /* Acquire access to the graphics card */ + data->fd = open(pathname, O_RDWR | O_CLOEXEC); + if (data->fd < 0) { + rc = figure_out_card_open_error(pathname); + goto fail_data; + } + + /* Acquire mode resources */ + data->res = drmModeGetResources(data->fd); + if (!data->res) { + rc = LIBGAMMA_ACQUIRING_MODE_RESOURCES_FAILED; + goto fail_fd; + } + + /* Get the number of CRTC:s that are available in the partition */ + if (data->res->count_crtcs < 0) { + rc = LIBGAMMA_NEGATIVE_CRTC_COUNT; + goto fail_res; + } + this->crtcs_available = (size_t)data->res->count_crtcs; + this->data = data; + return 0; + +fail_res: + drmModeFreeResources(data->res); +fail_fd: + close(data->fd); +fail_data: + free(data); + return rc; +} + + +/** + * Release all connectors and encoders + * + * @param this The graphics card data + */ +static void +release_connectors_and_encoders(libgamma_drm_card_data_t *restrict this) +{ + size_t i, n; + + /* Release individual encoders */ + if (this->encoders) + for (i = 0, n = (size_t)(this->res->count_connectors); i < n; i++) + if (this->encoders[i]) + drmModeFreeEncoder(this->encoders[i]); + /* Release encoder array */ + free(this->encoders); + this->encoders = NULL; + + /* Release individual connectors */ + if (this->connectors) + for (i = 0, n = (size_t)(this->res->count_connectors); i < n; i++) + if (this->connectors[i]) + drmModeFreeConnector(this->connectors[i]); + /* Release connector array */ + free(this->connectors); + this->connectors = NULL; +} + + +/** + * Release all resources held by a partition state + * + * @param this The partition state + */ +void +libgamma_linux_drm_partition_destroy(libgamma_partition_state_t *restrict this) +{ + libgamma_drm_card_data_t *restrict data = this->data; + release_connectors_and_encoders(data); + if (data->res) + drmModeFreeResources(data->res); + if (data->fd >= 0) + close(data->fd); + free(data); +} + + +/** + * Restore the gamma ramps all CRTC:s with a partition to the system settings. + * + * @param this The partition state. + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library. + */ +int +libgamma_linux_drm_partition_restore(libgamma_partition_state_t *restrict this) +{ + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; +} + + + +/** + * Initialise an allocated CRTC state + * + * @param this The CRTC state to initialise + * @param partition The partition state for the partition that the CRTC belongs to + * @param crtc The the index of the CRTC within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_linux_drm_crtc_initialise(libgamma_crtc_state_t *restrict this, + libgamma_partition_state_t *restrict partition, size_t crtc) +{ + libgamma_drm_card_data_t *restrict card = partition->data; + + if (crtc >= partition->crtcs_available) + return LIBGAMMA_NO_SUCH_CRTC; + this->data = (void*)(size_t)card->res->crtcs[crtc]; + return 0; +} + + +/** + * Release all resources held by a CRTC state + * + * @param this The CRTC state + */ +void +libgamma_linux_drm_crtc_destroy(libgamma_crtc_state_t *restrict this) +{ + (void) this; +} + + +/** + * Restore the gamma ramps for a CRTC to the system settings for that CRTC. + * + * @param this The CRTC state. + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library. + */ +int +libgamma_linux_drm_crtc_restore(libgamma_crtc_state_t *restrict this) +{ + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; +} + + +/** + * Find the connector that a CRTC belongs to + * + * @param this The CRTC state + * @param error Output of the error value to store of error report + * fields for data that requires the connector +'* @return The CRTC's conncetor, `NULL` on error + */ +static drmModeConnector * +find_connector(libgamma_crtc_state_t *restrict this, int *restrict error) +{ + uint32_t crtc_id = (uint32_t)(size_t)this->data; + libgamma_drm_card_data_t *restrict card = this->partition->data; + size_t i, n = (size_t)card->res->count_connectors; + /* Open connectors and encoders if not already opened */ + if (!card->connectors) { + /* Allocate connector and encoder arrays; we use `calloc` + so all non-loaded elements are `NULL` after an error */ + card->connectors = calloc(n, sizeof(drmModeConnector *)); + if (!card->connectors) + goto fail; + card->encoders = calloc(n, sizeof(drmModeEncoder *)); + if (!card->encoders) + goto fail; + /* Fill connector and encoder arrays */ + for (i = 0; i < n; i++) { + /* Get connector */ + card->connectors[i] = drmModeGetConnector(card->fd, card->res->connectors[i]); + if (!card->connectors[i]) + goto fail; + /* Get encoder if the connector is enabled. If it is disabled it + will not have an encoder, which is indicated by the encoder + ID being 0. In such case, leave the encoder to be `NULL`. */ + if (card->connectors[i]->encoder_id) { + card->encoders[i] = drmModeGetEncoder(card->fd, card->connectors[i]->encoder_id); + if (!card->encoders[i]) + goto fail; + } + } + } + /* No error has occurred yet */ + *error = 0; + /* Find connector */ + for (i = 0; i < n; i++) + if (card->encoders[i] && card->connectors[i] && card->encoders[i]->crtc_id == crtc_id) + return card->connectors[i]; + /* We did not find the connector */ + *error = LIBGAMMA_CONNECTOR_UNKNOWN; + return NULL; + +fail: + /* Report the error that got us here, release + resouces and exit with `NULL` for failure */ + *error = errno; + release_connectors_and_encoders(card); + return NULL; +} + + +/** + * Get the size of the gamma ramps for a CRTC + * + * @param out Instance of a data structure to fill with the information about the CRTC + * @param crtc The state of the CRTC whose information should be read + * @return The value stored in `out->gamma_size_error` + */ +static int +get_gamma_ramp_size(libgamma_crtc_information_t *restrict out, const libgamma_crtc_state_t *restrict crtc) +{ + libgamma_drm_card_data_t *restrict card = crtc->partition->data; + uint32_t crtc_id = card->res->crtcs[crtc->crtc]; + drmModeCrtc *restrict crtc_info; + /* Get CRTC information */ + errno = 0; + crtc_info = drmModeGetCrtc(card->fd, crtc_id); + out->gamma_size_error = crtc_info ? 0 : errno; + /* Get gamma ramp size */ + if (!out->gamma_size_error) { + /* Store gamma ramp size */ + out->red_gamma_size = out->green_gamma_size = out->blue_gamma_size = (size_t)crtc_info->gamma_size; + /* Sanity check gamma ramp size */ + out->gamma_size_error = crtc_info->gamma_size < 2 ? LIBGAMMA_SINGLETON_GAMMA_RAMP : 0; + /* Release CRTC information */ + drmModeFreeCrtc(crtc_info); + } + return out->gamma_size_error; +} + + +/** + * Get the a monitor's subpixel order + * + * @param out Instance of a data structure to fill with the information about the CRTC + * @param connector The connector + */ +static void +get_subpixel_order(libgamma_crtc_information_t *restrict out, const drmModeConnector *restrict connector) +{ +#define __select(value)\ + case DRM_MODE_SUBPIXEL_##value:\ + out->subpixel_order = LIBGAMMA_SUBPIXEL_ORDER_##value;\ + break + + switch (connector->subpixel) { + __select (UNKNOWN); + __select (HORIZONTAL_RGB); + __select (HORIZONTAL_BGR); + __select (VERTICAL_RGB); + __select (VERTICAL_BGR); + __select (NONE); + default: + out->subpixel_order_error = LIBGAMMA_SUBPIXEL_ORDER_NOT_RECOGNISED; + break; + } + +#undef __select +} + + +/** + * Get a connector's type + * + * @param out Instance of a data structure to fill with the information about the CRTC + * @param connector The connector + * @param connector_name_base Output for the basename of the connector + */ +static void +get_connector_type(libgamma_crtc_information_t *restrict out, const drmModeConnector *restrict connector, + const char **restrict connector_name_base) +{ +#define __select(type, name)\ + case DRM_MODE_CONNECTOR_##type:\ + out->connector_type = LIBGAMMA_CONNECTOR_TYPE_##type;\ + *connector_name_base = name;\ + break + + /* These may not have been included by , + but they should be available. Becuase we define them + outself, it is best to test them last. */ +#ifndef DRM_MODE_CONNECTOR_VIRTUAL +# define DRM_MODE_CONNECTOR_VIRTUAL 15 +#endif +#ifndef DRM_MODE_CONNECTOR_DSI +# define DRM_MODE_CONNECTOR_DSI 16 +#endif + + /* Translate connector type from DRM to libgamma + and store connector basename */ + switch (connector->connector_type) { + __select (Unknown, "Unknown" ); + __select (VGA, "VGA" ); + __select (DVII, "DVI-I" ); + __select (DVID, "DVI-D" ); + __select (DVIA, "DVI-A" ); + __select (Composite, "Composite"); + __select (SVIDEO, "SVIDEO" ); + __select (LVDS, "LVDS" ); + __select (Component, "Component"); + __select (9PinDIN, "DIN" ); + __select (DisplayPort, "DP" ); + __select (HDMIA, "HDMI-A" ); + __select (HDMIB, "HDMI-B" ); + __select (TV, "TV" ); + __select (eDP, "eDP" ); + __select (VIRTUAL, "VIRTUAL" ); + __select (DSI, "DSI" ); + default: + out->connector_type_error = LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED; + out->connector_name_error = LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED; + break; + } + +#undef __select +} + + +/** + * Read information from the CRTC's conncetor + * + * @param crtc The state of the CRTC whose information should be read + * @param out Instance of a data structure to fill with the information about the CRTC + * @param connector The CRTC's connector + * @param fields OR:ed identifiers for the information about the CRTC that should be read + * @return Non-zero if at least on error occured + */ +static int +read_connector_data(libgamma_crtc_state_t *restrict crtc, libgamma_crtc_information_t *restrict out, + const drmModeConnector *restrict connector, int32_t fields) +{ + const char *connector_name_base = NULL; + libgamma_drm_card_data_t *restrict card; + uint32_t type; + size_t i, n, c; + + /* Get some information that does not require too much work */ + if (fields & (LIBGAMMA_CRTC_INFO_MACRO_ACTIVE | LIBGAMMA_CRTC_INFO_MACRO_CONNECTOR)) { + /* Get whether or not a monitor is plugged in */ + out->active = connector->connection == DRM_MODE_CONNECTED; + out->active_error = connector->connection == DRM_MODE_UNKNOWNCONNECTION ? LIBGAMMA_STATE_UNKNOWN : 0; + if (!out->active) { + if (fields & (LIBGAMMA_CRTC_INFO_MACRO_VIEWPORT | LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER)) + out->width_mm_error = out->height_mm_error = out->subpixel_order_error = LIBGAMMA_NOT_CONNECTED; + goto not_connected; + } + + /* Get viewport dimension */ + out->width_mm = connector->mmWidth; + out->height_mm = connector->mmHeight; + + /* Get subpixel order */ + get_subpixel_order(out, connector); + + not_connected: + + /* Get connector type */ + get_connector_type(out, connector, &connector_name_base); + } + + /* Get the connector's name */ + if ((fields & LIBGAMMA_CRTC_INFO_CONNECTOR_NAME) && !out->connector_name_error) { + card = crtc->partition->data; + type = connector->connector_type; + n = (size_t)card->res->count_connectors; + + /* Allocate memory for the name of the connector */ + out->connector_name = malloc((strlen(connector_name_base) + 12) * sizeof(char)); + if (!out->connector_name) + return (out->connector_name_error = errno); + + /* Get the number of connectors with the same type on the same graphics card */ + for (i = c = 0; i < n && card->connectors[i] != connector; i++) + if (card->connectors[i]->connector_type == type) + c++; + + /* Construct and store connect name that is unique to the graphics card */ + sprintf(out->connector_name, "%s-%" PRIu32, connector_name_base, (uint32_t)(c + 1)); + } + + /* Did something go wrong? */ + return out->subpixel_order_error | out->active_error | out->connector_name_error; +} + + +/** + * Get the extended display identification data for a monitor + * + * @param crtc The CRTC state + * @param out Instance of a data structure to fill with the information about the CRTC + * @param connector The CRTC's connector + * @reutnr Non-zero on error + */ +static int +get_edid(libgamma_crtc_state_t *restrict crtc, libgamma_crtc_information_t *restrict out, drmModeConnector *connector) +{ + libgamma_drm_card_data_t *restrict card = crtc->partition->data; + int prop_n = connector->count_props; + int prop_i; + drmModePropertyRes *restrict prop; + drmModePropertyBlobRes *restrict blob; + + /* Test all properies on the connector */ + for (prop_i = 0; prop_i < prop_n; prop_i++) { + /* Get output property */ + prop = drmModeGetProperty(card->fd, connector->props[prop_i]); + if (!prop) + continue; + /* Is this property the EDID? */ + if (!strcmp(prop->name, "EDID")) { + /* Get the property value */ + blob = drmModeGetPropertyBlob(card->fd, (uint32_t)connector->prop_values[prop_i]); + if (!blob) { + drmModeFreeProperty(prop); + return (out->edid_error = LIBGAMMA_PROPERTY_VALUE_QUERY_FAILED); + } + if (blob->data) { + /* Get and store the length of the EDID */ + out->edid_length = blob->length; + /* Allocate memory for a copy of the EDID that is under our memory control */ + out->edid = malloc(out->edid_length * sizeof(unsigned char)); + if (!out->edid) { + out->edid_error = errno; + } else { + /* Copy the EDID so we can free resources that got us here */ + memcpy(out->edid, blob->data, (size_t)out->edid_length * sizeof(char)); + } + /* Free the propriety value and the propery */ + drmModeFreePropertyBlob(blob); + drmModeFreeProperty(prop); + /* Were we successful? */ + return !out->edid; + } + /* Free the propriety value */ + drmModeFreePropertyBlob(blob); + } + /* Free the propriety */ + drmModeFreeProperty(prop); + } + /* If we get here, we did not find a EDID */ + return (out->edid_error = LIBGAMMA_EDID_NOT_FOUND); +} + + +/** + * Read information about a CRTC + * + * @param this Instance of a data structure to fill with the information about the CRTC + * @param crtc The state of the CRTC whose information should be read + * @param fields OR:ed identifiers for the information about the CRTC that should be read + * @return Zero on success, -1 on error; on error refer to the error reports in `this` + */ +int +libgamma_linux_drm_get_crtc_information(libgamma_crtc_information_t *restrict this, + libgamma_crtc_state_t *restrict crtc, int32_t fields) +{ +#define _E(FIELD) ((fields & FIELD) ? LIBGAMMA_CRTC_INFO_NOT_SUPPORTED : 0) + + int e = 0, require_connector, free_edid, error; + drmModeConnector *restrict connector; + + /* Wipe all error indicators */ + memset(this, 0, sizeof(libgamma_crtc_information_t)); + + /* We need to free the EDID after us if it is not explicitly requested */ + free_edid = !(fields & LIBGAMMA_CRTC_INFO_EDID); + + /* Figure out whether we require the connector to get all information we want */ + require_connector = fields & (LIBGAMMA_CRTC_INFO_MACRO_ACTIVE | LIBGAMMA_CRTC_INFO_MACRO_CONNECTOR); + + /* If we are not interested in the connector or monitor, jump */ + if (!require_connector) + goto cont; + /* Find connector. */ + connector = find_connector(crtc, &error); + if (!connector) { + /* Store reported error in affected fields */ + e |= this->width_mm_error = this->height_mm_error + = this->connector_type_error = this->subpixel_order_error + = this->active_error = this->connector_name_error + = this->edid_error = this->gamma_error + = this->width_mm_edid_error = this->height_mm_edid_error = error; + goto cont; + } + + /* Read connector data and monitor data, excluding EDID */ + e |= read_connector_data(crtc, this, connector, fields); + + /* If we do not want any EDID information, jump */ + if (!(fields & LIBGAMMA_CRTC_INFO_MACRO_EDID)) + goto cont; + /* If there is not monitor that report error in EDID related fields */ + if (this->active_error || !this->active) { + e |= this->edid_error = this->gamma_error + = this->width_mm_edid_error = this->height_mm_edid_error + = LIBGAMMA_NOT_CONNECTED; + goto cont; + } + /* Get EDID */ + e |= get_edid(crtc, this, connector); + if (!this->edid) { + this->gamma_error = this->width_mm_edid_error = this->height_mm_edid_error = this->edid_error; + goto cont; + } + /* Parse EDID */ + if (fields & (LIBGAMMA_CRTC_INFO_MACRO_EDID ^ LIBGAMMA_CRTC_INFO_EDID)) + e |= libgamma_internal_parse_edid(this, fields); + +cont: + /* Get gamma ramp size */ + e |= (fields & LIBGAMMA_CRTC_INFO_GAMMA_SIZE) ? get_gamma_ramp_size(this, crtc) : 0; + /* Store gamma ramp depth */ + this->gamma_depth = 16; + /* DRM does not support quering gamma ramp support */ + e |= this->gamma_support_error = _E(LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT); + + /* Free the EDID after us */ + if (free_edid) { + free(this->edid); + this->edid = NULL; + } + + return e ? -1 : 0; + +#undef _E +} + + +/** + * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_linux_drm_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t *restrict ramps) +{ + libgamma_drm_card_data_t *restrict card = this->partition->data; + int r; +#ifdef DEBUG + /* Gamma ramp sizes are identical but not fixed */ + if (ramps->red_size != ramps->green_size || ramps->red_size != ramps->blue_size) + return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; +#endif + /* Read current gamma ramps */ + r = drmModeCrtcGetGamma(card->fd, (uint32_t)(size_t)this->data, (uint32_t)ramps->red_size, + ramps->red, ramps->green, ramps->blue); + return r ? LIBGAMMA_GAMMA_RAMP_READ_FAILED : 0; +} + + +/** + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_linux_drm_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t ramps) +{ + libgamma_drm_card_data_t *restrict card = this->partition->data; + int r; +#ifdef DEBUG + /* Gamma ramp sizes are identical but not fixed */ + if (ramps.red_size != ramps.green_size || ramps.red_size != ramps.blue_size) + return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; +#endif + + /* Apply gamma ramps */ + r = drmModeCrtcSetGamma(card->fd, (uint32_t)(size_t)this->data, + (uint32_t)ramps.red_size, ramps.red, ramps.green, ramps.blue); + /* Check for errors */ + if (r) { + switch (errno) { + case EACCES: + case EAGAIN: + case EIO: + /* Permission denied errors must be ignored, because we do not + * have permission to do this while a display server is active. + * We are also checking for some other error codes just in case. */ + case EBUSY: + case EINPROGRESS: + /* It is hard to find documentation for DRM (in fact all of this is + * just based on the functions names and some testing,) perhaps we + * could get this if we are updating to fast. */ + break; + case EBADF: + case ENODEV: + case ENXIO: + /* TODO: 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/gamma-linux-drm.h b/gamma-linux-drm.h new file mode 100644 index 0000000..a8f1e1c --- /dev/null +++ b/gamma-linux-drm.h @@ -0,0 +1,132 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef LIBGAMMA_GAMMA_LINUX_DRM_H +#define LIBGAMMA_GAMMA_LINUX_DRM_H + + +/** + * Return the capabilities of the adjustment method + * + * @param this The data structure to fill with the method's capabilities + */ +void libgamma_linux_drm_method_capabilities(libgamma_method_capabilities_t *restrict); + +/** + * Initialise an allocated site state + * + * @param this The site state to initialise + * @param site The site identifier, unless it is `NULL` it must a + * `free`:able. Once the state is destroyed the library + * will attempt to free it. There you should not free + * it yourself, and it must not be a string constant + * or allocate on the stack. Note however that it will + * not be free:d if this function fails. + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_linux_drm_site_initialise(libgamma_site_state_t *restrict, char *restrict); + +/** + * Release all resources held by a site state + * + * @param this The site state + */ +void libgamma_linux_drm_site_destroy(libgamma_site_state_t *restrict); + +/** + * Restore the gamma ramps all CRTC:s with a site to the system settings + * + * @param this The site state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_linux_drm_site_restore(libgamma_site_state_t *restrict); + + +/** + * Initialise an allocated partition state + * + * @param this The partition state to initialise + * @param site The site state for the site that the partition belongs to + * @param partition The the index of the partition within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_linux_drm_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); + +/** + * Release all resources held by a partition state + * + * @param this The partition state + */ +void libgamma_linux_drm_partition_destroy(libgamma_partition_state_t *restrict); + +/** + * Restore the gamma ramps all CRTC:s with a partition to the system settings + * + * @param this The partition state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_linux_drm_partition_restore(libgamma_partition_state_t *restrict); + + +/** + * Initialise an allocated CRTC state + * + * @param this The CRTC state to initialise + * @param partition The partition state for the partition that the CRTC belongs to + * @param crtc The the index of the CRTC within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_linux_drm_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); + +/** + * Release all resources held by a CRTC state + * + * @param this The CRTC state + */ +void libgamma_linux_drm_crtc_destroy(libgamma_crtc_state_t *restrict); + +/** + * Restore the gamma ramps for a CRTC to the system settings for that CRTC + * + * @param this The CRTC state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_linux_drm_crtc_restore(libgamma_crtc_state_t *restrict); + + +/** + * Read information about a CRTC + * + * @param this Instance of a data structure to fill with the information about the CRTC + * @param crtc The state of the CRTC whose information should be read + * @param fields OR:ed identifiers for the information about the CRTC that should be read + * @return Zero on success, -1 on error; on error refer to the error reports in `this` + */ +int libgamma_linux_drm_get_crtc_information(libgamma_crtc_information_t *restrict, libgamma_crtc_state_t *restrict, int32_t); + +/** + * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_linux_drm_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t *restrict); + +/** + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_linux_drm_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t); + + +#endif diff --git a/gamma-quartz-cg.c b/gamma-quartz-cg.c new file mode 100644 index 0000000..b0e3a29 --- /dev/null +++ b/gamma-quartz-cg.c @@ -0,0 +1,359 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef HAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS +# error Compiling gamma-quartz-cg.c without HAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS +#endif + +#include "gamma-quartz-cg.h" + +#include "libgamma-error.h" + +#ifdef FAKE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS +# include "fake-quartz-cg.h" +#else +# include +# include +# define close_fake_quartz_cg() /* For compatibility with "fake-quartz-cg.h" */ +#endif + +#include +#include + + +/** + * Return the capabilities of the adjustment method + * + * @param this The data structure to fill with the method's capabilities + */ +void +libgamma_quartz_cg_method_capabilities(libgamma_method_capabilities_t* restrict this) +{ + /* Gamma ramps size and depth can be queried */ + this->crtc_information = LIBGAMMA_CRTC_INFO_GAMMA_SIZE | LIBGAMMA_CRTC_INFO_GAMMA_DEPTH; + /* Quartz/CoreGraphics does not support sites or partitions */ + this->default_site_known = 1; + this->multiple_sites = 0; + this->multiple_partitions = 0; + /* Quartz/CoreGraphics does support CRTC:s */ + this->multiple_crtcs = 1; + /* Partitions are not supported... */ + this->partitions_are_graphics_cards = 0; + /* CoreGraphics have support for system restore */ + this->site_restore = 1; + this->partition_restore = 1; + /* But not for individual CRTC:s */ + this->crtc_restore = 0; + /* Gamma ramp sizes are identifical but not fixed */ + this->identical_gamma_sizes = 1; + this->fixed_gamma_size = 0; + /* Gamma ramp depths are fixed */ + this->fixed_gamma_depth = 1; + /* Quartz/CoreGraphics is a real adjustment method that can be faked */ +#ifdef FAKE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS + /* It is considered real but fake if it is translated to X RandR */ + this->fake = 1; +# ifdef HAVE_LIBGAMMA_METHOD_X_RANDR + this->real = 1; +# else + this->real = 0; +# endif +#else + /* It is real and not fake if we are running on Mac OS X */ + this->fake = 0; + this->real = 1; +#endif + /* Gamma ramp adjustments are non-persistent */ + this->auto_restore = 1; +} + + +/** + * Initialise an allocated site state + * + * @param this The site state to initialise + * @param site The site identifier, unless it is `NULL` it must a + * `free`:able. Once the state is destroyed the library + * will attempt to free it. There you should not free + * it yourself, and it must not be a string constant + * or allocate on the stack. Note however that it will + * not be free:d if this function fails. + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_quartz_cg_site_initialise(libgamma_site_state_t *restrict this, char *restrict site) +{ + this->partitions_available = 1; + return site ? 0 : LIBGAMMA_NO_SUCH_SITE; +} + + +/** + * Release all resources held by a site state + * + * @param this The site state + */ +void +libgamma_quartz_cg_site_destroy(libgamma_site_state_t *restrict this) +{ + (void) this; + close_fake_quartz_cg(); +} + + +/** + * Restore the gamma ramps all CRTC:s with a site to the system settings + * + * @param this The site state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_quartz_cg_site_restore(libgamma_site_state_t *restrict this) +{ + (void) this; + CGDisplayRestoreColorSyncSettings(); + return 0; +} + + + +/** + * Initialise an allocated partition state + * + * @param this The partition state to initialise + * @param site The site state for the site that the partition belongs to + * @param partition The the index of the partition within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_quartz_cg_partition_initialise(libgamma_partition_state_t *restrict this, + libgamma_site_state_t *restrict site, size_t partition) +{ + CGDirectDisplayID *crtcs, *crtcs_old; + uint32_t cap = 4, n; + + (void) site; + + this->data = NULL; + + if (partition) + return LIBGAMMA_NO_SUCH_PARTITION; + + /* Allocate array of CRTC ID:s */ + crtcs = malloc((size_t)cap * sizeof(CGDirectDisplayID)); + if (!crtcs) + return LIBGAMMA_ERRNO_SET; + + /* It is not possible to ask CoreGraphics how many CRTC:s are + * available. We have to ask it to give us a ID:s of a number + * of CRTC:s and ask for more if we got as many as we asked for. */ + for (;;) { + /* Ask for CRTC ID:s */ + if (CGGetOnlineDisplayList(cap, crtcs, &n) != kCGErrorSuccess) + return free(crtcs), LIBGAMMA_LIST_CRTCS_FAILED; + /* If we did not get as many as we asked for then we have all */ + if (n < cap) + break; + /* Increase the number CRTC ID:s to ask for */ + if (cap > SIZE_MAX / 2) /* We could also test ~0, but it is still too many */ + return free(crtcs), LIBGAMMA_IMPOSSIBLE_AMOUNT; + cap <<= 1; + /* Grow the array of CRTC ID:s so that it can fit all we are asking for */ + crtcs = realloc(crtcs_old = crtcs, (size_t)cap * sizeof(CGDirectDisplayID)); + if (!crtcs) { + free(crtcs_old); + return LIBGAMMA_ERRNO_SET; + } + } + + /* Store CRTC ID:s and CRTC count */ + this->data = crtcs; + this->crtcs_available = (size_t)n; + return 0; +} + + +/** + * Release all resources held by a partition state + * + * @param this The partition state + */ +void +libgamma_quartz_cg_partition_destroy(libgamma_partition_state_t *restrict this) +{ + free(this->data); +} + + +/** + * Restore the gamma ramps all CRTC:s with a partition to the system settings + * + * @param this The partition state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_quartz_cg_partition_restore(libgamma_partition_state_t *restrict this) +{ + return libgamma_quartz_cg_site_restore(this->site); +} + + + +/** + * Initialise an allocated CRTC state + * + * @param this The CRTC state to initialise + * @param partition The partition state for the partition that the CRTC belongs to + * @param crtc The the index of the CRTC within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_quartz_cg_crtc_initialise(libgamma_crtc_state_t *restrict this, + libgamma_partition_state_t *restrict partition, size_t crtc) +{ + (void) this; + return crtc < partition->crtcs_available ? 0 : LIBGAMMA_NO_SUCH_CRTC; +} + + +/** + * Release all resources held by a CRTC state + * + * @param this The CRTC state + */ +void +libgamma_quartz_cg_crtc_destroy(libgamma_crtc_state_t *restrict this) +{ + (void) this; +} + + +/** + * Restore the gamma ramps for a CRTC to the system settings for that CRTC + * + * @param this The CRTC state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_quartz_cg_crtc_restore(libgamma_crtc_state_t *restrict this) +{ + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; +} + + + +/** + * Read information about a CRTC + * + * @param this Instance of a data structure to fill with the information about the CRTC + * @param crtc The state of the CRTC whose information should be read + * @param fields OR:ed identifiers for the information about the CRTC that should be read. + * @return Zero on success, -1 on error. On error refer to the error reports in `this` + */ +int +libgamma_quartz_cg_get_crtc_information(libgamma_crtc_information_t *restrict this, + libgamma_crtc_state_t *restrict crtc, int32_t fields) +{ +#define SUPPORTED_FIELDS (LIBGAMMA_CRTC_INFO_GAMMA_SIZE | LIBGAMMA_CRTC_INFO_GAMMA_DEPTH) +#define _E(FIELD) ((fields & FIELD) ? LIBGAMMA_CRTC_INFO_NOT_SUPPORTED : 0) + + CGDirectDisplayID *restrict crtcs; + size_t gamma_size; + + /* Quartz/CoreGraphics does not support EDID or monitor dimensions */ + this->edid_error = _E(LIBGAMMA_CRTC_INFO_EDID); + this->width_mm_error = _E(LIBGAMMA_CRTC_INFO_WIDTH_MM); + this->height_mm_error = _E(LIBGAMMA_CRTC_INFO_HEIGHT_MM); + this->width_mm_edid_error = _E(LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID); + this->height_mm_edid_error = _E(LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID); + /* Quartz/CoreGraphics does support gamma ramp size query + The gamma ramps are identical but not fixed, and the query can fail */ + this->gamma_size_error = 0; + if ((fields & LIBGAMMA_CRTC_INFO_GAMMA_SIZE)) { + crtcs = crtc->partition->data; + gamma_size = CGDisplayGammaTableCapacity(crtcs[crtc->crtc]); + this->red_gamma_size = this->green_gamma_size = this->blue_gamma_size = (size_t)gamma_size; + this->gamma_size_error = gamma_size < 2 ? LIBGAMMA_SINGLETON_GAMMA_RAMP : 0; + } + /* Quartz/CoreGraphics uses `float` ramps */ + this->gamma_depth = -1; + this->gamma_depth_error = 0; + /* Quartz/CoreGraphics does not support gamma ramp support queries */ + this->gamma_support_error = _E(LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT); + /* Quartz/CoreGraphics does not support EDID or connector information */ + this->subpixel_order_error = _E(LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER); + this->active_error = _E(LIBGAMMA_CRTC_INFO_ACTIVE); + this->connector_name_error = _E(LIBGAMMA_CRTC_INFO_CONNECTOR_NAME); + this->connector_type_error = _E(LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE); + this->gamma_error = _E(LIBGAMMA_CRTC_INFO_GAMMA); + + /* We failed if gamma ramp size query failed or if an unsupport field was queried */ + return (this->gamma_size_error || (fields & ~SUPPORTED_FIELDS)) ? -1 : 0; + +#undef _E +#undef SUPPORTED_FIELDS +} + + +/** + * Get the current gamma ramps for a CRTC, `float` version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_quartz_cg_crtc_get_gamma_rampsf(libgamma_crtc_state_t *restrict this, libgamma_gamma_rampsf_t *restrict ramps) +{ + CGDirectDisplayID *restrict crtcs = this->partition->data; + CGDirectDisplayID crtc_id = crtcs[this->crtc]; + uint32_t gamma_size_out; + CGError r; +#ifdef DEBUG + /* Gamma ramps sizes are identical but not fixed */ + if (ramps->red_size != ramps->green_size || ramps->red_size != ramps->blue_size) + return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; +#endif + /* Read current gamma ramps */ + r = CGGetDisplayTransferByTable(crtc_id, (uint32_t)ramps->red_size, ramps->red, ramps->green, ramps->blue, &gamma_size_out); + if (r != kCGErrorSuccess) + return LIBGAMMA_GAMMA_RAMP_READ_FAILED; + /* I hope that it will not actually ever change, + but it does return the the gamma ramp size despite + that it can be queried without querying for more */ + if (gamma_size_out != ramps->red_size) + return LIBGAMMA_GAMMA_RAMP_SIZE_CHANGED; + return 0; +} + + +/** + * Set the gamma ramps for a CRTC, `float` version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_quartz_cg_crtc_set_gamma_rampsf(libgamma_crtc_state_t *restrict this, libgamma_gamma_rampsf_t ramps) +{ + CGDirectDisplayID *restrict crtcs = this->partition->data; + CGDirectDisplayID crtc_id = crtcs[this->crtc]; + CGError r; +#ifdef DEBUG + /* Gamma ramps sizes are identical but not fixed */ + if (ramps.red_size != ramps.green_size || ramps.red_size != ramps.blue_size) + return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; +#endif + /* Apply gamma ramps */ + r = CGSetDisplayTransferByTable(crtc_id, (uint32_t)ramps.red_size, ramps.red, ramps.green, ramps.blue); + return r == kCGErrorSuccess ? 0 : LIBGAMMA_GAMMA_RAMP_WRITE_FAILED; +} diff --git a/gamma-quartz-cg.h b/gamma-quartz-cg.h new file mode 100644 index 0000000..6044680 --- /dev/null +++ b/gamma-quartz-cg.h @@ -0,0 +1,133 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef LIBGAMMA_GAMMA_QUARTZ_CG_H +#define LIBGAMMA_GAMMA_QUARTZ_CG_H + + +/** + * Return the capabilities of the adjustment method + * + * @param this The data structure to fill with the method's capabilities + */ +void libgamma_quartz_cg_method_capabilities(libgamma_method_capabilities_t *restrict); + +/** + * Initialise an allocated site state + * + * @param this The site state to initialise + * @param site The site identifier, unless it is `NULL` it must a + * `free`:able. Once the state is destroyed the library + * will attempt to free it. There you should not free + * it yourself, and it must not be a string constant + * or allocate on the stack. Note however that it will + * not be free:d if this function fails. + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_quartz_cg_site_initialise(libgamma_site_state_t *restrict, char *restrict); + +/** + * Release all resources held by a site state + * + * @param this The site state + */ +void libgamma_quartz_cg_site_destroy(libgamma_site_state_t *restrict); + +/** + * Restore the gamma ramps all CRTC:s with a site to the system settings + * + * @param this The site state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_quartz_cg_site_restore(libgamma_site_state_t *restrict); + + +/** + * Initialise an allocated partition state + * + * @param this The partition state to initialise + * @param site The site state for the site that the partition belongs to + * @param partition The the index of the partition within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_quartz_cg_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); + +/** + * Release all resources held by a partition state + * + * @param this The partition state + */ +void libgamma_quartz_cg_partition_destroy(libgamma_partition_state_t *restrict); + +/** + * Restore the gamma ramps all CRTC:s with a partition to the system settings + * + * @param this The partition state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_quartz_cg_partition_restore(libgamma_partition_state_t *restrict); + + +/** + * Initialise an allocated CRTC state + * + * @param this The CRTC state to initialise + * @param partition The partition state for the partition that the CRTC belongs to + * @param crtc The the index of the CRTC within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +__attribute__((pure)) +int libgamma_quartz_cg_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); + +/** + * Release all resources held by a CRTC state + * + * @param this The CRTC state + */ +void libgamma_quartz_cg_crtc_destroy(libgamma_crtc_state_t *restrict); + +/** + * Restore the gamma ramps for a CRTC to the system settings for that CRTC + * + * @param this The CRTC state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_quartz_cg_crtc_restore(libgamma_crtc_state_t *restrict); + + +/** + * Read information about a CRTC + * + * @param this Instance of a data structure to fill with the information about the CRTC + * @param crtc The state of the CRTC whose information should be read + * @param fields OR:ed identifiers for the information about the CRTC that should be read + * @return Zero on success, -1 on error. On error refer to the error reports in `this` + */ +int libgamma_quartz_cg_get_crtc_information(libgamma_crtc_information_t *restrict, libgamma_crtc_state_t *restrict, int32_t); + +/** + * Get the current gamma ramps for a CRTC, `float` version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_quartz_cg_crtc_get_gamma_rampsf(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsf_t *restrict); + +/** + * Set the gamma ramps for a CRTC, `float` version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_quartz_cg_crtc_set_gamma_rampsf(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsf_t); + + +#endif diff --git a/gamma-w32-gdi.c b/gamma-w32-gdi.c new file mode 100644 index 0000000..e93b53d --- /dev/null +++ b/gamma-w32-gdi.c @@ -0,0 +1,360 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef HAVE_LIBGAMMA_METHOD_W32_GDI +# error Compiling gamma-w32-gdi.c without HAVE_LIBGAMMA_METHOD_W32_GDI +#endif + +#include "gamma-w32-gdi.h" + +#include "libgamma-error.h" + +#ifndef WINVER +# define WINVER 0x0500 +#endif +#ifdef FAKE_LIBGAMMA_METHOD_W32_GDI +# include "fake-w32-gdi.h" +#else +# include +# include +#endif + +#include + + +/** + * The gamma ramp size that devices will always have in Windows GDI + * + * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd372194(v=vs.85).aspx + */ +#define GAMMA_RAMP_SIZE 256 + + +/** + * Return the capabilities of the adjustment method + * + * @param this The data structure to fill with the method's capabilities + */ +void +libgamma_w32_gdi_method_capabilities(libgamma_method_capabilities_t *restrict this) +{ + /* Gamma ramps size, depth and support can be queried */ + this->crtc_information = LIBGAMMA_CRTC_INFO_GAMMA_SIZE | LIBGAMMA_CRTC_INFO_GAMMA_DEPTH; + /* Windows GDI does not support sites or partitions */ + this->default_site_known = 1; + this->multiple_sites = 0; + this->multiple_partitions = 0; + /* Windows GDI does support CRTC:s */ + this->multiple_crtcs = 1; + /* Partitions are not supported... */ + this->partitions_are_graphics_cards = 0; + /* Windows GDI does not have system restore capabilities */ + this->site_restore = 0; + this->partition_restore = 0; + this->crtc_restore = 0; + /* Ramps sizes are fixed and identical and ramp depth is too */ + this->identical_gamma_sizes = 1; + this->fixed_gamma_size = 1; + this->fixed_gamma_depth = 1; + /* Windows GDI is a real adjustment method that can be faked */ +#ifdef FAKE_LIBGAMMA_METHOD_W32_GDI + /* It is considered real but fake if it is translated to X RandR */ + this->fake = 1; +# ifdef HAVE_LIBGAMMA_METHOD_X_RANDR + this->real = 1; +# else + this->real = 0; +# endif +#else + /* It is real and not fake if we are running on Windows */ + this->fake = 0; + this->real = 1; +#endif + /* Gamma ramp adjustments are persistent */ + this->auto_restore = 0; +} + + +/** + * Initialise an allocated site state + * + * @param this The site state to initialise + * @param site The site identifier, unless it is `NULL` it must a + * `free`:able. Once the state is destroyed the library + * will attempt to free it. There you should not free + * it yourself, and it must not be a string constant + * or allocate on the stack. Note however that it will + * not be free:d if this function fails. + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_w32_gdi_site_initialise(libgamma_site_state_t *restrict this, char* restrict site) +{ + this->partitions_available = 1; + return !site ? 0 : LIBGAMMA_NO_SUCH_SITE; +} + + +/** + * Release all resources held by a site state + * + * @param this The site state + */ +void +libgamma_w32_gdi_site_destroy(libgamma_site_state_t *restrict this) +{ + (void) this; +} + + +/** + * Restore the gamma ramps all CRTC:s with a site to the system settings + * + * @param this The site state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_w32_gdi_site_restore(libgamma_site_state_t *restrict this) +{ + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; +} + + + +/** + * Initialise an allocated partition state + * + * @param this The partition state to initialise + * @param site The site state for the site that the partition belongs to + * @param partition The the index of the partition within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_w32_gdi_partition_initialise(libgamma_partition_state_t *restrict this, + libgamma_site_state_t *restrict site, size_t partition) +{ + DWORD n = 0; + DISPLAY_DEVICE display; + + (void) site; + + if (partition) + return LIBGAMMA_NO_SUCH_PARTITION; + + /* Count CRTC:s by iteration over all possible identifiers + until we reach on that does not exist */ + display.cb = sizeof(DISPLAY_DEVICE); + while (EnumDisplayDevices(NULL, n, &display, 0)) + if (n++ == UINT32_MAX) + return LIBGAMMA_IMPOSSIBLE_AMOUNT; + this->crtcs_available = (size_t)n; + return 0; +} + + +/** + * Release all resources held by a partition state + * + * @param this The partition state + */ +void +libgamma_w32_gdi_partition_destroy(libgamma_partition_state_t *restrict this) +{ + (void) this; +} + + +/** + * Restore the gamma ramps all CRTC:s with a partition to the system settings + * + * @param this The partition state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_w32_gdi_partition_restore(libgamma_partition_state_t *restrict this) +{ + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; +} + + + +/** + * Initialise an allocated CRTC state + * + * @param this The CRTC state to initialise + * @param partition The partition state for the partition that the CRTC belongs to + * @param crtc The the index of the CRTC within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_w32_gdi_crtc_initialise(libgamma_crtc_state_t *restrict this, + libgamma_partition_state_t *restrict partition, size_t crtc) +{ + DISPLAY_DEVICE display; + HDC context; + + (void) partition; + + this->data = NULL; + + /* Windows's API mandates this... */ + display.cb = sizeof(DISPLAY_DEVICE); + /* Get identifier for selected CRTC */ + if (!EnumDisplayDevices(NULL, (DWORD)crtc, &display, 0)) + return LIBGAMMA_NO_SUCH_CRTC; + /* Check that the connector is enabled, + * newer versions of Windows will always pass. + * (According to w32's documentation, but that + * that is a load of crap) */ + if (!(display.StateFlags & DISPLAY_DEVICE_ACTIVE)) + return LIBGAMMA_CONNECTOR_DISABLED; + /* Acquire CRTC connection. */ + context = CreateDC(TEXT("DISPLAY"), display.DeviceName, NULL, NULL); + if (!context) + return LIBGAMMA_OPEN_CRTC_FAILED; + this->data = context; + return 0; +} + + +/** + * Release all resources held by a CRTC state + * + * @param this The CRTC state + */ +void +libgamma_w32_gdi_crtc_destroy(libgamma_crtc_state_t *restrict this) +{ + if (this->data) + ReleaseDC(NULL, this->data); +} + + +/** + * Restore the gamma ramps for a CRTC to the system settings for that CRTC + * + * @param this The CRTC state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_w32_gdi_crtc_restore(libgamma_crtc_state_t *restrict this) +{ + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; +} + + + +/** + * Read information about a CRTC + * + * @param this Instance of a data structure to fill with the information about the CRTC + * @param crtc The state of the CRTC whose information should be read + * @param fields OR:ed identifiers for the information about the CRTC that should be read + * @return Zero on success, -1 on error. On error refer to the error reports in `this` + */ +int +libgamma_w32_gdi_get_crtc_information(libgamma_crtc_information_t *restrict this, + libgamma_crtc_state_t *restrict crtc, int32_t fields) +{ +#define KNOWN_FIELDS (LIBGAMMA_CRTC_INFO_GAMMA_SIZE | LIBGAMMA_CRTC_INFO_GAMMA_DEPTH) +#define _E(FIELD) ((fields & FIELD) ? LIBGAMMA_CRTC_INFO_NOT_SUPPORTED : 0) + + (void) crtc; + + /* Windows GDI does not support EDID or monitor dimensions */ + this->edid_error = _E(LIBGAMMA_CRTC_INFO_EDID); + this->width_mm_error = _E(LIBGAMMA_CRTC_INFO_WIDTH_MM); + this->height_mm_error = _E(LIBGAMMA_CRTC_INFO_HEIGHT_MM); + this->width_mm_edid_error = _E(LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID); + this->height_mm_edid_error = _E(LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID); + /* Windows GDI have fixed gamma ramp sizes */ + this->red_gamma_size = GAMMA_RAMP_SIZE; + this->green_gamma_size = GAMMA_RAMP_SIZE; + this->blue_gamma_size = GAMMA_RAMP_SIZE; + this->gamma_size_error = 0; + /* Windows GDI have fixed gamma ramp depth */ + this->gamma_depth = 16; + this->gamma_depth_error = 0; + /* It is possible to query Windows GDI whether the device + have gamma ramp support. It cannot fail. However, I think + the result is incorrect if multiple monitors are active, + so we cannot include this. */ + /* + if ((fields & LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT)) + this->gamma_support = GetDeviceCaps(crtc->data, COLORMGMTCAPS) == CM_GAMMA_RAMP; + this->gamma_support_error = 0; + */ + this->gamma_support_error = _E(LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT); + /* Windows GDI does not support EDID or connector information */ + this->subpixel_order_error = _E(LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER); + this->active_error = _E(LIBGAMMA_CRTC_INFO_ACTIVE); + this->connector_name_error = _E(LIBGAMMA_CRTC_INFO_CONNECTOR_NAME); + this->connector_type_error = _E(LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE); + this->gamma_error = _E(LIBGAMMA_CRTC_INFO_GAMMA); + + /* There was a failure if and only if unsupport field was requested. */ + return (fields & ~KNOWN_FIELDS) ? -1 : 0; + +#undef _E +#undef KNOWN_FIELDS +} + + +/** + * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_w32_gdi_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t *restrict ramps) +{ +#ifdef DEBUG + /* Windows GDI have fixed gamma ramp sizes */ + if (ramps-> red_size != GAMMA_RAMP_SIZE || + ramps->green_size != GAMMA_RAMP_SIZE || + ramps-> blue_size != GAMMA_RAMP_SIZE) + return LIBGAMMA_WRONG_GAMMA_RAMP_SIZE; +#endif + /* Read current gamma ramps */ + if (!GetDeviceGammaRamp(this->data, ramps->red)) + return LIBGAMMA_GAMMA_RAMP_READ_FAILED; + return 0; +} + + +/** + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_w32_gdi_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t ramps) +{ +#ifdef DEBUG + /* Windows GDI have fixed gamma ramp sizes */ + if (ramps. red_size != GAMMA_RAMP_SIZE || + ramps.green_size != GAMMA_RAMP_SIZE || + ramps. blue_size != GAMMA_RAMP_SIZE) + return LIBGAMMA_WRONG_GAMMA_RAMP_SIZE; +#endif + /* Apply gamma ramps */ + if (!SetDeviceGammaRamp(this->data, ramps.red)) + return LIBGAMMA_GAMMA_RAMP_WRITE_FAILED; + return 0; +} diff --git a/gamma-w32-gdi.h b/gamma-w32-gdi.h new file mode 100644 index 0000000..245a94d --- /dev/null +++ b/gamma-w32-gdi.h @@ -0,0 +1,132 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef LIBGAMMA_GAMMA_W32_GDI_H +#define LIBGAMMA_GAMMA_W32_GDI_H + + +/** + * Return the capabilities of the adjustment method + * + * @param this The data structure to fill with the method's capabilities + */ +void libgamma_w32_gdi_method_capabilities(libgamma_method_capabilities_t *restrict); + +/** + * Initialise an allocated site state + * + * @param this The site state to initialise + * @param site The site identifier, unless it is `NULL` it must a + * `free`:able. Once the state is destroyed the library + * will attempt to free it. There you should not free + * it yourself, and it must not be a string constant + * or allocate on the stack. Note however that it will + * not be free:d if this function fails. + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_w32_gdi_site_initialise(libgamma_site_state_t *restrict, char *restrict); + +/** + * Release all resources held by a site state + * + * @param this The site state + */ +void libgamma_w32_gdi_site_destroy(libgamma_site_state_t *restrict); + +/** + * Restore the gamma ramps all CRTC:s with a site to the system settings + * + * @param this The site state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_w32_gdi_site_restore(libgamma_site_state_t *restrict); + + +/** + * Initialise an allocated partition state + * + * @param this The partition state to initialise + * @param site The site state for the site that the partition belongs to + * @param partition The the index of the partition within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_w32_gdi_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); + +/** + * Release all resources held by a partition state + * + * @param this The partition state + */ +void libgamma_w32_gdi_partition_destroy(libgamma_partition_state_t *restrict); + +/** + * Restore the gamma ramps all CRTC:s with a partition to the system settings + * + * @param this The partition state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_w32_gdi_partition_restore(libgamma_partition_state_t *restrict); + + +/** + * Initialise an allocated CRTC state + * + * @param this The CRTC state to initialise + * @param partition The partition state for the partition that the CRTC belongs to + * @param crtc The the index of the CRTC within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_w32_gdi_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); + +/** + * Release all resources held by a CRTC state + * + * @param this The CRTC state + */ +void libgamma_w32_gdi_crtc_destroy(libgamma_crtc_state_t *restrict); + +/** + * Restore the gamma ramps for a CRTC to the system settings for that CRTC + * + * @param this The CRTC state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_w32_gdi_crtc_restore(libgamma_crtc_state_t *restrict); + + +/** + * Read information about a CRTC + * + * @param this Instance of a data structure to fill with the information about the CRTC + * @param crtc The state of the CRTC whose information should be read + * @param fields OR:ed identifiers for the information about the CRTC that should be read + * @return Zero on success, -1 on error. On error refer to the error reports in `this` + */ +int libgamma_w32_gdi_get_crtc_information(libgamma_crtc_information_t *restrict, libgamma_crtc_state_t *restrict, int32_t); + +/** + * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_w32_gdi_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t *restrict); + +/** + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_w32_gdi_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t); + + +#endif diff --git a/gamma-x-randr.c b/gamma-x-randr.c new file mode 100644 index 0000000..95d730c --- /dev/null +++ b/gamma-x-randr.c @@ -0,0 +1,985 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef HAVE_LIBGAMMA_METHOD_X_RANDR +# error Compiling gamma-x-randr.c without HAVE_LIBGAMMA_METHOD_X_RANDR +#endif + +#include "gamma-x-randr.h" + +#include "libgamma-error.h" +#include "edid.h" + +#include +#include +#include +#include +#ifdef DEBUG +# include +#endif + +#include +#include + + +/** + * The major version of RandR the library expects + */ +#define RANDR_VERSION_MAJOR 1 + +/** + * The minor version of RandR the library expects + */ +#define RANDR_VERSION_MINOR 3 + + + +/** + * Data structure for partition data + */ +typedef struct libgamma_x_randr_partition_data { + /** + * Mapping from CRTC indices to CRTC identifiers + */ + xcb_randr_crtc_t *crtcs; + + /** + * Mapping from output indices to output identifiers + */ + xcb_randr_output_t *outputs; + + /** + * The number of outputs available + */ + size_t outputs_count; + + /** + * Mapping from CRTC indices to output indices. + * CRTC's without an output (should be impossible) + * have the value `SIZE_MAX` which is impossible + * for an existing mapping + */ + size_t *crtc_to_output; + + /** + * Screen configuration timestamp + */ + xcb_timestamp_t config_timestamp; + +} libgamma_x_randr_partition_data_t; + + + +/** + * Translate an xcb error into a libgamma error + * + * @param error_code The xcb error + * @param default_error The libgamma error to use if the xcb error is not recognised + * @return The libgamma error + */ +static int +translate_error_(int error_code, int default_error) +{ + switch (error_code) { + case XCB_CONN_ERROR: return errno = ECONNABORTED, LIBGAMMA_ERRNO_SET; + case XCB_CONN_CLOSED_EXT_NOTSUPPORTED: return errno = ENOPROTOOPT, LIBGAMMA_ERRNO_SET; + case XCB_CONN_CLOSED_MEM_INSUFFICIENT: return errno = ENOMEM, LIBGAMMA_ERRNO_SET; + case XCB_CONN_CLOSED_REQ_LEN_EXCEED: return errno = EMSGSIZE, LIBGAMMA_ERRNO_SET; + case XCB_CONN_CLOSED_PARSE_ERR: return LIBGAMMA_NO_SUCH_SITE; + case XCB_CONN_CLOSED_INVALID_SCREEN: return LIBGAMMA_NO_SUCH_PARTITION; + case XCB_CONN_CLOSED_FDPASSING_FAILED: return errno = EIO, LIBGAMMA_ERRNO_SET; + default: + return default_error; + } +} + + +/** + * Translate an xcb error into a libgamma error + * + * @param error_code The xcb error + * @param default_error The libgamma error to use if the xcb error is not recognised + * @param return_errno Whether an `errno` value may be returned + * @return The libgamma error + */ +static int +translate_error(int error_code, int default_error, int return_errno) +{ + int r = translate_error_(error_code, default_error); + return (return_errno && r > 0) ? errno : r; +} + + +/** + * Return the capabilities of the adjustment method + * + * @param this The data structure to fill with the method's capabilities + */ +void +libgamma_x_randr_method_capabilities(libgamma_method_capabilities_t *restrict this) +{ + char *display = getenv("DISPLAY"); + /* Support for all information except active status and gamma ramp support. + Active status can be queried but it is not guaranteed produces an up to date result. */ + this->crtc_information = LIBGAMMA_CRTC_INFO_MACRO_EDID + | LIBGAMMA_CRTC_INFO_MACRO_VIEWPORT + | LIBGAMMA_CRTC_INFO_MACRO_RAMP + | LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER + | LIBGAMMA_CRTC_INFO_MACRO_CONNECTOR; + /* X RandR supports multiple sites, partitions and CRTC:s */ + this->default_site_known = display && *display; + this->multiple_sites = 1; + this->multiple_partitions = 1; + this->multiple_crtcs = 1; + /* Partitions are screens and not graphics cards in X */ + this->partitions_are_graphics_cards = 0; + /* X does not have system restore capabilities */ + this->site_restore = 0; + this->partition_restore = 0; + this->crtc_restore = 0; + /* Gamma ramp sizes are identical but not fixed */ + this->identical_gamma_sizes = 1; + this->fixed_gamma_size = 0; + /* Gamma ramp depths are fixed */ + this->fixed_gamma_depth = 1; + /* X RandR is a real non-faked adjustment method */ + this->real = 1; + this->fake = 0; + /* Gamma ramp adjustments are persistent */ + this->auto_restore = 0; +} + + +/* xcb violates the rule to never return struct:s */ +#ifdef __GNUC__ +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Waggregate-return" +#endif + + +/** + * Initialise an allocated site state + * + * @param this The site state to initialise + * @param site The site identifier, unless it is `NULL` it must a + * `free`:able. Once the state is destroyed the library + * will attempt to free it. There you should not free + * it yourself, and it must not be a string constant + * or allocate on the stack. Note however that it will + * not be free:d if this function fails. + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_x_randr_site_initialise(libgamma_site_state_t *restrict this, char *restrict site) +{ + xcb_generic_error_t *error = NULL; + xcb_connection_t *restrict connection; + xcb_randr_query_version_cookie_t cookie; + xcb_randr_query_version_reply_t *restrict reply; + const xcb_setup_t *restrict setup; + xcb_screen_iterator_t iter; + + /* Connect to the display server */ + this->data = connection = xcb_connect(site, NULL); + if (!connection || xcb_connection_has_error(connection)) + return LIBGAMMA_OPEN_SITE_FAILED; + + /* Query the version of the X RandR extension protocol */ + cookie = xcb_randr_query_version(connection, RANDR_VERSION_MAJOR, RANDR_VERSION_MINOR); + reply = xcb_randr_query_version_reply(connection, cookie, &error); + + /* Check for version query failure */ + if (error || !reply) { + /* Release resources */ + free(reply); + /* If `xcb_connect` failed, both `error` and `reply` will be `NULL`. + * TODO: Can both be `NULL` for any other reason? */ + if (!error && !reply) + return LIBGAMMA_OPEN_SITE_FAILED; + xcb_disconnect(connection); + /* Translate and report error. */ + if (error != NULL) + return translate_error(error->error_code, LIBGAMMA_PROTOCOL_VERSION_QUERY_FAILED, 0); + return LIBGAMMA_PROTOCOL_VERSION_QUERY_FAILED; + } + + /* Check protocol compatibility, + we require 1.3 but 2.x may not be backwards compatible */ + if (reply->major_version != RANDR_VERSION_MAJOR || reply->minor_version < RANDR_VERSION_MINOR) { +#ifdef DEBUG + /* Print used protocol */ + fprintf(stderr, "libgamma: RandR protocol version: %u.%u", reply->major_version, reply->minor_version); +#endif + /* Release resources */ + free(reply); + xcb_disconnect(connection); + /* Report error */ + return LIBGAMMA_PROTOCOL_VERSION_NOT_SUPPORTED; + } + + /* We do not longer need to know the version of the protocol */ + free(reply); + + /* Get available screens */ + setup = xcb_get_setup(connection); + if (!setup) { + xcb_disconnect(connection); + return LIBGAMMA_LIST_PARTITIONS_FAILED; + } + iter = xcb_setup_roots_iterator(setup); + /* Get the number of available screens */ + this->partitions_available = (size_t)iter.rem; + + /* Sanity check the number of available screens. */ + return iter.rem >= ? 0 : LIBGAMMA_NEGATIVE_PARTITION_COUNT; +} + + +/** + * Release all resources held by a site state + * + * @param this The site state + */ +void +libgamma_x_randr_site_destroy(libgamma_site_state_t *restrict this) +{ + xcb_disconnect((xcb_connection_t *)this->data); +} + + +/** + * Restore the gamma ramps all CRTC:s with a site to the system settings + * + * @param this The site state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_x_randr_site_restore(libgamma_site_state_t *restrict this) +{ + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; +} + + +/** + * Duplicate a memory area + * + * @param ptr The memory area + * @param bytes The size, in bytes, of the memory area + * @return A duplication of the memory, `NULL` if zero-length or on error + */ +static inline void * +memdup(void *restrict ptr, size_t bytes) +{ + char *restrict rc; + if (!bytes) + return NULL; + rc = malloc(bytes); + if (!rc) + return NULL; + memcpy(rc, ptr, bytes); + return rc; +} + + +/** + * Initialise an allocated partition state + * + * @param this The partition state to initialise + * @param site The site state for the site that the partition belongs to. + * @param partition The the index of the partition within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_x_randr_partition_initialise(libgamma_partition_state_t *restrict this, + libgamma_site_state_t *restrict site, size_t partition) +{ + int fail_rc = LIBGAMMA_ERRNO_SET; + xcb_connection_t *restrict connection = site->data; + xcb_screen_t *restrict screen = NULL; + xcb_generic_error_t *error = NULL; + const xcb_setup_t *restrict setup; + xcb_screen_iterator_t iter; + xcb_randr_get_screen_resources_current_cookie_t cookie; + xcb_randr_get_screen_resources_current_reply_t *restrict reply; + xcb_randr_crtc_t *restrict crtcs; + xcb_randr_output_t *restrict outputs; + libgamma_x_randr_partition_data_t *restrict data; + xcb_randr_get_output_info_cookie_t out_cookie; + xcb_randr_get_output_info_reply_t *out_reply; + size_t i; + uint16_t j; + + /* Get screen list */ + setup = xcb_get_setup(connection); + if (!setup) + return LIBGAMMA_LIST_PARTITIONS_FAILED; + iter = xcb_setup_roots_iterator(setup); + + /* Get the screen */ + for (i = 0; iter.rem > 0; i++, xcb_screen_next(&iter)) + if (i == partition) { + screen = iter.data; + break; + } + /* Report failure if we did not find the screen */ + if (!iter.rem) + return LIBGAMMA_NO_SUCH_PARTITION; + + /* Check that the screen is not `NULL`. + * (Do not think this can happen, but why not.) */ + if (!screen) + return LIBGAMMA_NULL_PARTITION; + + /* Get the current resources of the screen */ + cookie = xcb_randr_get_screen_resources_current(connection, screen->root); + reply = xcb_randr_get_screen_resources_current_reply(connection, cookie, &error); + if (error) + return translate_error(error->error_code, LIBGAMMA_LIST_CRTCS_FAILED, 0); + + /* Get the number of available CRTC:s */ + this->crtcs_available = reply->num_crtcs; + /* Get the CRTC and output lists */ + crtcs = xcb_randr_get_screen_resources_current_crtcs(reply); + outputs = xcb_randr_get_screen_resources_current_outputs(reply); + if (!crtcs || !outputs) { + free(reply); + return LIBGAMMA_REPLY_VALUE_EXTRACTION_FAILED; + } + + /* Allocate adjustment method dependent data memory area. + We use `calloc` because we want `data`'s pointers to be `NULL` if not allocated at `fail`. */ + data = calloc(1, sizeof(libgamma_x_randr_partition_data_t)); + if (!data) + goto fail; + + /* Copy the CRTC:s, just so we do not have to keep the reply in memory */ + data->crtcs = memdup(crtcs, (size_t)(reply->num_crtcs) * sizeof(xcb_randr_crtc_t)); + if (!data->crtcs && reply->num_crtcs > 0) + goto fail; + + /* Copy the outputs as well */ + data->outputs = memdup(outputs, (size_t)reply->num_outputs * sizeof(xcb_randr_output_t)); + if (!data->outputs && reply->num_outputs > 0) + goto fail; + + /* Get the number of available outputs */ + data->outputs_count = (size_t)reply->num_outputs; + + /* Create mapping table from CRTC indices to output indicies. (injection) */ + data->crtc_to_output = malloc((size_t)reply->num_crtcs * sizeof(size_t)); + if (!data->crtc) + goto fail; + /* All CRTC:s should be mapped, but incase they are not, all unmapped CRTC:s should have + an invalid target, namely `SIZE_MAX`, which is 1 more than the theoretical limit */ + for (i = 0; i < (size_t)reply->num_crtcs; i++) + data->crtc_to_output[i] = SIZE_MAX; + /* Fill the table */ + for (i = 0; i < (size_t)reply->num_outputs; i++) { + /* Query output (target) information */ + out_cookie = xcb_randr_get_output_info(connection, outputs[i], reply->config_timestamp); + out_reply = xcb_randr_get_output_info_reply(connection, out_cookie, &error); + if (error) { + fail_rc = translate_error(error->error_code, LIBGAMMA_OUTPUT_INFORMATION_QUERY_FAILED, 0); + goto fail; + } + + /* Find CRTC (source) */ + for (j = 0; j < reply->num_crtcs; j++) { + if (crtcs[j] == out_reply->crtc) { + data->crtc_to_output[j] = i; + break; + } + } + + /* Release output information */ + free(out_reply); + } + + /* Store the configuration timestamp */ + data->config_timestamp = reply->config_timestamp; + /* Store the adjustment method dependent data */ + this->data = data; + /* Release resources and return successfully */ + free(reply); + return 0; + +fail: + /* Release resources and return with an error */ + if (data) { + free(data->crtcs); + free(data->outputs); + free(data->crtc_to_output); + free(data); + } + free(reply); + return fail_rc; +} + + +/** + * Release all resources held by a partition state + * + * @param this The partition state + */ +void +libgamma_x_randr_partition_destroy(libgamma_partition_state_t *restrict this) +{ + libgamma_x_randr_partition_data_t *restrict data = this->data; + free(data->crtcs); + free(data->outputs); + free(data->crtc_to_output); + free(data); +} + + +/** + * Restore the gamma ramps all CRTC:s with a partition to the system settings + * + * @param this The partition state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_x_randr_partition_restore(libgamma_partition_state_t *restrict this) +{ + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; +} + + + +/** + * Initialise an allocated CRTC state + * + * @param this The CRTC state to initialise + * @param partition The partition state for the partition that the CRTC belongs to. + * @param crtc The the index of the CRTC within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_x_randr_crtc_initialise(libgamma_crtc_state_t *restrict this, + libgamma_partition_state_t *restrict partition, size_t crtc) +{ + libgamma_x_randr_partition_data_t *restrict screen_data = partition->data; + xcb_randr_crtc_t *restrict crtc_ids = screen_data->crtcs; + this->data = crtc_ids + crtc; + return crtc < partition->crtcs_available ? 0 : LIBGAMMA_NO_SUCH_CRTC; +} + + +/** + * Release all resources held by a CRTC state + * + * @param this The CRTC state + */ +void +libgamma_x_randr_crtc_destroy(libgamma_crtc_state_t *restrict this) +{ + (void) this; +} + + +/** + * Restore the gamma ramps for a CRTC to the system settings for that CRTC + * + * @param this The CRTC state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_x_randr_crtc_restore(libgamma_crtc_state_t *restrict this) +{ + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; +} + + + +/** + * Get the gamma ramp size of a CRTC + * + * @param this Instance of a data structure to fill with the information about the CRTC + * @param crtc The state of the CRTC whose information should be read + * @return Non-zero on error + */ +static int +get_gamma_ramp_size(libgamma_crtc_information_t *restrict out, libgamma_crtc_state_t *restrict crtc) +{ + xcb_connection_t *restrict connection = crtc->partition->site->data; + xcb_randr_crtc_t *restrict crtc_id = crtc->data; + xcb_randr_get_crtc_gamma_size_cookie_t cookie; + xcb_randr_get_crtc_gamma_size_reply_t *restrict reply; + xcb_generic_error_t *error; + + /* Query gamma ramp size */ + out->gamma_size_error = 0; + cookie = xcb_randr_get_crtc_gamma_size(connection, *crtc_id); + reply = xcb_randr_get_crtc_gamma_size_reply(connection, cookie, &error); + if (error) { + out->gamma_size_error = translate_error(error->error_code, LIBGAMMA_GAMMA_RAMPS_SIZE_QUERY_FAILED, 1); + return out->gamma_size_error; + } + /* Sanity check gamma ramp size */ + if (reply->size < 2) + out->gamma_size_error = LIBGAMMA_SINGLETON_GAMMA_RAMP; + /* Store gamma ramp size */ + out->red_gamma_size = out->green_gamma_size = out->blue_gamma_size = reply->size; + /* Release resources and return successfulnes */ + free(reply); + return out->gamma_size_error; +} + + +/** + * Read information from the CRTC's output + * + * @param out Instance of a data structure to fill with the information about the CRTC + * @param output The CRTC's output information + * @return Non-zero if at least on error occured + */ +static int +read_output_data(libgamma_crtc_information_t *restrict out, xcb_randr_get_output_info_reply_t *restrict output) +{ +#define __select(value)\ + case XCB_RENDER_SUB_PIXEL_##value: out->subpixel_order = LIBGAMMA_SUBPIXEL_ORDER_##value; break + + switch (output->connection) { + case XCB_RANDR_CONNECTION_CONNECTED: + /* We have a monitor connected, report that and store information that is provided to us */ + out->active = 1; + out->width_mm = output->mm_width; + out->height_mm = output->mm_height; + switch (output->subpixel_order) { + __select (UNKNOWN); + __select (HORIZONTAL_RGB); + __select (HORIZONTAL_BGR); + __select (VERTICAL_RGB); + __select (VERTICAL_BGR); + __select (NONE); + default: + out->subpixel_order_error = LIBGAMMA_SUBPIXEL_ORDER_NOT_RECOGNISED; + break; + } + return 0; + + case XCB_RANDR_CONNECTION_UNKNOWN: + /* If we do know whether a monitor is connected report that and assume it is not */ + out->active_error = LIBGAMMA_STATE_UNKNOWN; + /* fall through */ + default: + /* If no monitor is connected, report that on fails that require it */ + out->width_mm_error = LIBGAMMA_NOT_CONNECTED; + out->height_mm_error = LIBGAMMA_NOT_CONNECTED; + out->subpixel_order_error = LIBGAMMA_NOT_CONNECTED; + /* And store that we are not connected */ + out->active = 0; + /* This fuction only failed if we could not figure out whether a monitor is connected */ + return output->connection != XCB_RANDR_CONNECTION_UNKNOWN ? 0 : -1; + } + +#undef __select +} + + +/** + * Determine the connector type from the connector name + * + * @param this The CRTC information to use and extend + * @param Non-zero on error + */ +static int +get_connector_type(libgamma_crtc_information_t *restrict this) +{ + /* Since we require the name of the output of get the type of the connected, + copy any reported error on the output's name to the connector's type, + and report failure if there was an error */ + if ((this->connector_type_error = this->connector_name_error)) + return -1; + +#define __select(name, type)\ + do {\ + if (strstr(this->connector_name, name "-") == this->connector_name) {\ + this->connector_type = LIBGAMMA_CONNECTOR_TYPE_##type\ + return 0;\ + }\ + } while (0) + + /* Check begin on the name of the output to find out what type the connector is of */ + __select ("None", Unknown); + __select ("VGA", VGA); + __select ("DVI-I", DVII); + __select ("DVI-D", DVID); + __select ("DVI-A", DVIA); + __select ("DVI", DVI); + __select ("Composite", Composite); + __select ("S-Video", SVIDEO); + __select ("Component", Component); + __select ("LFP", LFP); + __select ("Proprietary", Unknown); + __select ("HDMI", HDMI); + __select ("DisplayPort", DisplayPort); + +#undef __select + + /* If there was no matching output name pattern report that and exit with an error */ + this->connector_name_error = LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED; + return -1; +} + + +/** + * Get the output name of a CRTC + * + * @param this Instance of a data structure to fill with the information about the CRTC + * @param output The CRTC's output's information + * @return Non-zero on error + */ +static int +get_output_name(libgamma_crtc_information_t *restrict out, xcb_randr_get_output_info_reply_t *restrict output) +{ + char *restrict store; + uint8_t *restrict name; + uint16_t length; + size_t i; + + /* Get the name of the output and the length of that name */ + name = xcb_randr_get_output_info_name(output); + length = output->name_len; /* There is no NUL-termination */ + if (!name) + return out->connector_name_error = LIBGAMMA_REPLY_VALUE_EXTRACTION_FAILED; + + /* Allocate a memory area for a NUL-terminated copy of the name */ + store = out->connector_name = malloc(((size_t)length + 1) * sizeof(char)); + if (!store) { + out->connector_name_error = errno; + return -1; + } + + /* char is guaranteed to be (u)int_least8_t, but it is only guaranteed to be (u)int8_t + * on POSIX, so to be truly portable we will not assume that char is (u)int8_t */ + for (i = 0; i < (size_t)length; i++) + store[i] = (char)name[i]; + store[length] = '\0'; + + return 0; +} + + +/** + * Get the Extended Display Information Data of the monitor connected to the connector of a CRTC + * + * @param out Instance of a data structure to fill with the information about the CRTC + * @param crtc The state of the CRTC whose information should be read + * @param output The CRTC's output + * @return Non-zero on error + */ +static int +get_edid(libgamma_crtc_information_t *restrict out, libgamma_crtc_state_t *restrict crtc, xcb_randr_output_t output) +{ + xcb_connection_t *restrict connection = crtc->partition->site->data; + xcb_randr_list_output_properties_cookie_t prop_cookie; + xcb_randr_list_output_properties_reply_t *restrict prop_reply; + xcb_atom_t *atoms; + xcb_atom_t *atoms_end; + xcb_generic_error_t *error; + xcb_get_atom_name_cookie_t atom_name_cookie; + xcb_get_atom_name_reply_t *restrict atom_name_reply; + char *restrict atom_name; + int atom_name_len; + xcb_randr_get_output_property_cookie_t atom_cookie; + xcb_randr_get_output_property_reply_t *restrict atom_reply; + unsigned char* restrict atom_data; + int length; + + /* Acquire a list of all properties of the output */ + prop_cookie = xcb_randr_list_output_properties(connection, output); + prop_reply = xcb_randr_list_output_properties_reply(connection, prop_cookie, &error); + if (error) + return out->edid_error = translate_error(error->error_code, LIBGAMMA_LIST_PROPERTIES_FAILED, 1); + + /* Extract the properties form the data structure that holds them, */ + atoms = xcb_randr_list_output_properties_atoms(prop_reply); + /* and get the last one so that we can iterate over them nicely */ + atoms_end = atoms + xcb_randr_list_output_properties_atoms_length(prop_reply); + + if (!atoms) { + free(prop_reply); + return out->edid_error = LIBGAMMA_REPLY_VALUE_EXTRACTION_FAILED; + } + + /* For each property */ + for (; atoms != atoms_end; atoms++) { + /* Acquire the atom name */ + atom_name_cookie = xcb_get_atom_name(connection, *atoms); + atom_name_reply = xcb_get_atom_name_reply(connection, atom_name_cookie, &error); + if (error) + continue; + + /* Extract the atom name from the data structure that holds it */ + atom_name = xcb_get_atom_name_name(atom_name_reply); + /* As well as the length of the name; it is not NUL-termianted */ + atom_name_len = xcb_get_atom_name_name_length(atom_name_reply); + + if (/* Check for errors */ + !atom_name || /* atom_name_len < 1 || */ + /* Check that the length is the expected length for the EDID property */ + atom_name_len != 4 || + /* Check that the property is the EDID property */ + atom_name[0] != 'E' || atom_name[1] != 'D' || atom_name[2] != 'I' || atom_name[3] != 'D') { + free(atom_name_reply); + continue; + } + + /* Acquire the property's value, we know that it is either 128 or 256 byte long */ + atom_cookie = xcb_randr_get_output_property(connection, output, *atoms, XCB_GET_PROPERTY_TYPE_ANY, 0, 256, 0, 0); + atom_reply = xcb_randr_get_output_property_reply(connection, atom_cookie, &error); + /* (*) EDID version 1.0 through 1.4 define it as 128 bytes long, + * but version 2.0 define it as 256 bytes long. However, + * version 2.0 is rare(?) and has been deprecated and replaced + * by version 1.3 (I guess that is with a new version epoch, + * but I do not know.) */ + if (error) { + free(atom_name_reply); + free(prop_reply); + return out->edid_error = LIBGAMMA_PROPERTY_VALUE_QUERY_FAILED; + } + + /* Extract the property's value */ + atom_data = xcb_randr_get_output_property_data(atom_reply); + /* and its actual length */ + length = xcb_randr_get_output_property_data_length(atom_reply); + if (!atom_data || length < 1) { + free(atom_reply); + free(atom_name_reply); + free(prop_reply); + return out->edid_error = LIBGAMMA_REPLY_VALUE_EXTRACTION_FAILED; + } + + /* Store the EDID */ + out->edid_length = (size_t)length; + out->edid = malloc((size_t)length * sizeof(unsigned char)); + if (!out->edid) + out->edid_error = errno; + else + memcpy(out->edid, atom_data, (size_t)length * sizeof(unsigned char)); + + /* Release resouces */ + free(atom_reply); + free(atom_name_reply); + free(prop_reply); + + return out->edid_error; + } + + return out->edid_error = LIBGAMMA_EDID_NOT_FOUND; +} + + +/** + * Read information about a CRTC + * + * @param this Instance of a data structure to fill with the information about the CRTC + * @param crtc The state of the CRTC whose information should be read + * @param fields OR:ed identifiers for the information about the CRTC that should be read + * @return Zero on success, -1 on error; on error refer to the error reports in `this` + */ +int +libgamma_x_randr_get_crtc_information(libgamma_crtc_information_t *restrict this, + libgamma_crtc_state_t *restrict crtc, int32_t fields) +{ +#define _E(FIELD) ((fields & FIELD) ? LIBGAMMA_CRTC_INFO_NOT_SUPPORTED : 0) + + int e = 0; + xcb_randr_get_output_info_reply_t *restrict output_info = NULL; + xcb_randr_output_t output; + int free_edid, free_name; + xcb_connection_t *restrict connection; + libgamma_x_randr_partition_data_t *restrict screen_data; + size_t output_index; + xcb_randr_get_output_info_cookie_t cookie; + xcb_generic_error_t *error; + + /* Wipe all error indicators */ + memset(this, 0, sizeof(libgamma_crtc_information_t)); + + /* We need to free the EDID after us if it is not explicitly requested */ + free_edid = !(fields & LIBGAMMA_CRTC_INFO_EDID); + + /* We need to free the output's name after us if it is not explicitly requested */ + free_name = !(fields & LIBGAMMA_CRTC_INFO_CONNECTOR_NAME); + + /* Jump if the output information is not required */ + if (!(fields & (LIBGAMMA_CRTC_INFO_MACRO_ACTIVE | LIBGAMMA_CRTC_INFO_MACRO_CONNECTOR))) + goto cont; + + /* Get connector and connector information */ + connection = crtc->partition->site->data; + screen_data = crtc->partition->data; + output_index = screen_data->crtc_to_output[crtc->crtc]; + /* `SIZE_MAX` is used for CRTC:s that misses mapping to its output (should not happen), + * because `SIZE_MAX - 1` is the highest theoretical possible value */ + if (output_index == SIZE_MAX) { + e |= this->edid_error = this->gamma_error = this->width_mm_edid_error + = this->height_mm_edid_error = this->connector_type_error + = this->connector_name_error = this->subpixel_order_error + = this->width_mm_error = this->height_mm_error + = this->active_error = LIBGAMMA_CONNECTOR_UNKNOWN; + goto cont; + } + /* Get the output */ + output = screen_data->outputs[output_index]; + /* Query output information */ + cookie = xcb_randr_get_output_info(connection, output, screen_data->config_timestamp); + output_info = xcb_randr_get_output_info_reply(connection, cookie, &error); + if (error) { + e |= this->edid_error = this->gamma_error = this->width_mm_edid_error + = this->height_mm_edid_error = this->connector_type_error + = this->connector_name_error = this->subpixel_order_error + = this->width_mm_error = this->height_mm_error + = this->active_error = LIBGAMMA_OUTPUT_INFORMATION_QUERY_FAILED; + goto cont; + } + + /* Get connector name */ + e |= get_output_name(this, output_info); + /* Get connector type */ + if (fields & LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE) + e |= get_connector_type(this); + /* Get additional output data, excluding EDID */ + e |= read_output_data(this, output_info); + if (fields & LIBGAMMA_CRTC_INFO_MACRO_VIEWPORT) + e |= this->width_mm_error | this->height_mm_error; + e |= (fields & LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER) ? this->subpixel_order_error : 0; + + /* If we do not want any EDID information, jump */ + if (!(fields & LIBGAMMA_CRTC_INFO_MACRO_EDID)) + goto cont; + /* If there is not monitor that report error in EDID related fields */ + if (!this->active) { + e |= this->edid_error = this->gamma_error = this->width_mm_edid_error + = this->height_mm_edid_error = LIBGAMMA_NOT_CONNECTED; + goto cont; + } + /* Get EDID */ + e |= get_edid(this, crtc, output); + if (!this->edid) { + this->gamma_error = this->width_mm_edid_error = this->height_mm_edid_error = this->edid_error; + goto cont; + } + /* Parse EDID */ + if ((fields & (LIBGAMMA_CRTC_INFO_MACRO_EDID ^ LIBGAMMA_CRTC_INFO_EDID))) + e |= libgamma_internal_parse_edid(this, fields); + +cont: + /* Get gamma ramp size */ + e |= (fields & LIBGAMMA_CRTC_INFO_GAMMA_SIZE) ? get_gamma_ramp_size(this, crtc) : 0; + /* Store gamma ramp depth. */ + this->gamma_depth = 16; + /* X RandR does not support quering gamma ramp support. */ + e |= this->gamma_support_error = _E(LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT); + + /* Free the EDID after us */ + if (free_edid) { + free(this->edid); + this->edid = NULL; + } + /* Free the output name after us */ + if (free_name) { + free(this->connector_name); + this->connector_name = NULL; + } + + free(output_info); + return e ? -1 : 0; + +#undef _E +} + + +/** + * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_x_randr_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t *restrict ramps) +{ + xcb_connection_t *restrict connection = this->partition->site->data; + xcb_randr_get_crtc_gamma_cookie_t cookie; + xcb_randr_get_crtc_gamma_reply_t *restrict reply; + xcb_generic_error_t *error; + uint16_t *restrict red; + uint16_t *restrict green; + uint16_t *restrict blue; + +#ifdef DEBUG + /* Gamma ramp sizes are identical but not fixed */ + if (ramps->red_size != ramps->green_size || ramps->red_size != ramps->blue_size) + return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; +#endif + + /* Read current gamma ramps */ + cookie = xcb_randr_get_crtc_gamma(connection, *(xcb_randr_crtc_t *)this->data); + reply = xcb_randr_get_crtc_gamma_reply(connection, cookie, &error); + + /* Check for errors */ + if (error) + return translate_error(error->error_code, LIBGAMMA_GAMMA_RAMP_READ_FAILED, 0); + + /* Get current gamma ramps from response */ + red = xcb_randr_get_crtc_gamma_red(reply); + green = xcb_randr_get_crtc_gamma_green(reply); + blue = xcb_randr_get_crtc_gamma_blue(reply); + /* Copy over the gamma ramps to our memory */ + memcpy(ramps->red, red, ramps->red_size * sizeof(uint16_t)); + memcpy(ramps->green, green, ramps->green_size * sizeof(uint16_t)); + memcpy(ramps->blue, blue, ramps->blue_size * sizeof(uint16_t)); + + free(reply); + return 0; +} + + +/** + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_x_randr_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t ramps) +{ + xcb_connection_t *restrict connection = this->partition->site->data; + xcb_void_cookie_t cookie; + xcb_generic_error_t *restrict error; +#ifdef DEBUG + /* Gamma ramp sizes are identical but not fixed */ + if (ramps.red_size != ramps.green_size || ramps.red_size != ramps.blue_size) + return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; +#endif + + /* Apply gamma ramps */ + cookie = xcb_randr_set_crtc_gamma_checked(connection, *(xcb_randr_crtc_t*)this->data, + (uint16_t)ramps.red_size, ramps.red, ramps.green, ramps.blue); + /* Check for errors */ + error = xcb_request_check(connection, cookie); + if (error) + return translate_error(error->error_code, LIBGAMMA_GAMMA_RAMP_WRITE_FAILED, 0); + return 0; +} + + +#ifdef __GNUC__ +# pragma GCC diagnostic pop +#endif diff --git a/gamma-x-randr.h b/gamma-x-randr.h new file mode 100644 index 0000000..3591192 --- /dev/null +++ b/gamma-x-randr.h @@ -0,0 +1,132 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef LIBGAMMA_GAMMA_X_RANDR_H +#define LIBGAMMA_GAMMA_X_RANDR_H + + +/** + * Return the capabilities of the adjustment method + * + * @param this The data structure to fill with the method's capabilities + */ +void libgamma_x_randr_method_capabilities(libgamma_method_capabilities_t *restrict); + +/** + * Initialise an allocated site state + * + * @param this The site state to initialise + * @param site The site identifier, unless it is `NULL` it must a + * `free`:able. Once the state is destroyed the library + * will attempt to free it. There you should not free + * it yourself, and it must not be a string constant + * or allocate on the stack. Note however that it will + * not be free:d if this function fails + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_x_randr_site_initialise(libgamma_site_state_t *restrict, char *restrict); + +/** + * Release all resources held by a site state. + * + * @param this The site state. + */ +void libgamma_x_randr_site_destroy(libgamma_site_state_t *restrict); + +/** + * Restore the gamma ramps all CRTC:s with a site to the system settings + * + * @param this The site state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_x_randr_site_restore(libgamma_site_state_t *restrict); + + +/** + * Initialise an allocated partition state + * + * @param this The partition state to initialise + * @param site The site state for the site that the partition belongs to + * @param partition The the index of the partition within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_x_randr_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); + +/** + * Release all resources held by a partition state + * + * @param this The partition state + */ +void libgamma_x_randr_partition_destroy(libgamma_partition_state_t *restrict); + +/** + * Restore the gamma ramps all CRTC:s with a partition to the system settings + * + * @param this The partition state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_x_randr_partition_restore(libgamma_partition_state_t *restrict); + + +/** + * Initialise an allocated CRTC state + * + * @param this The CRTC state to initialise + * @param partition The partition state for the partition that the CRTC belongs to + * @param crtc The the index of the CRTC within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_x_randr_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); + +/** + * Release all resources held by a CRTC state + * + * @param this The CRTC state + */ +void libgamma_x_randr_crtc_destroy(libgamma_crtc_state_t *restrict); + +/** + * Restore the gamma ramps for a CRTC to the system settings for that CRTC + * + * @param this The CRTC state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_x_randr_crtc_restore(libgamma_crtc_state_t *restrict); + + +/** + * Read information about a CRTC + * + * @param this Instance of a data structure to fill with the information about the CRTC + * @param crtc The state of the CRTC whose information should be read + * @param fields OR:ed identifiers for the information about the CRTC that should be read + * @return Zero on success, -1 on error; on error refer to the error reports in `this` + */ +int libgamma_x_randr_get_crtc_information(libgamma_crtc_information_t *restrict, libgamma_crtc_state_t *restrict, int32_t); + +/** + * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_x_randr_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t *restrict); + +/** + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_x_randr_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t); + + +#endif diff --git a/gamma-x-vidmode.c b/gamma-x-vidmode.c new file mode 100644 index 0000000..bae9d31 --- /dev/null +++ b/gamma-x-vidmode.c @@ -0,0 +1,312 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef HAVE_LIBGAMMA_METHOD_X_VIDMODE +# error Compiling gamma-x-vidmode.c without HAVE_LIBGAMMA_METHOD_X_VIDMODE +#endif + +#include "gamma-x-vidmode.h" + +#include "libgamma-error.h" + +#include +#include + +#include +#include + + +/** + * Return the capabilities of the adjustment method + * + * @param this The data structure to fill with the method's capabilities + */ +void +libgamma_x_vidmode_method_capabilities(libgamma_method_capabilities_t *restrict this) +{ + char *restrict display = getenv("DISPLAY"); + /* Gamma ramps size and depth can be queried */ + this->crtc_information = LIBGAMMA_CRTC_INFO_GAMMA_SIZE | LIBGAMMA_CRTC_INFO_GAMMA_DEPTH; + /* X VidMode supports multiple sites and partitions but not CRTC:s */ + this->default_site_known = (display && *display) ? 1 : 0; + this->multiple_sites = 1; + this->multiple_partitions = 1; + this->multiple_crtcs = 0; + /* Partitions are screens and not graphics cards in X */ + this->partitions_are_graphics_cards = 0; + /* X does not have system restore capabilities */ + this->site_restore = 0; + this->partition_restore = 0; + this->crtc_restore = 0; + /* Gamma ramp sizes are identical but not fixed */ + this->identical_gamma_sizes = 1; + this->fixed_gamma_size = 0; + /* Gamma ramp depths are fixed */ + this->fixed_gamma_depth = 1; + /* X VidMode is a real non-faked adjustment method */ + this->real = 1; + this->fake = 0; + /* Gamma ramp adjustments are persistent */ + this->auto_restore = 0; +} + + +/** + * Initialise an allocated site state + * + * @param this The site state to initialise + * @param site The site identifier, unless it is `NULL` it must a + * `free`:able. Once the state is destroyed the library + * will attempt to free it. There you should not free + * it yourself, and it must not be a string constant + * or allocate on the stack. Note however that it will + * not be free:d if this function fails. + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_x_vidmode_site_initialise(libgamma_site_state_t *restrict this, char *restrict site) +{ + /* Connect to the display */ + Display *restrict connection; + int _major, _minor, screens; + this->data = connection = XOpenDisplay(site); + if (!this->data) + return LIBGAMMA_OPEN_SITE_FAILED; + /* Query X VidMode extension protocol version */ + if (!XF86VidModeQueryVersion(connection, &_major, &_minor)) { + XCloseDisplay(connection); + return LIBGAMMA_PROTOCOL_VERSION_QUERY_FAILED; + } + /* Query the number of available screens */ + screens = ScreenCount(connection); + if (screens < 0) { + XCloseDisplay(connection); + return LIBGAMMA_NEGATIVE_PARTITION_COUNT; + } + this->partitions_available = (size_t)screens; + return 0; +} + + +/** + * Release all resources held by a site state + * + * @param this The site state + */ +void +libgamma_x_vidmode_site_destroy(libgamma_site_state_t *restrict this) +{ + XCloseDisplay((Display *)this->data); +} + + +/** + * Restore the gamma ramps all CRTC:s with a site to the system settings + * + * @param this The site state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_x_vidmode_site_restore(libgamma_site_state_t *restrict this) +{ + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; +} + + + +/** + * Initialise an allocated partition state + * + * @param this The partition state to initialise + * @param site The site state for the site that the partition belongs to + * @param partition The the index of the partition within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_x_vidmode_partition_initialise(libgamma_partition_state_t *restrict this, + libgamma_site_state_t *restrict site, size_t partition) +{ + this->crtcs_available = 1; + return partition < site->partitions_available ? 0 : LIBGAMMA_NO_SUCH_PARTITION; +} + + +/** + * Release all resources held by a partition state + * + * @param this The partition state + */ +void +libgamma_x_vidmode_partition_destroy(libgamma_partition_state_t *restrict this) +{ + (void) this; +} + + +/** + * Restore the gamma ramps all CRTC:s with a partition to the system settings + * + * @param this The partition state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_x_vidmode_partition_restore(libgamma_partition_state_t *restrict this) +{ + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; +} + + + +/** + * Initialise an allocated CRTC state + * + * @param this The CRTC state to initialise + * @param partition The partition state for the partition that the CRTC belongs to + * @param crtc The the index of the CRTC within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_x_vidmode_crtc_initialise(libgamma_crtc_state_t *restrict this, + libgamma_partition_state_t *restrict partition, size_t crtc) +{ + (void) this; + (void) partition; + return !crtc ? 0 : LIBGAMMA_NO_SUCH_CRTC; +} + + +/** + * Release all resources held by a CRTC state + * + * @param this The CRTC state + */ +void +libgamma_x_vidmode_crtc_destroy(libgamma_crtc_state_t *restrict this) +{ + (void) this; +} + + +/** + * Restore the gamma ramps for a CRTC to the system settings for that CRTC + * + * @param this The CRTC state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_x_vidmode_crtc_restore(libgamma_crtc_state_t *restrict this) +{ + (void) this; + errno = ENOTSUP; + return LIBGAMMA_ERRNO_SET; +} + + + +/** + * Read information about a CRTC + * + * @param this Instance of a data structure to fill with the information about the CRTC + * @param crtc The state of the CRTC whose information should be read + * @param fields OR:ed identifiers for the information about the CRTC that should be read + * @return Zero on success, -1 on error; oOn error refer to the error reports in `this` + */ +int +libgamma_x_vidmode_get_crtc_information(libgamma_crtc_information_t *restrict this, + libgamma_crtc_state_t *restrict crtc, int32_t fields) +{ +#define _E(FIELD) ((fields & FIELD) ? LIBGAMMA_CRTC_INFO_NOT_SUPPORTED : 0) + + Display *restrict connection; + int stops = 0; + + /* X VidMode does not support EDID or monitor dimensions */ + this->edid_error = _E(LIBGAMMA_CRTC_INFO_EDID); + this->width_mm_error = _E(LIBGAMMA_CRTC_INFO_WIDTH_MM); + this->height_mm_error = _E(LIBGAMMA_CRTC_INFO_HEIGHT_MM); + this->width_mm_edid_error = _E(LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID); + this->height_mm_edid_error = _E(LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID); + this->gamma_size_error = 0; + /* X VidMode does support gamma ramp size query. The gamma + ramps are identical but not fixed, and the query can fail. */ + if ((fields & LIBGAMMA_CRTC_INFO_GAMMA_SIZE)) { + connection = crtc->partition->site->data; + if (!XF86VidModeGetGammaRampSize(connection, (int)crtc->partition->partition, &stops)) + this->gamma_size_error = LIBGAMMA_GAMMA_RAMPS_SIZE_QUERY_FAILED; + else if (stops < 2) + this->gamma_size_error = LIBGAMMA_SINGLETON_GAMMA_RAMP; + this->red_gamma_size = this->green_gamma_size = this->blue_gamma_size = (size_t)stops; + } + /* X VidMode uses 16-bit integer ramps */ + this->gamma_depth = 16; + this->gamma_depth_error = 0; + /* X VidMode does not support gamma ramp support queries */ + this->gamma_support_error = _E(LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT); + /* X VidMode does not support EDID or connector information */ + this->subpixel_order_error = _E(LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER); + this->active_error = _E(LIBGAMMA_CRTC_INFO_ACTIVE); + this->connector_name_error = _E(LIBGAMMA_CRTC_INFO_CONNECTOR_NAME); + this->connector_type_error = _E(LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE); + this->gamma_error = _E(LIBGAMMA_CRTC_INFO_GAMMA); + + /* We failed if gamma ramp size query failed or if an unsupport field was queried. */ + return (this->gamma_size_error || (fields & ~(LIBGAMMA_CRTC_INFO_GAMMA_DEPTH | LIBGAMMA_CRTC_INFO_GAMMA_SIZE))) ? -1 : 0; + +#undef _E +} + + +/** + * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_x_vidmode_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t *restrict ramps) +{ +#ifdef DEBUG + /* Gamma ramp sizes are identical but not fixed */ + if (ramps->red_size != ramps->green_size || ramps->red_size != ramps->blue_size) + return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; +#endif + /* Read current gamma ramps */ + if (!XF86VidModeGetGammaRamp((Display *)this->partition->site->data, (int)this->partition->partition, + (int)ramps->red_size, ramps->red, ramps->green, ramps->blue)) + return LIBGAMMA_GAMMA_RAMP_READ_FAILED; + return 0; +} + + +/** + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_x_vidmode_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t ramps) +{ +#ifdef DEBUG + /* Gamma ramp sizes are identical but not fixed */ + if (ramps.red_size != ramps.green_size || ramps.red_size != ramps.blue_size) + return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; +#endif + /* Apply gamma ramps */ + if (!XF86VidModeSetGammaRamp((Display *)this->partition->site->data, (int)this->partition->partition, + (int)ramps.red_size, ramps.red, ramps.green, ramps.blue)) + return LIBGAMMA_GAMMA_RAMP_WRITE_FAILED; + return 0; +} diff --git a/gamma-x-vidmode.h b/gamma-x-vidmode.h new file mode 100644 index 0000000..8a95442 --- /dev/null +++ b/gamma-x-vidmode.h @@ -0,0 +1,133 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef LIBGAMMA_GAMMA_X_VIDMODE_H +#define LIBGAMMA_GAMMA_X_VIDMODE_H + + +/** + * Return the capabilities of the adjustment method + * + * @param this The data structure to fill with the method's capabilities + */ +void libgamma_x_vidmode_method_capabilities(libgamma_method_capabilities_t *restrict); + +/** + * Initialise an allocated site state + * + * @param this The site state to initialise + * @param site The site identifier, unless it is `NULL` it must a + * `free`:able. Once the state is destroyed the library + * will attempt to free it. There you should not free + * it yourself, and it must not be a string constant + * or allocate on the stack. Note however that it will + * not be free:d if this function fails. + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_x_vidmode_site_initialise(libgamma_site_state_t *restrict, char *restrict); + +/** + * Release all resources held by a site state + * + * @param this The site state + */ +void libgamma_x_vidmode_site_destroy(libgamma_site_state_t *restrict); + +/** + * Restore the gamma ramps all CRTC:s with a site to the system settings + * + * @param this The site state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_x_vidmode_site_restore(libgamma_site_state_t *restrict); + + +/** + * Initialise an allocated partition state + * + * @param this The partition state to initialise + * @param site The site state for the site that the partition belongs to + * @param partition The the index of the partition within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_x_vidmode_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); + +/** + * Release all resources held by a partition state + * + * @param this The partition state + */ +void libgamma_x_vidmode_partition_destroy(libgamma_partition_state_t *restrict); + +/** + * Restore the gamma ramps all CRTC:s with a partition to the system settings + * + * @param this The partition state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_x_vidmode_partition_restore(libgamma_partition_state_t *restrict); + + +/** + * Initialise an allocated CRTC state + * + * @param this The CRTC state to initialise + * @param partition The partition state for the partition that the CRTC belongs to + * @param crtc The the index of the CRTC within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +__attribute__((const)) +int libgamma_x_vidmode_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); + +/** + * Release all resources held by a CRTC state + * + * @param this The CRTC state + */ +void libgamma_x_vidmode_crtc_destroy(libgamma_crtc_state_t *restrict); + +/** + * Restore the gamma ramps for a CRTC to the system settings for that CRTC + * + * @param this The CRTC state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_x_vidmode_crtc_restore(libgamma_crtc_state_t *restrict); + + +/** + * Read information about a CRTC + * + * @param this Instance of a data structure to fill with the information about the CRTC + * @param crtc The state of the CRTC whose information should be read + * @param fields OR:ed identifiers for the information about the CRTC that should be read + * @return Zero on success, -1 on error; on error refer to the error reports in `this` + */ +int libgamma_x_vidmode_get_crtc_information(libgamma_crtc_information_t *restrict, libgamma_crtc_state_t *restrict, int32_t); + +/** + * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_x_vidmode_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t *restrict); + +/** + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_x_vidmode_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t); + + +#endif diff --git a/get_ramps.h b/get_ramps.h new file mode 100644 index 0000000..8d4c011 --- /dev/null +++ b/get_ramps.h @@ -0,0 +1,30 @@ +/* See LICENSE file for copyright and license details. */ + +/* + * This file is intended to be included from + * libgamma_crtc_get_gamma_ramps{8,16,32,64,f,d} + */ + +#define CONCAT(A, B) A##B + + +gamma_ramps_any_t ramps_; +switch (this->partition->site->method) { +#define X(CONST, CNAME, MDEPTH, MRAMPS)\ +case CONST:\ + if (!(MDEPTH)) {\ + return APPEND_RAMPS(libgamma_dummy_crtc_get_gamma_)(this, ramps);\ + } else if ((DEPTH) == (MDEPTH)) {\ + return libgamma_##CNAME##_crtc_get_gamma_##MRAMPS(this, (void *)ramps);\ + } else {\ + ramps_.TYPE = *ramps;\ + return libgamma_internal_translated_ramp_get(this, &ramps_, DEPTH, MDEPTH, libgamma_crtc_get_gamma_##MRAMPS);\ + } +LIST_AVAILABLE_METHODS(X) +#undef X +default: + return LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD; +} + + +#undef CONCAT diff --git a/libgamma.h b/libgamma.h new file mode 100644 index 0000000..d798f33 --- /dev/null +++ b/libgamma.h @@ -0,0 +1,2474 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef LIBGAMMA_H +#define LIBGAMMA_H + +#include +#include +#include +#include + + +#ifndef __GNUC__ +# define LIBGAMMA_GCC_ONLY__(X) X +#else +# define LIBGAMMA_GCC_ONLY__(X) +#endif + + + +/** + * `errno` has be set with a standard error number + * to indicate the what has gone wrong + */ +#define LIBGAMMA_ERRNO_SET (-1) + +/** + * The selected adjustment method does not exist + * or has been excluded at compile-time + */ +#define LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD (-2) /* TODO use separate error code if disable at compile-time */ + +/** + * The selected site does not exist + */ +#define LIBGAMMA_NO_SUCH_SITE (-3) + +/** + * The selected partition does not exist + */ +#define LIBGAMMA_NO_SUCH_PARTITION (-4) + +/** + * The selected CRTC does not exist + */ +#define LIBGAMMA_NO_SUCH_CRTC (-5) + +/** + * Counter overflowed when counting the number + * of available items + */ +#define LIBGAMMA_IMPOSSIBLE_AMOUNT (-6) + +/** + * The selected connector is disabled, it does + * not have a CRTC + */ +#define LIBGAMMA_CONNECTOR_DISABLED (-7) + +/** + * The selected CRTC could not be opened, + * reason unknown + */ +#define LIBGAMMA_OPEN_CRTC_FAILED (-8) + +/** + * The CRTC information field is not supported + * by the adjustment method + */ +#define LIBGAMMA_CRTC_INFO_NOT_SUPPORTED (-9) + +/** + * Failed to read the current gamma ramps for + * the selected CRTC, reason unknown + */ +#define LIBGAMMA_GAMMA_RAMP_READ_FAILED (-10) + +/** + * Failed to write the current gamma ramps for + * the selected CRTC, reason unknown + */ +#define LIBGAMMA_GAMMA_RAMP_WRITE_FAILED (-11) + +/** + * The specified ramp sizes does not match the + * ramps sizes returned by the adjustment methods + * in response to the query/command + */ +#define LIBGAMMA_GAMMA_RAMP_SIZE_CHANGED (-12) + +/** + * The specified ramp sizes are not identical + * which is required by the adjustment method + * + * (Only returned in debug mode) + */ +#define LIBGAMMA_MIXED_GAMMA_RAMP_SIZE (-13) + +/** + * The specified ramp sizes are not supported + * by the adjustment method + * + * (Only returned in debug mode) + */ +#define LIBGAMMA_WRONG_GAMMA_RAMP_SIZE (-14) + +/** + * The adjustment method reported that the gamma + * ramps size is 1, or perhaps even zero or negative + */ +#define LIBGAMMA_SINGLETON_GAMMA_RAMP (-15) + +/** + * The adjustment method failed to list + * available CRTC:s, reason unknown + */ +#define LIBGAMMA_LIST_CRTCS_FAILED (-16) + +/** + * Failed to acquire mode resources from the + * adjustment method + */ +#define LIBGAMMA_ACQUIRING_MODE_RESOURCES_FAILED (-17) + +/** + * The adjustment method reported that a negative + * number of partitions exists in the site + */ +#define LIBGAMMA_NEGATIVE_PARTITION_COUNT (-18) + +/** + * The adjustment method reported that a negative + * number of CRTC:s exists in the partition + */ +#define LIBGAMMA_NEGATIVE_CRTC_COUNT (-19) + +/** + * Device cannot be access becauses of + * insufficient permissions + */ +#define LIBGAMMA_DEVICE_RESTRICTED (-20) + +/** + * Device cannot be access, reason unknown + */ +#define LIBGAMMA_DEVICE_ACCESS_FAILED (-21) + +/** + * Device cannot be access, membership of the + * `libgamma_group_gid` (named by `libgamma_group_name` + * (can be `NULL`, if so `errno` may have been set + * to tell why)) is required + */ +#define LIBGAMMA_DEVICE_REQUIRE_GROUP (-22) + +/** + * The graphics card appear to have been removed + */ +#define LIBGAMMA_GRAPHICS_CARD_REMOVED (-23) + +/** + * The state of the requested information is unknown + */ +#define LIBGAMMA_STATE_UNKNOWN (-24) + +/** + * Failed to determine which connector the + * CRTC belongs to + */ +#define LIBGAMMA_CONNECTOR_UNKNOWN (-25) + +/** + * The detected connector type is not listed + * in this library and has to be updated + */ +#define LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED (-26) + +/** + * The detected subpixel order is not listed + * in this library and has to be updated + */ +#define LIBGAMMA_SUBPIXEL_ORDER_NOT_RECOGNISED (-27) + +/** + * The length of the EDID does not match that + * of any supported EDID structure revision + */ +#define LIBGAMMA_EDID_LENGTH_UNSUPPORTED (-28) + +/** + * The magic number in the EDID does not match + * that of any supported EDID structure revision + */ +#define LIBGAMMA_EDID_WRONG_MAGIC_NUMBER (-29) + +/** + * The EDID structure revision used by the + * monitor is not supported + */ +#define LIBGAMMA_EDID_REVISION_UNSUPPORTED (-30) + +/** + * The gamma characteristics field in the EDID + * is left unspecified + * + * (This could be considered a non-error) + */ +#define LIBGAMMA_GAMMA_NOT_SPECIFIED (-31) + +/** + * The checksum in the EDID is incorrect, all + * request information has been provided + * by you cannot count on it + */ +#define LIBGAMMA_EDID_CHECKSUM_ERROR (-32) + +/** + * Both of the errors `LIBGAMMA_GAMMA_NOT_SPECIFIED` + * and `LIBGAMMA_EDID_CHECKSUM_ERROR` have occurred + */ +#define LIBGAMMA_GAMMA_NOT_SPECIFIED_AND_EDID_CHECKSUM_ERROR (-33) + +/** + * Failed to query the gamma ramps size from the + * adjustment method, reason unknown + */ +#define LIBGAMMA_GAMMA_RAMPS_SIZE_QUERY_FAILED (-34) + +/** + * The selected partition could not be opened, + * reason unknown + */ +#define LIBGAMMA_OPEN_PARTITION_FAILED (-35) + +/** + * The selected site could not be opened, + * reason unknown + */ +#define LIBGAMMA_OPEN_SITE_FAILED (-36) + +/** + * Failed to query the adjustment method for + * its protocol version, reason unknown + */ +#define LIBGAMMA_PROTOCOL_VERSION_QUERY_FAILED (-37) + +/** + * The adjustment method's version of its + * protocol is not supported + */ +#define LIBGAMMA_PROTOCOL_VERSION_NOT_SUPPORTED (-38) + +/** + * The adjustment method failed to list + * available partitions, reason unknown + */ +#define LIBGAMMA_LIST_PARTITIONS_FAILED (-39) + +/** + * Partition exists by index, but the partition + * at that index does not exist + */ +#define LIBGAMMA_NULL_PARTITION (-40) + +/** + * There is not monitor connected to the + * connector of the selected CRTC + */ +#define LIBGAMMA_NOT_CONNECTED (-41) + +/** + * Data extraction from a reply from the + * adjustment method failed, reason unknown + */ +#define LIBGAMMA_REPLY_VALUE_EXTRACTION_FAILED (-42) + +/** + * No EDID property was found on the output + */ +#define LIBGAMMA_EDID_NOT_FOUND (-43) + +/** + * Failed to list properties on the output, + * reason unknown + */ +#define LIBGAMMA_LIST_PROPERTIES_FAILED (-44) + +/** + * Failed to query a property's value from + * the output, reason unknown + */ +#define LIBGAMMA_PROPERTY_VALUE_QUERY_FAILED (-45) + +/** + * A request for information on an output + * failed, reason unknown + */ +#define LIBGAMMA_OUTPUT_INFORMATION_QUERY_FAILED (-46) + +/* DEVELOPERS: Remember to update LIBGAMMA_ERROR_MIN below and LIST_ERRORS in common.h when adding errors */ + +/** + * The number of the libgamma error with the + * lowest number in the version of the library + * that the program is compiled against + */ +#define LIBGAMMA_ERROR_MIN (-46) + +/** + * The number of the libgamma error with the + * lowest number in the version of the library + * that the program is linked against + */ +extern const int libgamma_error_min; + + + +/** + * The identifier for the dummy adjustment method + * + * This method can be configured and is useful for + * testing your program's ability to handle errors + */ +#define LIBGAMMA_METHOD_DUMMY 0 + +/** + * The identifier for the adjustment method with + * uses the RandR protocol under the X display server + */ +#define LIBGAMMA_METHOD_X_RANDR 1 + +/** + * The identifier for the adjustment method with + * uses the VidMode protocol under the X display server + * + * This is an older alternative to RandR that can + * work on some drivers that are not supported by RandR, + * however it can only control the primary CRTC per + * screen (partition) + */ +#define LIBGAMMA_METHOD_X_VIDMODE 2 + +/** + * The identifier for the Direct Rendering Manager + * adjustment method that is available in Linux + * (built in to the Linux kernel with a userland + * library for access) and is a part of the + * Direct Rendering Infrastructure. + * + * This adjustment method will work when you are + * in non-graphical mode; however a display server + * cannot be started while this is running, but it + * can be started while a display server is running + */ +#define LIBGAMMA_METHOD_LINUX_DRM 3 + +/** + * The identifier for the Graphics Device Interface + * adjustment method that is available in Windows + * + * This method is not well tested; it can be compiled + * to be available under X.org using a translation layer + */ +#define LIBGAMMA_METHOD_W32_GDI 4 + +/** + * The identifier for the CoreGraphics adjustment + * method that is available in Mac OS X that can + * adjust gamma ramps under the Quartz display server + * + * This method is not well tested; it can be compiled + * to be available under X.org using a translation layer + */ +#define LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS 5 + +/* DEVELOPERS: Remember to update LIBGAMMA_METHOD_COUNT below and LIST_METHODS in common.h when adding methods */ + +/** + * The number adjustment methods provided by the + * version this library the program is compiled + * against + * + * This number includes both compile-time enabled + * and compile-time disabled adjustment methods + */ +#define LIBGAMMA_METHOD_COUNT 6 + +/** + * The number adjustment methods provided by the + * version this library the program is linked + * against + * + * This number includes both compile-time enabled + * and compile-time disabled adjustment methods + */ +extern const int libgamma_method_count; + + + +/** + * Types for connectors + */ +typedef enum libgamma_connector_type { + /** + * The adjustment method does not know the connector's type + * + * (This could be considered an error) + */ + LIBGAMMA_CONNECTOR_TYPE_Unknown = 0, + + /** + * Video Graphics Array (VGA) + */ + LIBGAMMA_CONNECTOR_TYPE_VGA, + + /** + * Digital Visual Interface, unknown type + */ + LIBGAMMA_CONNECTOR_TYPE_DVI, + + /** + * Digital Visual Interface, integrated (DVI-I) + */ + LIBGAMMA_CONNECTOR_TYPE_DVII, + + /** + * Digital Visual Interface, digital only (DVI-D) + */ + LIBGAMMA_CONNECTOR_TYPE_DVID, + + /** + * Digital Visual Interface, analogue only (DVI-A) + */ + LIBGAMMA_CONNECTOR_TYPE_DVIA, + + /** + * Composite video + */ + LIBGAMMA_CONNECTOR_TYPE_Composite, + + /** + * Separate Video (S-video) + */ + LIBGAMMA_CONNECTOR_TYPE_SVIDEO, + + /** + * Low-voltage differential signaling (LVDS) + */ + LIBGAMMA_CONNECTOR_TYPE_LVDS, + + /** + * Component video, usually separate cables for each channel + */ + LIBGAMMA_CONNECTOR_TYPE_Component, + + /** + * 9 pin DIN (Deutsches Institut für Normung) connector + */ + LIBGAMMA_CONNECTOR_TYPE_9PinDIN, + + /** + * DisplayPort + */ + LIBGAMMA_CONNECTOR_TYPE_DisplayPort, + + /** + * High-Definition Multimedia Interface (HDMI), unknown type + */ + LIBGAMMA_CONNECTOR_TYPE_HDMI, + + /** + * High-Definition Multimedia Interface, type A (HDMI-A) + */ + LIBGAMMA_CONNECTOR_TYPE_HDMIA, + + /** + * High-Definition Multimedia Interface, type B (HDMI-B) + */ + LIBGAMMA_CONNECTOR_TYPE_HDMIB, + + /** + * Television, unknown connector + */ + LIBGAMMA_CONNECTOR_TYPE_TV, + + /** + * Embedded DisplayPort (eDP) + */ + LIBGAMMA_CONNECTOR_TYPE_eDP, + + /** + * A virtual connector + */ + LIBGAMMA_CONNECTOR_TYPE_VIRTUAL, + + /** + * Display Serial Interface (DSI) + */ + LIBGAMMA_CONNECTOR_TYPE_DSI, + + /** + * LFP connector + * + * (What is this?) + */ + LIBGAMMA_CONNECTOR_TYPE_LFP + + /* DEVELOPERS: Remember to update LIBGAMMA_CONNECTOR_TYPE_COUNT below + * and LIST_CONNECTOR_TYPES in common.h when adding methods */ + +} libgamma_connector_type_t; + +/** + * The number of values defined in `libgamma_connector_type_t` + * in the version of the library the program is compiled against + */ +#define LIBGAMMA_CONNECTOR_TYPE_COUNT 20 + +/** + * The number of values defined in `libgamma_connector_type_t` + * in the version of the library the program is linked against + */ +extern const int libgamma_connector_type_count; + + + +/** + * 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 + + /* DEVELOPERS: Remember to update LIBGAMMA_SUBPIXEL_ORDER_COUNT below + * and LIST_SUBPIXEL_ORDERS in common.h when adding methods */ + +} libgamma_subpixel_order_t; + +/** + * The number of values defined in `libgamma_subpixel_order_t` + * in the version of the library the program is compiled against + */ +#define LIBGAMMA_SUBPIXEL_ORDER_COUNT 6 + +/** + * The number of values defined in `libgamma_subpixel_order_t` + * in the version of the library the program is linked against + */ +extern const int libgamma_subpixel_order_count; + + + +/** + * 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; + + + +/** + * Capabilities of adjustment methods + */ +typedef struct libgamma_method_capabilities { + /** + * OR of the CRTC information fields in `libgamma_crtc_information_t` + * that may (but can fail) be read successfully + */ + int32_t crtc_information; + + /** + * Whether the default site is known, if true the site is integrated + * to the system or can be determined using environment variables + */ + unsigned default_site_known : 1; + + /** + * Whether the adjustment method supports multiple sites rather + * than just the default site + */ + unsigned multiple_sites : 1; + + /** + * Whether the adjustment method supports multiple partitions + * per site + */ + unsigned multiple_partitions : 1; + + /** + * Whether the adjustment method supports multiple CRTC:s + * per partition per site + */ + unsigned multiple_crtcs : 1; + + /** + * Whether the partition to graphics card is a bijection + */ + unsigned partitions_are_graphics_cards : 1; + + /** + * Whether the adjustment method supports `libgamma_site_restore` + */ + unsigned site_restore : 1; + + /** + * Whether the adjustment method supports `libgamma_partition_restore` + */ + unsigned partition_restore : 1; + + /** + * Whether the adjustment method supports `libgamma_crtc_restore` + */ + unsigned crtc_restore : 1; + + /** + * Whether the `red_gamma_size`, `green_gamma_size` and `blue_gamma_size` + * fields in `libgamma_crtc_information_t` will always have the same + * values as each other for the adjustment method + */ + unsigned identical_gamma_sizes : 1; + + /** + * Whether the `red_gamma_size`, `green_gamma_size` and `blue_gamma_size` + * fields in `libgamma_crtc_information_t` will always be filled with the + * same value for the adjustment method + */ + unsigned fixed_gamma_size : 1; + + /** + * Whether the `gamma_depth` field in `libgamma_crtc_information_t` + * will always be filled with the same value for the adjustment method + */ + unsigned fixed_gamma_depth : 1; + + /** + * Whether the adjustment method will actually perform adjustments + */ + unsigned real : 1; + + /** + * Whether the adjustment method is implement using a translation layer + */ + unsigned fake : 1; + + /** + * Whether adjustments are undone when the process disconnects from + * the display server + */ + unsigned auto_restore : 1; + +} libgamma_method_capabilities_t; + + +/** + * Site state + * + * On operating systems that integrate a graphical environment + * there is usually just one site. However, one systems with + * pluggable graphics, like Unix-like systems such as GNU/Linux + * and the BSD:s, there can usually be any (feasible) number of + * sites. In X.org parlance they are called displays. + */ +typedef struct libgamma_site_state { + /** + * Adjustment method implementation specific data + * + * You as a user of this library should not touch this + */ + void *data; + + /** + * This field specifies, for the methods if this library, + * which adjustment method (display server and protocol) + * is used to adjust the gamma ramps + */ + int method; + + /** + * The site identifier. It can either be `NULL` or a string. + * `NULL` indicates the default site. On systems like the + * Unix-like systems, where the graphics are pluggable, this + * is usually resolved by an environment variable, such as + * "DISPLAY" for X.org. + */ + char *site; + + /** + * The number of partitions that is available on this site. + * Probably the majority of display server only one partition + * per site. However, X.org can, and traditional used to have + * on multi-headed environments, multiple partitions per site. + * In X.org partitions are called 'screens'. It is not to be + * confused with monitor. A screen is a collection of monitors, + * and the mapping from monitors to screens is a surjection. + * On hardware-level adjustment methods, such as Direct + * Rendering Manager, a partition is a graphics card. + */ + size_t partitions_available; + +} libgamma_site_state_t; + + +/** + * Partition state + * + * Probably the majority of display server only one partition + * per site. However, X.org can, and traditional used to have + * on multi-headed environments, multiple partitions per site. + * In X.org partitions are called 'screens'. It is not to be + * confused with monitor. A screen is a collection of monitors, + * and the mapping from monitors to screens is a surjection. + * On hardware-level adjustment methods, such as Direct + * Rendering Manager, a partition is a graphics card. + */ +typedef struct libgamma_partition_state { + /** + * Adjustment method implementation specific data + * + * You as a user of this library should not touch this + */ + void *data; + + /** + * The site this partition belongs to + */ + libgamma_site_state_t *site; + + /** + * The index of the partition + */ + size_t partition; + + /** + * The number of CRTC:s that are available + * under this partition + * + * Note that the CRTC:s are not necessarily + * online + */ + size_t crtcs_available; + +} libgamma_partition_state_t; + + +/** + * Cathode ray tube controller state + * + * The CRTC controls the gamma ramps for the + * monitor that is plugged in to the connector + * that the CRTC belongs to + */ +typedef struct libgamma_crtc_state { + /** + * Adjustment method implementation specific data + * + * You as a user of this library should not touch this + */ + void *data; + + /** + * The partition this CRTC belongs to + */ + libgamma_partition_state_t *partition; + + /** + * The index of the CRTC within its partition + */ + size_t crtc; + +} libgamma_crtc_state_t; + + +/** + * Cathode ray tube controller information data structure + */ +typedef struct libgamma_crtc_information { + +/** + * 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) + + /** + * 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; + + +/** + * 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) + + /** + * 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; + + +/** + * 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) + + /** + * 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; + + +/** + * 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) + + /** + * 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; + + +/** + * 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) + + /** + * 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; + + +/** + * 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) + + /** + * 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; + + +/** + * 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) + + /** + * 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; + + +/** + * 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) + + /** + * `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; + + +/** + * 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) + + /** + * 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; + + +/** + * For a `libgamma_crtc_information_t` fill in the + * value for `active` and report errors to `active_error` + */ +#define LIBGAMMA_CRTC_INFO_ACTIVE (1 << 9) + + /** + * 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; + + +/** + * 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) + + /** + * 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; + + +/** + * 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) + + /** + * 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; + + +/** + * For a `libgamma_crtc_information_t` fill in the + * values for `gamma_red`, `gamma_green`, and `gamma_blue` + * and report errors to `gamma_error` + */ +#define LIBGAMMA_CRTC_INFO_GAMMA (1 << 12) + + /** + * The 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; + + /* DEVELOPERS: Remember to update LIBGAMMA_CRTC_INFO_COUNT below and maybe + * also some of the list of LIBGAMMA_CRTC_INFO_* macros below */ + +} libgamma_crtc_information_t; + +/** + * The number of `LIBGAMMA_CRTC_INFO_*` values defined in + * the version of the library the program is compiled against + * + * This exclude the combining macros defined below this macro + */ +#define LIBGAMMA_CRTC_INFO_COUNT 13 + +/** + * The number of `LIBGAMMA_CRTC_INFO_*` values defined in + * the version of the library the program is linked against + */ +extern const int libgamma_crtc_info_count; + +/** + * Macro for both `libgamma_crtc_information_t` fields + * that can specify the size of the monitor's viewport + * as specified in the monitor's Extended Display + * Information Data + */ +#define LIBGAMMA_CRTC_INFO_MACRO_EDID_VIEWPORT (LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID | LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID) + +/** + * Macro for all `libgamma_crtc_information_t` fields + * that can be filled if the adjustment method have + * support for reading the monitors' Extended Display + * Information Data + */ +#define LIBGAMMA_CRTC_INFO_MACRO_EDID (LIBGAMMA_CRTC_INFO_EDID | LIBGAMMA_CRTC_INFO_MACRO_EDID_VIEWPORT | LIBGAMMA_CRTC_INFO_GAMMA) + +/** + * Macro for both `libgamma_crtc_information_t` fields + * that can specify the size of the monitor's viewport + * as provided by the adjustment method without this + * library having to parse the monitor's Extended Display + * Information Data + */ +#define LIBGAMMA_CRTC_INFO_MACRO_VIEWPORT (LIBGAMMA_CRTC_INFO_WIDTH_MM | LIBGAMMA_CRTC_INFO_HEIGHT_MM) + +/** + * Macro for the `libgamma_crtc_information_t` fields + * that specifies the CRTC's gamma ramp sizes and gamma + * ramp depth + */ +#define LIBGAMMA_CRTC_INFO_MACRO_RAMP (LIBGAMMA_CRTC_INFO_GAMMA_SIZE | LIBGAMMA_CRTC_INFO_GAMMA_DEPTH) + +/** + * Macro for the `libgamma_crtc_information_t` fields + * that specifies the CRTC's connector type and the + * partition unique name of the connector + */ +#define LIBGAMMA_CRTC_INFO_MACRO_CONNECTOR (LIBGAMMA_CRTC_INFO_CONNECTOR_NAME | LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE) + +/** + * Macro for the `libgamma_crtc_information_t` fields + * that required there is a monitor attached to the + * connector, and that status itself + */ +#define LIBGAMMA_CRTC_INFO_MACRO_ACTIVE (LIBGAMMA_CRTC_INFO_MACRO_EDID | LIBGAMMA_CRTC_INFO_MACRO_VIEWPORT |\ + LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER | LIBGAMMA_CRTC_INFO_ACTIVE) + + + +/** + * Gamma ramp structure for 8-bit gamma ramps + */ +typedef struct libgamma_gamma_ramps8 { + /** + * The size of `red` + */ + size_t red_size; + + /** + * The size of `green` + */ + size_t green_size; + + /** + * The size of `blue` + */ + size_t blue_size; + + /** + * The gamma ramp for the red channel + */ + uint8_t *red; + + /** + * The gamma ramp for the green channel + */ + uint8_t *green; + + /** + * The gamma ramp for the blue channel + */ + uint8_t *blue; + +} libgamma_gamma_ramps8_t; + + +/** + * Gamma ramp structure for 16-bit gamma ramps + */ +typedef struct libgamma_gamma_ramps16 +{ + /** + * The size of `red` + */ + size_t red_size; + + /** + * The size of `green` + */ + size_t green_size; + + /** + * The size of `blue` + */ + size_t blue_size; + + /** + * The gamma ramp for the red channel + */ + uint16_t *red; + + /** + * The gamma ramp for the green channel + */ + uint16_t *green; + + /** + * The gamma ramp for the blue channel + */ + uint16_t *blue; + +} libgamma_gamma_ramps16_t; + + +/** + * Gamma ramp structure for 32-bit gamma ramps + */ +typedef struct libgamma_gamma_ramps32 +{ + /** + * The size of `red` + */ + size_t red_size; + + /** + * The size of `green` + */ + size_t green_size; + + /** + * The size of `blue` + */ + size_t blue_size; + + /** + * The gamma ramp for the red channel + */ + uint32_t *red; + + /** + * The gamma ramp for the green channel + */ + uint32_t *green; + + /** + * The gamma ramp for the blue channel + */ + uint32_t *blue; + +} libgamma_gamma_ramps32_t; + + +/** + * Gamma ramp structure for 64-bit gamma ramps + */ +typedef struct libgamma_gamma_ramps64 +{ + /** + * The size of `red` + */ + size_t red_size; + + /** + * The size of `green` + */ + size_t green_size; + + /** + * The size of `blue` + */ + size_t blue_size; + + /** + * The gamma ramp for the red channel + */ + uint64_t *red; + + /** + * The gamma ramp for the green channel + */ + uint64_t *green; + + /** + * The gamma ramp for the blue channel + */ + uint64_t *blue; + +} libgamma_gamma_ramps64_t; + + +/** + * Gamma ramp structure for `float` gamma ramps + */ +typedef struct libgamma_gamma_rampsf +{ + /** + * The size of `red` + */ + size_t red_size; + + /** + * The size of `green` + */ + size_t green_size; + + /** + * The size of `blue` + */ + size_t blue_size; + + /** + * The gamma ramp for the red channel + */ + float *red; + + /** + * The gamma ramp for the green channel + */ + float *green; + + /** + * The gamma ramp for the blue channel + */ + float *blue; + +} libgamma_gamma_rampsf_t; + + +/** + * Gamma ramp structure for `double` gamma ramps + */ +typedef struct libgamma_gamma_rampsd +{ + /** + * The size of `red` + */ + size_t red_size; + + /** + * The size of `green` + */ + size_t green_size; + + /** + * The size of `blue` + */ + size_t blue_size; + + /** + * The gamma ramp for the red channel + */ + double *red; + + /** + * The gamma ramp for the green channel + */ + double *green; + + /** + * The gamma ramp for the blue channel + */ + double *blue; + +} libgamma_gamma_rampsd_t; + + +/** + * Mapping function from [0, 1] float encoding value to [0, 2⁸ − 1] integer output value + * + * @param encoding [0, 1] float encoding value + * @return [0, 2⁸ − 1] integer output value + */ +typedef uint8_t libgamma_gamma_ramps8_fun(float); + +/** + * Mapping function from [0, 1] float encoding value to [0, 2¹⁶ − 1] integer output value + * + * @param encoding [0, 1] float encoding value + * @return [0, 2¹⁶ − 1] integer output value + */ +typedef uint16_t libgamma_gamma_ramps16_fun(float); + +/** + * Mapping function from [0, 1] float encoding value to [0, 2³² − 1] integer output value + * + * @param encoding [0, 1] float encoding value + * @return [0, 2³² − 1] integer output value + */ +typedef uint32_t libgamma_gamma_ramps32_fun(float); + +/** + * Mapping function from [0, 1] float encoding value to [0, 2⁶⁴ − 1] integer output value + * + * @param encoding [0, 1] float encoding value + * @return [0, 2⁶⁴ − 1] integer output value + */ +typedef uint64_t libgamma_gamma_ramps64_fun(float); + +/** + * Mapping function from [0, 1] float encoding value to [0, 1] float output value + * + * @param encoding [0, 1] float encoding value + * @return [0, 1] float output value + */ +typedef float libgamma_gamma_rampsf_fun(float); + +/** + * Mapping function from [0, 1] double precision float encoding + * value to [0, 1] double precision float output value + * + * @param encoding [0, 1] float encoding value + * @return [0, 1] float output value + */ +typedef double libgamma_gamma_rampsd_fun(double); + + + +/** + * Prints an error to stderr in a `perror` fashion + * + * @param name The text to add at the beginning + * @param error_code The error code, may be an `errno` value + */ +void libgamma_perror(const char *, int); + +/** + * Get a description of an error + * + * @param error_code The error code, may be an `errno` value, if + * `LIBGAMMA_ERRNO_SET`, the current value of `errno` + * will be used + * @return The description associated with the error code, + * `NULL` if the error code is not recognised + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__warn_unused_result__))) +const char *libgamma_strerror(int); + +/** + * Get a description of an error + * + * @param error_code The error code, may be an `errno` value, if + * `LIBGAMMA_ERRNO_SET`, the current value of `errno` + * will be used + * @param buf Buffer that shall be used if a description must be generated + * @param bufsize The size of `buf`, 1024 is recommended + * @return The description associated with the error code; + * can only be `NULL` if `buf` is `NULL`. If the buffer + * is insufficient, a truncated but NUL-terminated + * description is returned and `errno` is set to `ERANGE`; + * `errno` is otherwise unmodified + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__warn_unused_result__))) +const char *libgamma_strerror_r(int, char[], size_t); + +/** + * Get the name of the definition associated with a `libgamma` error code + * + * @param value The error code + * @return The name of the definition associated with the error code, + * `NULL` if the error code does not exist + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__warn_unused_result__, __const__))) +const char *libgamma_name_of_error(int); + +/** + * Get the value of a `libgamma` error definition refered to by name + * + * @param name The name of the definition associated with the error code + * @return The error code, zero if the name is `NULL` + * or does not refer to a `libgamma` error + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__warn_unused_result__, __pure__))) +int libgamma_value_of_error(const char *); + +/** + * Get the group that the user needs to be a member + * of if `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned + * + * @return The group that the user needs to be a member of + * if `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned + */ +#ifndef __WIN32__ +gid_t libgamma_group_gid_get(void); +#else +short libgamma_group_gid_get(void); +#endif + +/** + * Set the group that the user needs to be a member + * of if `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned + * + * @param value The group that the user needs to be a member of + * if `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned + */ +#ifndef __WIN32__ +void libgamma_group_gid_set(gid_t); +#else +void libgamma_group_gid_set(short); +#endif + +/** + * Get the group that the user needs to be a member of + * if `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned + * + * @return The 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 + */ +const char *libgamma_group_name_get(void); + +/** + * Set the group that the user needs to be a member of + * if `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned + * + * @param value The group that the user needs to be a member of if + * `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned, may be `NULL` + */ +void libgamma_group_name_set(const char *); + + + + +/** + * Get the name of an adjustment method, + * for example "randr" for `LIBGAMMA_METHOD_X_RANDR` + * + * @param method The adjustment method + * @return The name adjustment method, `NULL` if not + * recognised (errno is not changed) + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__warn_unused_result__, __const__))) +const char *libgamma_name_of_method(int); + +/** + * Get the name of the constant for an adjustment + * method, for example "LIBGAMMA_METHOD_X_RANDR" + * for `LIBGAMMA_METHOD_X_RANDR` + * + * @param method The adjustment method + * @return The name of the constant for adjustment method, + * `NULL` if not recognised (errno is not changed) + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__warn_unused_result__, __const__))) +const char *libgamma_const_of_method(int); + +/** + * Check whether an adjustment method is available, + * non-existing (invalid) methods will be identified + * as not available under the rationale that the + * library may be out of date + * + * @param method The adjustment method + * @return Whether the adjustment method is available + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__warn_unused_result__, __const__))) +int libgamma_is_method_available(int); + +/** + * Get the value of an adjustment method + * + * @param method The name of the adjustment method, for example + * "randr" or "LIBGAMMA_METHOD_X_RANDR" + * @return The adjustment method; for example `LIBGAMMA_METHOD_X_RANDR` + * for "randr" and "LIBGAMMA_METHOD_X_RANDR" + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__warn_unused_result__, __pure__))) +int libgamma_value_of_method(const char *); + + + +/** + * Get the name of a connector type, + * for example "VGA" for `LIBGAMMA_CONNECTOR_TYPE_VGA` + * + * "Unknown" is returned for `LIBGAMMA_CONNECTOR_TYPE_Unknown`, + * "TV" is returned for `LIBGAMMA_CONNECTOR_TYPE_TV`, + * "Virtual" is returned for `LIBGAMMA_CONNECTOR_TYPE_Virtual` + * + * @param connector The connector type + * @return The name connector type, `NULL` if not + * recognised (errno is not changed) + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__warn_unused_result__, __const__))) +const char *libgamma_name_of_connector_type(int); + +/** + * Get the name of the constant for a connector + * type, for example "LIBGAMMA_CONNECTOR_TYPE_VGA" + * for `LIBGAMMA_CONNECTOR_TYPE_VGA` + * + * @param connector The connector type + * @return The name of the constant for connector type, + * `NULL` if not recognised (errno is not changed) + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__warn_unused_result__, __const__))) +const char *libgamma_const_of_connector_type(int); + +/** + * Get the value of a connector type + * + * @param connector The name of the connector type, for example + * "VGA" or "LIBGAMMA_CONNECTOR_TYPE_VGA" + * @return The connector type; for example `LIBGAMMA_CONNECTOR_TYPE_VGA` + * for "VGA" and "LIBGAMMA_CONNECTOR_TYPE_VGA" + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__warn_unused_result__, __pure__))) +int libgamma_value_of_connector_type(const char *); + + + +/** + * Get the name of a subpixel order, for example + * "Horizontal RGB" for `LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_RGB` + * + * "Unknown" is returned for `LIBGAMMA_SUBPIXEL_ORDER_UNKNOWN`, + * "None" is returned for `LIBGAMMA_SUBPIXEL_ORDER_NONE` + * + * @param order The subpixel order + * @return The name subpixel order, `NULL` if not + * recognised (errno is not changed) + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__warn_unused_result__, __const__))) +const char *libgamma_name_of_subpixel_order(int); + +/** + * Get the name of the constant for a subpixel order, + * for example "LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_RGB" + * for `LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_RGB` + * + * @param order The subpixel order + * @return The name of the constant for subpixel order, + * `NULL` if not recognised (errno is not changed) + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__warn_unused_result__, __const__))) +const char *libgamma_const_of_subpixel_order(int); + +/** + * Get the value of a subpixel order + * + * @param order The name of the subpixel order, for example + * "Horizontal RGB" or "LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_RGB" + * @return The subpixel order; for example `LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_RGB` + * for "Horizontal RGB" and "LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_RGB" + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__warn_unused_result__, __pure__))) +int libgamma_value_of_subpixel_order(const char *); + + + +/** + * Initialise a gamma ramp in the proper way that allows all adjustment + * methods to read from and write to it without causing segmentation violation + * + * The input must have `red_size`, `green_size`, and `blue_size` set to the + * sizes of the gamma ramps that should be allocated + * + * @param this The gamma ramps + * @return Zero on success, -1 on allocation error, `errno` will be set accordingly + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_gamma_ramps8_initialise(libgamma_gamma_ramps8_t *restrict); + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_ramps8_initialise` or otherwise + * initialised in the proper manner + * + * @param this The gamma ramps + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +void libgamma_gamma_ramps8_destroy(libgamma_gamma_ramps8_t *restrict); + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_ramps8_initialise` or otherwise + * initialised in the proper manner, as well as release the pointer + * to the structure + * + * @param this The gamma ramps + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +void libgamma_gamma_ramps8_free(libgamma_gamma_ramps8_t *restrict); + + +/** + * Initialise a gamma ramp in the proper way that allows all adjustment + * methods to read from and write to it without causing segmentation violation + * + * The input must have `red_size`, `green_size`, and `blue_size` set to the + * sizes of the gamma ramps that should be allocated + * + * @param this The gamma ramps + * @return Zero on success, -1 on allocation error, `errno` will be set accordingly + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_gamma_ramps16_initialise(libgamma_gamma_ramps16_t *restrict); + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_ramps16_initialise` or otherwise + * initialised in the proper manner + * + * @param this The gamma ramps + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +void libgamma_gamma_ramps16_destroy(libgamma_gamma_ramps16_t *restrict); + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_ramps16_initialise` or otherwise + * initialised in the proper manner, as well as release the pointer + * to the structure + * + * @param this The gamma ramps + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +void libgamma_gamma_ramps16_free(libgamma_gamma_ramps16_t *restrict); + + +/** + * Initialise a gamma ramp in the proper way that allows all adjustment + * methods to read from and write to it without causing segmentation violation + * + * The input must have `red_size`, `green_size`, and `blue_size` set to the + * sizes of the gamma ramps that should be allocated. + * + * @param this The gamma ramps + * @return Zero on success, -1 on allocation error, `errno` will be set accordingly + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_gamma_ramps32_initialise(libgamma_gamma_ramps32_t *restrict); + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_ramps32_initialise` or otherwise + * initialised in the proper manner + * + * @param this The gamma ramps + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +void libgamma_gamma_ramps32_destroy(libgamma_gamma_ramps32_t *restrict); + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_ramps32_initialise` or otherwise + * initialised in the proper manner, as well as release the pointer + * to the structure + * + * @param this The gamma ramps + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +void libgamma_gamma_ramps32_free(libgamma_gamma_ramps32_t *restrict); + + +/** + * Initialise a gamma ramp in the proper way that allows all adjustment + * methods to read from and write to it without causing segmentation violation. + * + * The input must have `red_size`, `green_size`, and `blue_size` set to the + * sizes of the gamma ramps that should be allocated. + * + * @param this The gamma ramps + * @return Zero on success, -1 on allocation error, `errno` will be set accordingly + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_gamma_ramps64_initialise(libgamma_gamma_ramps64_t *restrict); + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_ramps64_initialise` or otherwise + * initialised in the proper manner + * + * @param this The gamma ramps + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +void libgamma_gamma_ramps64_destroy(libgamma_gamma_ramps64_t *restrict); + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_ramps64_initialise` or otherwise + * initialised in the proper manner, as well as release the pointer + * to the structure + * + * @param this The gamma ramps + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +void libgamma_gamma_ramps64_free(libgamma_gamma_ramps64_t *restrict); + + +/** + * Initialise a gamma ramp in the proper way that allows all adjustment + * methods to read from and write to it without causing segmentation violation + * + * The input must have `red_size`, `green_size`, and `blue_size` set to the + * sizes of the gamma ramps that should be allocated + * + * @param this The gamma ramps + * @return Zero on success, -1 on allocation error, `errno` will be set accordingly + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_gamma_rampsf_initialise(libgamma_gamma_rampsf_t *restrict); + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_rampsf_initialise` or otherwise + * initialised in the proper manner + * + * @param this The gamma ramps + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +void libgamma_gamma_rampsf_destroy(libgamma_gamma_rampsf_t *restrict); + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_rampsf_initialise` or otherwise + * initialised in the proper manner, as well as release the pointer + * to the structure + * + * @param this The gamma ramps + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +void libgamma_gamma_rampsf_free(libgamma_gamma_rampsf_t *restrict); + + +/** + * Initialise a gamma ramp in the proper way that allows all adjustment + * methods to read from and write to it without causing segmentation violation + * + * The input must have `red_size`, `green_size`, and `blue_size` set to the + * sizes of the gamma ramps that should be allocated + * + * @param this The gamma ramps + * @return Zero on success, -1 on allocation error, `errno` will be set accordingly + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_gamma_rampsd_initialise(libgamma_gamma_rampsd_t *restrict); + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_rampsd_initialise` or otherwise + * initialised in the proper manner + * + * @param this The gamma ramps + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +void libgamma_gamma_rampsd_destroy(libgamma_gamma_rampsd_t *restrict); + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_rampsd_initialise` or otherwise + * initialised in the proper manner, as well as release the pointer + * to the structure + * + * @param this The gamma ramps + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +void libgamma_gamma_rampsd_free(libgamma_gamma_rampsd_t *restrict); + + + +/** + * List available adjustment methods by their order of preference based on the environment + * + * @param methods Output array of methods, should be able to hold `LIBGAMMA_METHOD_COUNT` elements + * @param buf_size The number of elements that fits in `methods`, it should be `LIBGAMMA_METHOD_COUNT`, + * This is used to avoid writing outside the output buffer if this library adds new + * adjustment methods without the users of the library recompiling + * @param operation Allowed values: + * 0: Methods that the environment suggests will work, excluding fake + * 1: Methods that the environment suggests will work, including fake + * 2: All real non-fake methods + * 3: All real methods + * 4: All methods + * Other values invoke undefined behaviour + * @return The number of element that have been stored in `methods`, or should + * have been stored if the buffer was large enough + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__warn_unused_result__))) +size_t libgamma_list_methods(int *restrict, size_t, int); + +/** + * 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) + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_method_capabilities(libgamma_method_capabilities_t *restrict, int); + +/** + * Return the default site for an adjustment method + * + * @param method The adjustment method (display server and protocol) + * @return The default site, `NULL` if it cannot be determined or + * if multiple sites are not supported by the adjustment + * method + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__warn_unused_result__))) +const char *libgamma_method_default_site(int); + +/** + * Return the default variable that determines + * the default site for an adjustment method + * + * @param method The adjustment method (display server and protocol) + * @return The environ variables that is used to determine the + * default site, `NULL` if there is none, that is, if + * the method does not support multiple sites + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__warn_unused_result__))) +const char *libgamma_method_default_site_variable(int); + + + +/** + * Initialise an allocated site state + * + * @param this The site state to initialise + * @param method The adjustment method (display server and protocol) + * @param site The site identifier, unless it is `NULL` it must a + * `free`:able. Once the state is destroyed the library + * will attempt to free it. There you should not free + * it yourself, and it must not be a string constant + * or allocate on the stack. Note however that it will + * not be `free`:d if this function fails. + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__(1)))) +int libgamma_site_initialise(libgamma_site_state_t *restrict, int, char *restrict); + +/** + * Release all resources held by a site state + * + * @param this The site state + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +void libgamma_site_destroy(libgamma_site_state_t *restrict); + +/** + * Release all resources held by a site state + * and free the site state pointer + * + * @param this The site state + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +inline void +libgamma_site_free(libgamma_site_state_t *restrict this__) +{ + libgamma_site_destroy(this__); + free(this__); +} + +/** + * Restore the gamma ramps all CRTC:s within a site to the system settings + * + * @param this The site state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_site_restore(libgamma_site_state_t *restrict); + + + +/** + * Initialise an allocated partition state + * + * @param this The partition state to initialise + * @param site The site state for the site that the partition belongs to + * @param partition The index of the partition within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); + +/** + * Release all resources held by a partition state + * + * @param this The partition state + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +void libgamma_partition_destroy(libgamma_partition_state_t *restrict); + +/** + * Release all resources held by a partition state + * and free the partition state pointer + * + * @param this The partition state + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +inline void +libgamma_partition_free(libgamma_partition_state_t *restrict this__) +{ + libgamma_partition_destroy(this__); + free(this__); +} + +/** + * Restore the gamma ramps all CRTC:s within a partition to the system settings + * + * @param this The partition state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_partition_restore(libgamma_partition_state_t *restrict); + + + +/** + * Initialise an allocated CRTC state + * + * @param this The CRTC state to initialise + * @param partition The partition state for the partition that the CRTC belongs to + * @param crtc The index of the CRTC within the partition + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); + +/** + * Release all resources held by a CRTC state + * + * @param this The CRTC state + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +void libgamma_crtc_destroy(libgamma_crtc_state_t *restrict); + +/** + * Release all resources held by a CRTC state + * and free the CRTC state pointer + * + * @param this The CRTC state + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +inline void +libgamma_crtc_free(libgamma_crtc_state_t *restrict this__) +{ + libgamma_crtc_destroy(this__); + free(this__); +} + +/** + * Restore the gamma ramps for a CRTC to the system settings for that CRTC + * + * @param this The CRTC state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_crtc_restore(libgamma_crtc_state_t *restrict); + + +/** + * Read information about a CRTC + * + * @param this Instance of a data structure to fill with the information about the CRTC + * @param crtc The state of the CRTC whose information should be read + * @param fields OR:ed identifiers for the information about the CRTC that should be read + * @return Zero on success, -1 on error; on error refer to the error reports in `this` + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_get_crtc_information(libgamma_crtc_information_t *restrict, libgamma_crtc_state_t *restrict, int32_t); + +/** + * Release all resources in an information data structure for a CRTC + * + * @param this The CRTC information + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +void libgamma_crtc_information_destroy(libgamma_crtc_information_t *restrict); + +/** + * Release all resources in an information data structure for a CRTC + * and free the data structure pointer + * + * @param this The CRTC information + */ +inline void +libgamma_crtc_information_free(libgamma_crtc_information_t *restrict this__) +{ + libgamma_crtc_information_destroy(this__); + free(this__); +} + + +/** + * Convert a raw representation of an EDID to a 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 + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__warn_unused_result__))) +char *libgamma_behex_edid_lowercase(const unsigned char *restrict, size_t); + +/** + * Convert a raw representation of an EDID to an uppercase hexadecimal representation + * + * @param edid The EDID in raw representation + * @param length The length of `edid` + * @return The EDID in uppercase hexadecimal representation, + * NULL` on allocation error, `errno` will be set accordingly + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__warn_unused_result__))) +char *libgamma_behex_edid_uppercase(const unsigned char *restrict, size_t); + +/** + * Convert a raw representation of an EDID to a lowercase hexadecimal representation + * + * @param edid:const unsigned char* The EDID in raw representation + * @param length:size_t The length of `edid` + * @return :char* The EDID in lowercase hexadecimal representation, + * `NULL` on allocation error, `errno` will be set accordingly + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__warn_unused_result__))) +inline char * +libgamma_behex_edid(const unsigned char *restrict edid__, size_t length__) +{ + return libgamma_behex_edid_lowercase(edid__, length__); +} + +/** + * Convert an hexadecimal representation of an EDID to a raw representation + * + * @param edid The EDID in hexadecimal representation + * @return The EDID in raw representation, it will be half the length + * of `edid` (the input value); `NULL` on allocation error or + * if the EDID is malformated, `errno` will be set accordingly + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__warn_unused_result__, __nonnull__))) +unsigned char *libgamma_unhex_edid(const char *restrict); + + + +/** + * Get the current gamma ramps for a CRTC, 8-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_crtc_get_gamma_ramps8(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps8_t *restrict); + +/** + * Set the gamma ramps for a CRTC, 8-bit gamma-depth version. + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_crtc_set_gamma_ramps8(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps8_t); + +/** + * Set the gamma ramps for a CRTC, 8-bit gamma-depth function version + * + * Note that this will probably involve the library allocating temporary data + * + * @param this The CRTC state + * @param red_function The function that generates the gamma ramp for the red channel + * @param green_function The function that generates the gamma ramp for the green channel + * @param blue_function The function that generates the gamma ramp for the blue channel + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_crtc_set_gamma_ramps8_f(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps8_fun *, + libgamma_gamma_ramps8_fun *, libgamma_gamma_ramps8_fun *); + + +/** + * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__, __hot__))) +int libgamma_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t *restrict); + +/** + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__, __hot__))) +int libgamma_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t); + +/** + * Set the gamma ramps for a CRTC, 16-bit gamma-depth function version + * + * Note that this will probably involve the library allocating temporary data + * + * @param this The CRTC state + * @param red_function The function that generates the gamma ramp for the red channel + * @param green_function The function that generates the gamma ramp for the green channel + * @param blue_function The function that generates the gamma ramp for the blue channel + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__, __hot__))) +int libgamma_crtc_set_gamma_ramps16_f(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_fun *, + libgamma_gamma_ramps16_fun *, libgamma_gamma_ramps16_fun *); + + +/** + * Get the current gamma ramps for a CRTC, 32-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_crtc_get_gamma_ramps32(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps32_t *restrict); + +/** + * Set the gamma ramps for a CRTC, 32-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_crtc_set_gamma_ramps32(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps32_t); + +/** + * Set the gamma ramps for a CRTC, 32-bit gamma-depth function version + * + * Note that this will probably involve the library allocating temporary data + * + * @param this The CRTC state + * @param red_function The function that generates the gamma ramp for the red channel + * @param green_function The function that generates the gamma ramp for the green channel + * @param blue_function The function that generates the gamma ramp for the blue channel + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_crtc_set_gamma_ramps32_f(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps32_fun *, + libgamma_gamma_ramps32_fun *, libgamma_gamma_ramps32_fun *); + + +/** + * Get the current gamma ramps for a CRTC, 64-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_crtc_get_gamma_ramps64(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps64_t *restrict); + +/** + * Set the gamma ramps for a CRTC, 64-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_crtc_set_gamma_ramps64(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps64_t); + +/** + * Set the gamma ramps for a CRTC, 64-bit gamma-depth function version + * + * Note that this will probably involve the library allocating temporary data + * + * @param this The CRTC state + * @param red_function The function that generates the gamma ramp for the red channel + * @param green_function The function that generates the gamma ramp for the green channel + * @param blue_function The function that generates the gamma ramp for the blue channel + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +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` version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_crtc_set_gamma_rampsf(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsf_t); + +/** + * Get the current gamma ramps for a CRTC, `float` version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_crtc_get_gamma_rampsf(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsf_t *restrict); + +/** + * Set the gamma ramps for a CRTC, `float` function version + * + * Note that this will probably involve the library allocating temporary data + * + * @param this The CRTC state + * @param red_function The function that generates the gamma ramp for the red channel + * @param green_function The function that generates the gamma ramp for the green channel + * @param blue_function The function that generates the gamma ramp for the blue channel + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_crtc_set_gamma_rampsf_f(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsf_fun *, + libgamma_gamma_rampsf_fun *, libgamma_gamma_rampsf_fun *); + + +/** + * Get the current gamma ramps for a CRTC, `double` version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_crtc_get_gamma_rampsd(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsd_t *restrict); + +/** + * Set the gamma ramps for a CRTC, `double` version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int libgamma_crtc_set_gamma_rampsd(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsd_t); + +/** + * Set the gamma ramps for a CRTC, `double` function version + * + * Note that this will probably involve the library allocating temporary data + * + * @param this The CRTC state + * @param red_function The function that generates the gamma ramp for the red channel + * @param green_function The function that generates the gamma ramp for the green channel + * @param blue_function The function that generates the gamma ramp for the blue channel + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +LIBGAMMA_GCC_ONLY__(__attribute__((__nonnull__))) +int libgamma_crtc_set_gamma_rampsd_f(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsd_fun *, + libgamma_gamma_rampsd_fun *, libgamma_gamma_rampsd_fun *); + + + +#undef LIBGAMMA_GCC_ONLY__ +#endif diff --git a/libgamma_behex_edid.c b/libgamma_behex_edid.c new file mode 100644 index 0000000..269d68f --- /dev/null +++ b/libgamma_behex_edid.c @@ -0,0 +1,13 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Convert a raw representation of an EDID to a lowercase hexadecimal representation + * + * @param edid:const unsigned char* The EDID in raw representation + * @param length:size_t The length of `edid` + * @return :char* The EDID in lowercase hexadecimal representation, + * `NULL` on allocation error, `errno` will be set accordingly + */ +extern inline char *libgamma_behex_edid(const unsigned char *restrict, size_t); diff --git a/libgamma_behex_edid_lowercase.c b/libgamma_behex_edid_lowercase.c new file mode 100644 index 0000000..9c4f04e --- /dev/null +++ b/libgamma_behex_edid_lowercase.c @@ -0,0 +1,33 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Convert a raw representation of an EDID to a lowercase hexadecimal representation + * + * @param edid The EDID in raw representation + * @param length The length of `edid` + * @return The EDID in lowercase hexadecimal representation, + * `NULL` on allocation error, `errno` will be set accordingly + */ +char * +libgamma_behex_edid_lowercase(const unsigned char *restrict edid, size_t length) +{ + char *restrict out; + size_t i; + + /* Allocate memory area for the output string */ + out = malloc((length * 2 + 1) * sizeof(char)); + if (!out) + return NULL; + + /* Translate from raw octets to hexadecimal */ + for (i = 0; i < length; i++) { + out[i * 2 + 0] = "0123456789abcdef"[(edid[i] >> 4) & 15]; + out[i * 2 + 1] = "0123456789abcdef"[(edid[i] >> 0) & 15]; + } + /* NUL-terminate the output string */ + out[length * 2] = '\0'; + + return out; +} diff --git a/libgamma_behex_edid_uppercase.c b/libgamma_behex_edid_uppercase.c new file mode 100644 index 0000000..5545aa5 --- /dev/null +++ b/libgamma_behex_edid_uppercase.c @@ -0,0 +1,33 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Convert a raw representation of an EDID to an uppercase hexadecimal representation + * + * @param edid The EDID in raw representation + * @param length The length of `edid` + * @return The EDID in uppercase hexadecimal representation, + * NULL` on allocation error, `errno` will be set accordingly + */ +char * +libgamma_behex_edid_uppercase(const unsigned char *restrict edid, size_t length) +{ + char *restrict out; + size_t i; + + /* Allocate memory area for the output string */ + out = malloc((length * 2 + 1) * sizeof(char)); + if (!out) + return NULL; + + /* Translate from raw octets to hexadecimal */ + for (i = 0; i < length; i++) { + out[i * 2 + 0] = "0123456789ABCDEF"[(edid[i] >> 4) & 15]; + out[i * 2 + 1] = "0123456789ABCDEF"[(edid[i] >> 0) & 15]; + } + /* NUL-terminate the output string */ + out[length * 2] = '\0'; + + return out; +} diff --git a/libgamma_connector_type_count.c b/libgamma_connector_type_count.c new file mode 100644 index 0000000..75934e1 --- /dev/null +++ b/libgamma_connector_type_count.c @@ -0,0 +1,9 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * The number of values defined in `libgamma_connector_type_t` + * in the version of the library the program is linked against + */ +extern const int libgamma_connector_type_count; diff --git a/libgamma_const_of_connector_type.c b/libgamma_const_of_connector_type.c new file mode 100644 index 0000000..a006b88 --- /dev/null +++ b/libgamma_const_of_connector_type.c @@ -0,0 +1,25 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Get the name of the constant for a connector + * type, for example "LIBGAMMA_CONNECTOR_TYPE_VGA" + * for `LIBGAMMA_CONNECTOR_TYPE_VGA` + * + * @param connector The connector type + * @return The name of the constant for connector type, + * `NULL` if not recognised (errno is not changed) + */ +const char * +libgamma_const_of_connector_type(int connector) +{ + switch (connector) { +#define X(CONST, NAME)\ + case CONST: return #CONST; + LIST_CONNECTOR_TYPES(X) +#undef X + default: + return NULL; + } +} diff --git a/libgamma_const_of_method.c b/libgamma_const_of_method.c new file mode 100644 index 0000000..e6fd872 --- /dev/null +++ b/libgamma_const_of_method.c @@ -0,0 +1,24 @@ +#include "common.h" + + +/** + * Get the name of the constant for an adjustment + * method, for example "LIBGAMMA_METHOD_X_RANDR" + * for `LIBGAMMA_METHOD_X_RANDR` + * + * @param method The adjustment method + * @return The name of the constant for adjustment method, + * `NULL` if not recognised (errno is not changed) + */ +const char * +libgamma_const_of_method(int method) +{ + switch (method) { +#define X(CONST, NAME, CNAME, ENABLED)\ + case CONST: return #CONST; + LIST_METHODS(X) +#undef X + default: + return NULL; + } +} diff --git a/libgamma_const_of_subpixel_order.c b/libgamma_const_of_subpixel_order.c new file mode 100644 index 0000000..7f69bea --- /dev/null +++ b/libgamma_const_of_subpixel_order.c @@ -0,0 +1,25 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Get the name of the constant for a subpixel order, + * for example "LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_RGB" + * for `LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_RGB` + * + * @param order The subpixel order + * @return The name of the constant for subpixel order, + * `NULL` if not recognised (errno is not changed) + */ +const char * +libgamma_const_of_subpixel_order(int order) +{ + switch (order) { +#define X(CONST, NAME)\ + case CONST: return #CONST; + LIST_SUBPIXEL_ORDERS(X) +#undef X + default: + return NULL; + } +} diff --git a/libgamma_crtc_destroy.c b/libgamma_crtc_destroy.c new file mode 100644 index 0000000..9724e31 --- /dev/null +++ b/libgamma_crtc_destroy.c @@ -0,0 +1,23 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Release all resources held by a CRTC state + * + * @param this The CRTC state + */ +void +libgamma_crtc_destroy(libgamma_crtc_state_t *restrict this) +{ + switch (this->partition->site->method) { +#define X(CONST, CNAME, DEPTH, RAMPS)\ + case CONST:\ + libgamma_##CNAME##_crtc_destroy(this);\ + break; + LIST_AVAILABLE_METHODS(X) +#undef X + default: + break; + } +} diff --git a/libgamma_crtc_free.c b/libgamma_crtc_free.c new file mode 100644 index 0000000..986fd96 --- /dev/null +++ b/libgamma_crtc_free.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Release all resources held by a CRTC state + * and free the CRTC state pointer + * + * @param this The CRTC state + */ +extern inline void libgamma_crtc_free(libgamma_crtc_state_t *restrict); diff --git a/libgamma_crtc_get_gamma_ramps16.c b/libgamma_crtc_get_gamma_ramps16.c new file mode 100644 index 0000000..388f03f --- /dev/null +++ b/libgamma_crtc_get_gamma_ramps16.c @@ -0,0 +1,21 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t* restrict ramps) +{ +#define DEPTH 16 +#define RAMPS ramps16 +#define TYPE bits16 +#define APPEND_RAMPS(X) X##ramps16 +#include "get_ramps.h" +} diff --git a/libgamma_crtc_get_gamma_ramps32.c b/libgamma_crtc_get_gamma_ramps32.c new file mode 100644 index 0000000..32ce2ef --- /dev/null +++ b/libgamma_crtc_get_gamma_ramps32.c @@ -0,0 +1,21 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Get the current gamma ramps for a CRTC, 32-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_crtc_get_gamma_ramps32(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps32_t* restrict ramps) +{ +#define DEPTH 32 +#define RAMPS ramps32 +#define TYPE bits32 +#define APPEND_RAMPS(X) X##ramps32 +#include "get_ramps.h" +} diff --git a/libgamma_crtc_get_gamma_ramps64.c b/libgamma_crtc_get_gamma_ramps64.c new file mode 100644 index 0000000..3db19f9 --- /dev/null +++ b/libgamma_crtc_get_gamma_ramps64.c @@ -0,0 +1,21 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Get the current gamma ramps for a CRTC, 64-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_crtc_get_gamma_ramps64(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps64_t* restrict ramps) +{ +#define DEPTH 64 +#define RAMPS ramps64 +#define TYPE bits64 +#define APPEND_RAMPS(X) X##ramps64 +#include "get_ramps.h" +} diff --git a/libgamma_crtc_get_gamma_ramps8.c b/libgamma_crtc_get_gamma_ramps8.c new file mode 100644 index 0000000..9e359aa --- /dev/null +++ b/libgamma_crtc_get_gamma_ramps8.c @@ -0,0 +1,21 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Get the current gamma ramps for a CRTC, 8-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_crtc_get_gamma_ramps8(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps8_t* restrict ramps) +{ +#define DEPTH 8 +#define RAMPS ramps8 +#define TYPE bits8 +#define APPEND_RAMPS(X) X##ramps8 +#include "get_ramps.h" +} diff --git a/libgamma_crtc_get_gamma_rampsd.c b/libgamma_crtc_get_gamma_rampsd.c new file mode 100644 index 0000000..e00e86c --- /dev/null +++ b/libgamma_crtc_get_gamma_rampsd.c @@ -0,0 +1,21 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Get the current gamma ramps for a CRTC, `double` version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_crtc_get_gamma_rampsd(libgamma_crtc_state_t *restrict this, libgamma_gamma_rampsd_t* restrict ramps) +{ +#define DEPTH -2 +#define RAMPS rampsd +#define TYPE float_double +#define APPEND_RAMPS(X) X##rampsd +#include "get_ramps.h" +} diff --git a/libgamma_crtc_get_gamma_rampsf.c b/libgamma_crtc_get_gamma_rampsf.c new file mode 100644 index 0000000..ee233ca --- /dev/null +++ b/libgamma_crtc_get_gamma_rampsf.c @@ -0,0 +1,21 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Get the current gamma ramps for a CRTC, `float` version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_crtc_get_gamma_rampsf(libgamma_crtc_state_t *restrict this, libgamma_gamma_rampsf_t* restrict ramps) +{ +#define DEPTH -1 +#define RAMPS rampsf +#define TYPE float_single +#define APPEND_RAMPS(X) X##rampsf +#include "get_ramps.h" +} diff --git a/libgamma_crtc_info_count.c b/libgamma_crtc_info_count.c new file mode 100644 index 0000000..1b68fe3 --- /dev/null +++ b/libgamma_crtc_info_count.c @@ -0,0 +1,12 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * The number of `LIBGAMMA_CRTC_INFO_*` values defined in + * the version of the library the program is linked against + * + * This exclude the combining macros defined below the + * `LIBGAMMA_CRTC_INFO_COUNT` macro + */ +const int libgamma_crtc_info_count = LIBGAMMA_CRTC_INFO_COUNT; diff --git a/libgamma_crtc_information_destroy.c b/libgamma_crtc_information_destroy.c new file mode 100644 index 0000000..0789451 --- /dev/null +++ b/libgamma_crtc_information_destroy.c @@ -0,0 +1,15 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Release all resources in an information data structure for a CRTC + * + * @param this The CRTC information + */ +void +libgamma_crtc_information_destroy(libgamma_crtc_information_t *restrict this) +{ + free(this->edid); + free(this->connector_name); +} diff --git a/libgamma_crtc_information_free.c b/libgamma_crtc_information_free.c new file mode 100644 index 0000000..84048f4 --- /dev/null +++ b/libgamma_crtc_information_free.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Release all resources in an information data structure for a CRTC + * and free the data structure pointer + * + * @param this The CRTC information + */ +extern inline void libgamma_crtc_information_free(libgamma_crtc_information_t *restrict); diff --git a/libgamma_crtc_initialise.c b/libgamma_crtc_initialise.c new file mode 100644 index 0000000..0d010b7 --- /dev/null +++ b/libgamma_crtc_initialise.c @@ -0,0 +1,29 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Initialise an allocated CRTC state + * + * @param this The CRTC state to initialise + * @param partition The partition state for the partition that the CRTC belongs to + * @param crtc The index of the CRTC within the partition + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_crtc_initialise(libgamma_crtc_state_t *restrict this, libgamma_partition_state_t *restrict partition, size_t crtc) +{ + this->partition = partition; + this->crtc = crtc; + + switch (partition->site->method) { +#define X(CONST, CNAME, DEPTH, RAMPS)\ + case CONST:\ + return libgamma_##CNAME##_crtc_initialise(this, partition, crtc); + LIST_AVAILABLE_METHODS(X) +#undef X + default: + return LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD; + } +} diff --git a/libgamma_crtc_restore.c b/libgamma_crtc_restore.c new file mode 100644 index 0000000..d2a716f --- /dev/null +++ b/libgamma_crtc_restore.c @@ -0,0 +1,24 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Restore the gamma ramps for a CRTC to the system settings for that CRTC + * + * @param this The CRTC state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_crtc_restore(libgamma_crtc_state_t *restrict this) +{ + switch (this->partition->site->method) { +#define X(CONST, CNAME, DEPTH, RAMPS)\ + case CONST:\ + return libgamma_##CNAME##_crtc_restore(this); + LIST_AVAILABLE_METHODS(X) +#undef X + default: + return LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD; + } +} diff --git a/libgamma_crtc_set_gamma_ramps16.c b/libgamma_crtc_set_gamma_ramps16.c new file mode 100644 index 0000000..2ad0b03 --- /dev/null +++ b/libgamma_crtc_set_gamma_ramps16.c @@ -0,0 +1,21 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Set the gamma ramps for a CRTC, 16-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t ramps) +{ +#define DEPTH 16 +#define RAMPS ramps16 +#define TYPE bits16 +#define APPEND_RAMPS(X) X##ramps16 +#include "set_ramps.h" +} diff --git a/libgamma_crtc_set_gamma_ramps16_f.c b/libgamma_crtc_set_gamma_ramps16_f.c new file mode 100644 index 0000000..7b12530 --- /dev/null +++ b/libgamma_crtc_set_gamma_ramps16_f.c @@ -0,0 +1,25 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Set the gamma ramps for a CRTC, 16-bit gamma-depth function version + * + * Note that this will probably involve the library allocating temporary data + * + * @param this The CRTC state + * @param red_function The function that generates the gamma ramp for the red channel + * @param green_function The function that generates the gamma ramp for the green channel + * @param blue_function The function that generates the gamma ramp for the blue channel + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +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) +{ +#define TYPE uint16_t +#define RAMPS ramps16 +#define APPEND_RAMPS(X) X##ramps16 +#include "set_ramps_fun.h" +} diff --git a/libgamma_crtc_set_gamma_ramps32.c b/libgamma_crtc_set_gamma_ramps32.c new file mode 100644 index 0000000..f042c68 --- /dev/null +++ b/libgamma_crtc_set_gamma_ramps32.c @@ -0,0 +1,21 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Set the gamma ramps for a CRTC, 32-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_crtc_set_gamma_ramps32(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps32_t ramps) +{ +#define DEPTH 32 +#define RAMPS ramps32 +#define TYPE bits32 +#define APPEND_RAMPS(X) X##ramps32 +#include "set_ramps.h" +} diff --git a/libgamma_crtc_set_gamma_ramps32_f.c b/libgamma_crtc_set_gamma_ramps32_f.c new file mode 100644 index 0000000..d0bfef1 --- /dev/null +++ b/libgamma_crtc_set_gamma_ramps32_f.c @@ -0,0 +1,25 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Set the gamma ramps for a CRTC, 32-bit gamma-depth function version + * + * Note that this will probably involve the library allocating temporary data + * + * @param this The CRTC state + * @param red_function The function that generates the gamma ramp for the red channel + * @param green_function The function that generates the gamma ramp for the green channel + * @param blue_function The function that generates the gamma ramp for the blue channel + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +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) +{ +#define TYPE uint32_t +#define RAMPS ramps32 +#define APPEND_RAMPS(X) X##ramps32 +#include "set_ramps_fun.h" +} diff --git a/libgamma_crtc_set_gamma_ramps64.c b/libgamma_crtc_set_gamma_ramps64.c new file mode 100644 index 0000000..4314738 --- /dev/null +++ b/libgamma_crtc_set_gamma_ramps64.c @@ -0,0 +1,21 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Set the gamma ramps for a CRTC, 64-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_crtc_set_gamma_ramps64(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps64_t ramps) +{ +#define DEPTH 64 +#define RAMPS ramps64 +#define TYPE bits64 +#define APPEND_RAMPS(X) X##ramps64 +#include "set_ramps.h" +} diff --git a/libgamma_crtc_set_gamma_ramps64_f.c b/libgamma_crtc_set_gamma_ramps64_f.c new file mode 100644 index 0000000..3a0417d --- /dev/null +++ b/libgamma_crtc_set_gamma_ramps64_f.c @@ -0,0 +1,25 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Set the gamma ramps for a CRTC, 64-bit gamma-depth function version + * + * Note that this will probably involve the library allocating temporary data + * + * @param this The CRTC state + * @param red_function The function that generates the gamma ramp for the red channel + * @param green_function The function that generates the gamma ramp for the green channel + * @param blue_function The function that generates the gamma ramp for the blue channel + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +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) +{ +#define TYPE uint64_t +#define RAMPS ramps64 +#define APPEND_RAMPS(X) X##ramps64 +#include "set_ramps_fun.h" +} diff --git a/libgamma_crtc_set_gamma_ramps8.c b/libgamma_crtc_set_gamma_ramps8.c new file mode 100644 index 0000000..b0f041b --- /dev/null +++ b/libgamma_crtc_set_gamma_ramps8.c @@ -0,0 +1,21 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Set the gamma ramps for a CRTC, 8-bit gamma-depth version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_crtc_set_gamma_ramps8(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps8_t ramps) +{ +#define DEPTH 8 +#define RAMPS ramps8 +#define TYPE bits8 +#define APPEND_RAMPS(X) X##ramps8 +#include "set_ramps.h" +} diff --git a/libgamma_crtc_set_gamma_ramps8_f.c b/libgamma_crtc_set_gamma_ramps8_f.c new file mode 100644 index 0000000..f33d960 --- /dev/null +++ b/libgamma_crtc_set_gamma_ramps8_f.c @@ -0,0 +1,25 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Set the gamma ramps for a CRTC, 8-bit gamma-depth function version + * + * Note that this will probably involve the library allocating temporary data + * + * @param this The CRTC state + * @param red_function The function that generates the gamma ramp for the red channel + * @param green_function The function that generates the gamma ramp for the green channel + * @param blue_function The function that generates the gamma ramp for the blue channel + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +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) +{ +#define TYPE uint8_t +#define RAMPS ramps8 +#define APPEND_RAMPS(X) X##ramps8 +#include "set_ramps_fun.h" +} diff --git a/libgamma_crtc_set_gamma_rampsd.c b/libgamma_crtc_set_gamma_rampsd.c new file mode 100644 index 0000000..7d02bdb --- /dev/null +++ b/libgamma_crtc_set_gamma_rampsd.c @@ -0,0 +1,21 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Set the gamma ramps for a CRTC, `double` version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_crtc_set_gamma_rampsd(libgamma_crtc_state_t *restrict this, libgamma_gamma_rampsd_t ramps) +{ +#define DEPTH -2 +#define RAMPS rampsd +#define TYPE float_double +#define APPEND_RAMPS(X) X##rampsd +#include "set_ramps.h" +} diff --git a/libgamma_crtc_set_gamma_rampsd_f.c b/libgamma_crtc_set_gamma_rampsd_f.c new file mode 100644 index 0000000..7154b60 --- /dev/null +++ b/libgamma_crtc_set_gamma_rampsd_f.c @@ -0,0 +1,25 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Set the gamma ramps for a CRTC, `double` function version + * + * Note that this will probably involve the library allocating temporary data + * + * @param this The CRTC state + * @param red_function The function that generates the gamma ramp for the red channel + * @param green_function The function that generates the gamma ramp for the green channel + * @param blue_function The function that generates the gamma ramp for the blue channel + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +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) +{ +#define TYPE double +#define RAMPS rampsd +#define APPEND_RAMPS(X) X##rampsd +#include "set_ramps_fun.h" +} diff --git a/libgamma_crtc_set_gamma_rampsf.c b/libgamma_crtc_set_gamma_rampsf.c new file mode 100644 index 0000000..a31a007 --- /dev/null +++ b/libgamma_crtc_set_gamma_rampsf.c @@ -0,0 +1,21 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Set the gamma ramps for a CRTC, `float` version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_crtc_set_gamma_rampsf(libgamma_crtc_state_t *restrict this, libgamma_gamma_rampsf_t ramps) +{ +#define DEPTH -1 +#define RAMPS rampsf +#define TYPE float_single +#define APPEND_RAMPS(X) X##rampsf +#include "set_ramps.h" +} diff --git a/libgamma_crtc_set_gamma_rampsf_f.c b/libgamma_crtc_set_gamma_rampsf_f.c new file mode 100644 index 0000000..a5a639f --- /dev/null +++ b/libgamma_crtc_set_gamma_rampsf_f.c @@ -0,0 +1,25 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Set the gamma ramps for a CRTC, `float` function version + * + * Note that this will probably involve the library allocating temporary data + * + * @param this The CRTC state + * @param red_function The function that generates the gamma ramp for the red channel + * @param green_function The function that generates the gamma ramp for the green channel + * @param blue_function The function that generates the gamma ramp for the blue channel + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +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) +{ +#define TYPE float +#define RAMPS rampsf +#define APPEND_RAMPS(X) X##rampsf +#include "set_ramps_fun.h" +} diff --git a/libgamma_error_min.c b/libgamma_error_min.c new file mode 100644 index 0000000..199429c --- /dev/null +++ b/libgamma_error_min.c @@ -0,0 +1,10 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * The number of the libgamma error with the + * lowest number in the version of the library + * that the program is linked against + */ +const int libgamma_error_min = LIBGAMMA_ERROR_MIN; diff --git a/libgamma_gamma_ramps16_destroy.c b/libgamma_gamma_ramps16_destroy.c new file mode 100644 index 0000000..09a00f1 --- /dev/null +++ b/libgamma_gamma_ramps16_destroy.c @@ -0,0 +1,16 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_ramps_initialise` or otherwise + * initialises in the proper manner + * + * @param this The gamma ramps + */ +void +libgamma_gamma_ramps16_destroy(libgamma_gamma_ramps16_t *restrict this) +{ + free(this->red); +} diff --git a/libgamma_gamma_ramps16_free.c b/libgamma_gamma_ramps16_free.c new file mode 100644 index 0000000..70b9368 --- /dev/null +++ b/libgamma_gamma_ramps16_free.c @@ -0,0 +1,18 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_ramps_initialise` or otherwise + * initialises in the proper manner, as well as release the pointer + * to the structure + * + * @param this The gamma ramps + */ +void +libgamma_gamma_ramps16_free(libgamma_gamma_ramps16_t *restrict this) +{ + free(this->red); + free(this); +} diff --git a/libgamma_gamma_ramps16_initialise.c b/libgamma_gamma_ramps16_initialise.c new file mode 100644 index 0000000..c1c55cb --- /dev/null +++ b/libgamma_gamma_ramps16_initialise.c @@ -0,0 +1,28 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Initialise a gamma ramp in the proper way that allows all adjustment + * methods to read from and write to it without causing segmentation violation + * + * The input must have `red_size`, `green_size`, and `blue_size` set to the + * sizes of the gamma ramps that should be allocated + * + * @param this The gamma ramps + * @return Zero on success, -1 on allocation error, `errno` will be set accordingly + */ +int +libgamma_gamma_ramps16_initialise(libgamma_gamma_ramps16_t *restrict this) +{ + size_t n = this->red_size + this->green_size + this->blue_size; +#ifdef HAVE_LIBGAMMA_METHOD_LINUX_DRM + /* Valgrind complains about us reading uninitialize memory if we just use malloc */ + this->red = calloc(n, sizeof(uint16_t)); +#else + this->red = malloc(n * sizeof(uint16_t)); +#endif + this->green = &this-> red[this-> red_size]; + this->blue = &this->green[this->green_size]; + return this->red ? 0 : -1; +} diff --git a/libgamma_gamma_ramps32_destroy.c b/libgamma_gamma_ramps32_destroy.c new file mode 100644 index 0000000..4eabb6a --- /dev/null +++ b/libgamma_gamma_ramps32_destroy.c @@ -0,0 +1,16 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_ramps32_initialise` or otherwise + * initialises in the proper manner + * + * @param this The gamma ramps + */ +void +libgamma_gamma_ramps32_destroy(libgamma_gamma_ramps32_t *restrict this) +{ + free(this->red); +} diff --git a/libgamma_gamma_ramps32_free.c b/libgamma_gamma_ramps32_free.c new file mode 100644 index 0000000..96f7909 --- /dev/null +++ b/libgamma_gamma_ramps32_free.c @@ -0,0 +1,18 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_ramps32_initialise` or otherwise + * initialises in the proper manner, as well as release the pointer + * to the structure + * + * @param this The gamma ramps + */ +void +libgamma_gamma_ramps32_free(libgamma_gamma_ramps32_t *restrict this) +{ + free(this->red); + free(this); +} diff --git a/libgamma_gamma_ramps32_initialise.c b/libgamma_gamma_ramps32_initialise.c new file mode 100644 index 0000000..28c1a00 --- /dev/null +++ b/libgamma_gamma_ramps32_initialise.c @@ -0,0 +1,23 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Initialise a gamma ramp in the proper way that allows all adjustment + * methods to read from and write to it without causing segmentation violation + * + * The input must have `red_size`, `green_size`, and `blue_size` set to the + * sizes of the gamma ramps that should be allocated + * + * @param this The gamma ramps + * @return Zero on success, -1 on allocation error, `errno` will be set accordingly + */ +int +libgamma_gamma_ramps32_initialise(libgamma_gamma_ramps32_t *restrict this) +{ + size_t n = this->red_size + this->green_size + this->blue_size; + this->red = malloc(n * sizeof(uint32_t)); + this->green = &this-> red[this-> red_size]; + this->blue = &this->green[this->green_size]; + return this->red ? 0 : -1; +} diff --git a/libgamma_gamma_ramps64_destroy.c b/libgamma_gamma_ramps64_destroy.c new file mode 100644 index 0000000..aeb85c4 --- /dev/null +++ b/libgamma_gamma_ramps64_destroy.c @@ -0,0 +1,16 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_ramps64_initialise` or otherwise + * initialises in the proper manner + * + * @param this The gamma ramps + */ +void +libgamma_gamma_ramps64_destroy(libgamma_gamma_ramps64_t *restrict this) +{ + free(this->red); +} diff --git a/libgamma_gamma_ramps64_free.c b/libgamma_gamma_ramps64_free.c new file mode 100644 index 0000000..493d8be --- /dev/null +++ b/libgamma_gamma_ramps64_free.c @@ -0,0 +1,18 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_ramps64_initialise` or otherwise + * initialises in the proper manner, as well as release the pointer + * to the structure + * + * @param this The gamma ramps + */ +void +libgamma_gamma_ramps64_free(libgamma_gamma_ramps64_t *restrict this) +{ + free(this->red); + free(this); +} diff --git a/libgamma_gamma_ramps64_initialise.c b/libgamma_gamma_ramps64_initialise.c new file mode 100644 index 0000000..1196670 --- /dev/null +++ b/libgamma_gamma_ramps64_initialise.c @@ -0,0 +1,23 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Initialise a gamma ramp in the proper way that allows all adjustment + * methods to read from and write to it without causing segmentation violation + * + * The input must have `red_size`, `green_size`, and `blue_size` set to the + * sizes of the gamma ramps that should be allocated + * + * @param this The gamma ramps + * @return Zero on success, -1 on allocation error, `errno` will be set accordingly + */ +int +libgamma_gamma_ramps64_initialise(libgamma_gamma_ramps64_t *restrict this) +{ + size_t n = this->red_size + this->green_size + this->blue_size; + this->red = malloc(n * sizeof(uint64_t)); + this->green = &this-> red[this-> red_size]; + this->blue = &this->green[this->green_size]; + return this->red ? 0 : -1; +} diff --git a/libgamma_gamma_ramps8_destroy.c b/libgamma_gamma_ramps8_destroy.c new file mode 100644 index 0000000..630d8cc --- /dev/null +++ b/libgamma_gamma_ramps8_destroy.c @@ -0,0 +1,16 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_ramps8_initialise` or otherwise + * initialises in the proper manner + * + * @param this The gamma ramps + */ +void +libgamma_gamma_ramps8_destroy(libgamma_gamma_ramps8_t *restrict this) +{ + free(this->red); +} diff --git a/libgamma_gamma_ramps8_free.c b/libgamma_gamma_ramps8_free.c new file mode 100644 index 0000000..78be13a --- /dev/null +++ b/libgamma_gamma_ramps8_free.c @@ -0,0 +1,18 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_ramps8_initialise` or otherwise + * initialises in the proper manner, as well as release the pointer + * to the structure + * + * @param this The gamma ramps + */ +void +libgamma_gamma_ramps8_free(libgamma_gamma_ramps8_t *restrict this) +{ + free(this->red); + free(this); +} diff --git a/libgamma_gamma_ramps8_initialise.c b/libgamma_gamma_ramps8_initialise.c new file mode 100644 index 0000000..6c64614 --- /dev/null +++ b/libgamma_gamma_ramps8_initialise.c @@ -0,0 +1,23 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Initialise a gamma ramp in the proper way that allows all adjustment + * methods to read from and write to it without causing segmentation violation + * + * The input must have `red_size`, `green_size`, and `blue_size` set to the + * sizes of the gamma ramps that should be allocated + * + * @param this The gamma ramps + * @return Zero on success, -1 on allocation error, `errno` will be set accordingly + */ +int +libgamma_gamma_ramps8_initialise(libgamma_gamma_ramps8_t *restrict this) +{ + size_t n = this->red_size + this->green_size + this->blue_size; + this->red = malloc(n * sizeof(uint8_t)); + this->green = &this-> red[this-> red_size]; + this->blue = &this->green[this->green_size]; + return this->red ? 0 : -1; +} diff --git a/libgamma_gamma_rampsd_destroy.c b/libgamma_gamma_rampsd_destroy.c new file mode 100644 index 0000000..47e27f3 --- /dev/null +++ b/libgamma_gamma_rampsd_destroy.c @@ -0,0 +1,16 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_rampsd_initialise` or otherwise + * initialises in the proper manner + * + * @param this The gamma ramps + */ +void +libgamma_gamma_rampsd_destroy(libgamma_gamma_rampsd_t *restrict this) +{ + free(this->red); +} diff --git a/libgamma_gamma_rampsd_free.c b/libgamma_gamma_rampsd_free.c new file mode 100644 index 0000000..c9b18bc --- /dev/null +++ b/libgamma_gamma_rampsd_free.c @@ -0,0 +1,18 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_rampsd_initialise` or otherwise + * initialises in the proper manner, as well as release the pointer + * to the structure + * + * @param this The gamma ramps + */ +void +libgamma_gamma_rampsd_free(libgamma_gamma_rampsd_t *restrict this) +{ + free(this->red); + free(this); +} diff --git a/libgamma_gamma_rampsd_initialise.c b/libgamma_gamma_rampsd_initialise.c new file mode 100644 index 0000000..bb98d0b --- /dev/null +++ b/libgamma_gamma_rampsd_initialise.c @@ -0,0 +1,23 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Initialise a gamma ramp in the proper way that allows all adjustment + * methods to read from and write to it without causing segmentation violation + * + * The input must have `red_size`, `green_size`, and `blue_size` set to the + * sizes of the gamma ramps that should be allocated + * + * @param this The gamma ramps + * @return Zero on success, -1 on allocation error, `errno` will be set accordingly + */ +int +libgamma_gamma_rampsd_initialise(libgamma_gamma_rampsd_t *restrict this) +{ + size_t n = this->red_size + this->green_size + this->blue_size; + this->red = malloc(n * sizeof(double)); + this->green = &this-> red[this-> red_size]; + this->blue = &this->green[this->green_size]; + return this->red ? 0 : -1; +} diff --git a/libgamma_gamma_rampsf_destroy.c b/libgamma_gamma_rampsf_destroy.c new file mode 100644 index 0000000..0b184d7 --- /dev/null +++ b/libgamma_gamma_rampsf_destroy.c @@ -0,0 +1,16 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_rampsf_initialise` or otherwise + * initialises in the proper manner + * + * @param this The gamma ramps + */ +void +libgamma_gamma_rampsf_destroy(libgamma_gamma_rampsf_t *restrict this) +{ + free(this->red); +} diff --git a/libgamma_gamma_rampsf_free.c b/libgamma_gamma_rampsf_free.c new file mode 100644 index 0000000..fdaf590 --- /dev/null +++ b/libgamma_gamma_rampsf_free.c @@ -0,0 +1,18 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Release resources that are held by a gamma ramp strcuture that + * has been allocated by `libgamma_gamma_rampsf_initialise` or otherwise + * initialises in the proper manner, as well as release the pointer + * to the structure + * + * @param this The gamma ramps + */ +void +libgamma_gamma_rampsf_free(libgamma_gamma_rampsf_t *restrict this) +{ + free(this->red); + free(this); +} diff --git a/libgamma_gamma_rampsf_initialise.c b/libgamma_gamma_rampsf_initialise.c new file mode 100644 index 0000000..b77fc83 --- /dev/null +++ b/libgamma_gamma_rampsf_initialise.c @@ -0,0 +1,23 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Initialise a gamma ramp in the proper way that allows all adjustment + * methods to read from and write to it without causing segmentation violation + * + * The input must have `red_size`, `green_size`, and `blue_size` set to the + * sizes of the gamma ramps that should be allocated + * + * @param this The gamma ramps + * @return Zero on success, -1 on allocation error, `errno` will be set accordingly + */ +int +libgamma_gamma_rampsf_initialise(libgamma_gamma_rampsf_t *restrict this) +{ + size_t n = this->red_size + this->green_size + this->blue_size; + this->red = malloc(n * sizeof(float)); + this->green = &this-> red[this-> red_size]; + this->blue = &this->green[this->green_size]; + return this->red ? 0 : -1; +} diff --git a/libgamma_get_crtc_information.c b/libgamma_get_crtc_information.c new file mode 100644 index 0000000..023c07f --- /dev/null +++ b/libgamma_get_crtc_information.c @@ -0,0 +1,29 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + + +/** + * Read information about a CRTC + * + * @param this Instance of a data structure to fill with the information about the CRTC + * @param crtc The state of the CRTC whose information should be read + * @param fields OR:ed identifiers for the information about the CRTC that should be read + * @return Zero on success, -1 on error; on error refer to the error reports in `this` + */ +int +libgamma_get_crtc_information(libgamma_crtc_information_t *restrict this, libgamma_crtc_state_t *restrict crtc, int32_t fields) +{ + this->edid = NULL; + this->connector_name = NULL; + + switch (crtc->partition->site->method) { +#define X(CONST, CNAME, DEPTH, RAMPS)\ + case CONST:\ + return libgamma_##CNAME##_get_crtc_information(this, crtc, fields); + LIST_AVAILABLE_METHODS(X) +#undef X + default: + return LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD; + } +} diff --git a/libgamma_group_gid.c b/libgamma_group_gid.c new file mode 100644 index 0000000..b493a0d --- /dev/null +++ b/libgamma_group_gid.c @@ -0,0 +1,37 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + +#ifdef __WIN32__ +#define gid_t short +#endif + + +static _Thread_local gid_t libgamma_group_gid; + + +/** + * Get the group that the user needs to be a member + * of if `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned + * + * @return The group that the user needs to be a member of + * if `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned + */ +gid_t +libgamma_group_gid_get(void) +{ + return libgamma_group_gid; +} + + +/** + * Set the group that the user needs to be a member + * of if `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned + * + * @param value The group that the user needs to be a member of + * if `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned + */ +void +libgamma_group_gid_set(gid_t value) +{ + libgamma_group_gid = value; +} diff --git a/libgamma_group_name.c b/libgamma_group_name.c new file mode 100644 index 0000000..ce59300 --- /dev/null +++ b/libgamma_group_name.c @@ -0,0 +1,44 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +static _Thread_local char group_name[64]; /* Group names can only be up to 16 bytes logn, 63 is a very safe number */ + + +/** + * Get the group that the user needs to be a member of + * if `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned + * + * @return The 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 + */ +const char * +libgamma_group_name_get(void) +{ + return *group_name ? group_name : NULL; +} + + +/** + * Set the group that the user needs to be a member of + * if `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned + * + * @param value The group that the user needs to be a member of if + * `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned, may be `NULL` + */ +void +libgamma_group_name_set(const char *value) +{ + size_t n; + if (!value) { + *group_name = 0; + } else { + n = strlen(value); + if (n >= sizeof(group_name)) + *group_name = 0; + else + memcpy(group_name, value, n + 1); + } +} diff --git a/libgamma_internal_allocated_any_ramp.c b/libgamma_internal_allocated_any_ramp.c new file mode 100644 index 0000000..7e6953d --- /dev/null +++ b/libgamma_internal_allocated_any_ramp.c @@ -0,0 +1,60 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Just an arbitrary version + */ +#define ANY bits64 + +/** + * Concatenation of all ramps + */ +#define ALL red + + +/** + * Allocate and initalise a gamma ramp with any depth + * + * @param ramps_sys Output gamma ramps + * @param ramps The gamma ramps whose sizes should be duplicated + * @param depth The depth of the gamma ramps to allocate, + * `-1` for `float`, `-2` for `double` + * @param elements Output reference for the grand size of the gamma ramps + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_internal_allocated_any_ramp(gamma_ramps_any_t *restrict ramps_sys, gamma_ramps_any_t ramps, + signed depth, size_t *restrict elements) +{ + /* Calculate the size of the allocation to do */ + size_t d, n = ramps.ANY.red_size + ramps.ANY.green_size + ramps.ANY.blue_size; + switch (depth) { + case 8: d = sizeof(uint8_t); break; + case 16: d = sizeof(uint16_t); break; + case 32: d = sizeof(uint32_t); break; + case 64: d = sizeof(uint64_t); break; + case -1: d = sizeof(float); break; + case -2: d = sizeof(double); break; + default: + return errno = EINVAL, LIBGAMMA_ERRNO_SET; + } + + /* Copy the gamma ramp sizes */ + ramps_sys->ANY = ramps.ANY; + /* Allocate the new ramps */ +#ifdef HAVE_LIBGAMMA_METHOD_LINUX_DRM + /* Valgrind complains about us reading uninitialize memory if we just use malloc */ + ramps_sys->ANY.red = calloc(n, d); +#else + ramps_sys->ANY.red = malloc(n * d); +#endif + ramps_sys->ANY.green = (void *)&((char *)ramps_sys->ANY. red)[ramps.ANY. red_size * d / sizeof(char)]; + ramps_sys->ANY.blue = (void *)&((char *)ramps_sys->ANY.green)[ramps.ANY.green_size * d / sizeof(char)]; + + /* Report the total gamma ramp size */ + *elements = n; + /* Report successfulness */ + return ramps_sys->ANY.red ? 0 : LIBGAMMA_ERRNO_SET; +} diff --git a/libgamma_internal_parse_edid.c b/libgamma_internal_parse_edid.c new file mode 100644 index 0000000..aa1b87c --- /dev/null +++ b/libgamma_internal_parse_edid.c @@ -0,0 +1,99 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/* + * EDID structure revision 1.1: + * http://en.wikipedia.org/w/index.php?title=Extended_display_identification_data&oldid=46295569 + * + * EDID structure revision 1.3: + * http://en.wikipedia.org/wiki/Extended_display_identification_data + */ + + +/** + * Parse the EDID of a monitor + * + * @param this Instance of a data structure to fill with the information about the EDID; + * it must contain the EDID and its length + * @param fields OR:ed identifiers for the information about the EDID that should be parsed; + * fields that do not have to do with EDID are ignored + * @return Non-zero on error + */ +int +libgamma_internal_parse_edid(libgamma_crtc_information_t *restrict this, int32_t fields) +{ +#define __test_version(edid, major, minor_min, minor_max)\ + ((edid)[18] == (major) && (minor_min) <= (edid)[19] && (edid)[19] <= (minor_max)) +#define __m(value)\ + (this->edid[index++] != (value)) + + int error = 0, checksum = 0; + size_t i, index = 0; + + /* If the length of the EDID is not 128 bytes, we know that it is not of EDID + structure revision 1.0–1.3, and thus we do not support it. Additionally + this make sure we do not do segmentation violation on the next test. */ + if (this->edid_length != 128) + error = LIBGAMMA_EDID_LENGTH_UNSUPPORTED; + /* Check that the magic number of that of the EDID structure. */ + else if (__m(0x00) || __m(0xFF) || __m(0xFF) || __m(0xFF) || __m(0xFF) || __m(0xFF) || __m(0xFF) || __m(0x00)) + error = LIBGAMMA_EDID_WRONG_MAGIC_NUMBER; + /* Check that EDID structure revision 1.1–1.3 is used, those are the only + version we support. EDID structure revision 1.3 is also by far the most + commonly use revision and it is currently the newest revision. We know + that parsing works for both revision 1.1 and revision 1.3, because of + this we assume it is also correct for revision 1.2. However, we are not + assuming this for revision 1.0 which appeared in August 1994 and was + replaced by revision 1.1 in April 1996. */ + else if (!__test_version(this->edid, 1, 1, 3)) + error = LIBGAMMA_EDID_REVISION_UNSUPPORTED; + + /* If we have encountered an error, report it for the fields that require + the EDID to be parsed. Note that it is not stored for the EDID field + itself because it is not considered an error just because we do not + support the used version. */ + this->width_mm_edid_error = this->height_mm_edid_error = this->gamma_error = error; + + /* Retrieve the size of the viewport. This is done even if it is not + requested because it is not worth it branch. */ + this->width_mm_edid = (size_t)this->edid[21] * 10; + this->height_mm_edid = (size_t)this->edid[22] * 10; + + /* Retrieve the monitor's gamma characteristics. */ + if ((fields & LIBGAMMA_CRTC_INFO_GAMMA) && !error) { + if (this->edid[23] == 0xFF) { + /* If the gamma charactistics is FFh (3,55) it should be interpreted as not specified. */ + this->gamma_error = LIBGAMMA_GAMMA_NOT_SPECIFIED; + } else { + this->gamma_blue = (float)((int)this->edid[23] + 100) / 100.f; + this->gamma_red = this->gamma_green = this->gamma_blue; + } + } + + /* If not error has occurred, calculate and test the checksum. + It is not considered an error that the gamma characteristics + is left unspecified in the EDID. */ + if (!error) + for (i = 0; i < this->edid_length; i++) + checksum += (int)this->edid[i]; + /* The checksum should be zero. */ + if (checksum & 255) { + /* Store the error in all fields that require the EDID to be parsed, + as well as the EDID field itself. */ + error = LIBGAMMA_EDID_CHECKSUM_ERROR; + this->edid_error = this->width_mm_edid_error = this->height_mm_edid_error = error; + /* If the gamma characteristics is not specified, that is kept, + and the checksum error is augmented. */ + this->gamma_error = this->gamma_error == LIBGAMMA_GAMMA_NOT_SPECIFIED + ? LIBGAMMA_GAMMA_NOT_SPECIFIED_AND_EDID_CHECKSUM_ERROR : error; + } + + /* Return whether or not we encountered an error or if + the gamma characteristics was requested but is not + specified in the monitor's EDID. */ + return error | this->gamma_error; + +#undef __m +#undef __test_version +} diff --git a/libgamma_internal_translate_from_64.c b/libgamma_internal_translate_from_64.c new file mode 100644 index 0000000..b8adbfe --- /dev/null +++ b/libgamma_internal_translate_from_64.c @@ -0,0 +1,44 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Concatenation of all ramps + */ +#define ALL red + +/** + * Preform installation in an `for (i = 0; i < n; i++)` + * loop and do a `break` afterwords + */ +#define __translate(instruction) for (i = 0; i < n; i++) instruction; break + + +/** + * Undo the actions of `libgamma_internal_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 + */ +void +libgamma_internal_translate_from_64(signed depth, size_t n, 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; + } +} diff --git a/libgamma_internal_translate_to_64.c b/libgamma_internal_translate_to_64.c new file mode 100644 index 0000000..3733186 --- /dev/null +++ b/libgamma_internal_translate_to_64.c @@ -0,0 +1,184 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Just an arbitrary version + */ +#define ANY bits64 + +/** + * Concatenation of all ramps + */ +#define ALL red + + +/** + * Preform installation in an `for (i = 0; i < n; i++)` + * loop and do a `break` afterwords + */ +#define __translate(instruction) for (i = 0; i < n; i++) instruction; break + + +/** + * Convert a [0, 1] `float` to a full range `uint64_t` + * and mark sure rounding errors does not cause the + * value be 0 instead of ~0 and vice versa + * + * @param value To `float` to convert + * @return The value as an `uint64_t` + */ +static uint64_t +float_to_64(float value) +{ + /* TODO Which is faster? */ + +#if defined(HAVE_INT128) && __WORDSIZE == 64 + /* `__int128` is a GNU C extension, which + (because it is not ISO C) emits a warning + under -pedantic */ +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wpedantic" + + /* In GCC we can use `__int128`, this is + a signed 128-bit integer. It fits all + uint64_t values but also native values, + which is a nice because it eleminates + some overflow condition tests. It is + also more readable. */ + + /* Convert to integer */ + __int128 product = (__int128)(value * (float)UINT64_MAX); + /* Negative overflow */ + if (product > UINT64_MAX) + return UINT64_MAX; + /* Positive overflow */ + if (product < 0) + return 0; + /* Did not overflow */ + return (uint64_t)product; + +# pragma GCC diagnostic pop +#else + + /* If we are not using GCC we cannot be + sure that we have `__int128` so we have + to use `uint64_t` and perform overflow + checkes based on the input value */ + + /* Convert to integer. */ + uint64_t product = (uint64_t)(value * (float)UINT64_MAX); + /* Negative overflow, + if the input is less than 0.5 but + the output is greater then we got + -1 when we should have gotten 0 */ + if (value < 0.1f && product > 0xF000000000000000ULL) + return 0; + /* Positive overflow, + if the input is greater than 0.5 + but the output is less then we got + 0 when we should have gotten ~0 */ + else if (value > 0.9f && product < 0x1000000000000000ULL) + return (uint64_t)~0; + /* Did not overflow */ + return product; + +#endif +} + + +/** + * Convert a [0, 1] `double` to a full range `uint64_t` + * and mark sure rounding errors does not cause the + * value be 0 instead of ~0 and vice versa + * + * @param value To `double` to convert + * @return The value as an `uint64_t` + */ +static uint64_t +double_to_64(double value) +{ + /* TODO Which is faster? */ + +#if defined(HAVE_INT128) && __WORDSIZE == 64 + /* `__int128` is a GNU C extension, which + (because it is not ISO C) emits a warning + under -pedantic */ +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wpedantic" + + /* In GCC we can use `__int128`, this is + a signed 128-bit integer. It fits all + uint64_t values but also native values, + which is a nice because it eleminates + some overflow condition tests. It is + also more readable. */ + + /* Convert to integer */ + __int128 product = (__int128)(value * (double)UINT64_MAX); + /* Negative overflow */ + if (product > UINT64_MAX) + return UINT64_MAX; + /* Positive overflow */ + if (product < 0) + return 0; + /* Did not overflow */ + return (uint64_t)product; + +# pragma GCC diagnostic pop +#else + + /* If we are not using GCC we cannot be + sure that we have `__int128` so we have + to use `uint64_t` and perform overflow + checkes based on the input value. */ + + /* Convert to integer. */ + uint64_t product = (uint64_t)(value * (double)UINT64_MAX); + /* Negative overflow, + if the input is less than 0.5 but + the output is greater then we got + -1 when we should have gotten 0 */ + if (value < (double)0.1f && product > 0xF000000000000000ULL) + product = 0; + /* Positive overflow, + if the input is greater than 0.5 + but the output is less then we got + 0 when we should have gotten ~0 */ + else if ((value > (double)0.9f) && (product < 0x1000000000000000ULL)) + product = (uint64_t)~0; + /* Did not overflow */ + return product; + +#endif +} + + +/** + * Convert any set of gamma ramps into a 64-bit integer array with all channels + * + * @param depth The depth of the gamma ramp, `-1` for `float`, `-2` for `double` + * @param n The grand size of gamma ramps (sum of all channels' sizes) + * @param out Output array + * @param in Input gamma ramps + */ +void +libgamma_internal_translate_to_64(signed depth, size_t n, uint64_t *restrict out, 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; + } +} diff --git a/libgamma_internal_translated_ramp_get_.c b/libgamma_internal_translated_ramp_get_.c new file mode 100644 index 0000000..1ea4bdc --- /dev/null +++ b/libgamma_internal_translated_ramp_get_.c @@ -0,0 +1,60 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Just an arbitrary version + */ +#define ANY bits64 + + +/** + * Get the current gamma ramps for a CRTC, re-encoding version + * + * @param this The CRTC state + * @param ramps The gamma ramps to fill with the current values + * @param depth_user The depth of the gamma ramps that are provided by the user, + * `-1` for `float`, `-2` for `double` + * @param depth_system The depth of the gamma ramps as required by the adjustment method, + * `-1` for `float`, `-2` for `double` + * @param fun Function that is to be used read the ramps, its parameters have + * the same function as those of this function with the same names, + * and the return value too is identical + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_internal_translated_ramp_get_(libgamma_crtc_state_t *restrict this, gamma_ramps_any_t *restrict ramps, + signed depth_user, signed depth_system, get_ramps_any_fun *fun) +{ + size_t n; + int r; + gamma_ramps_any_t ramps_sys; + uint64_t *restrict ramps_full; + + /* Allocate ramps with proper data type */ + if ((r = libgamma_internal_allocated_any_ramp(&ramps_sys, *ramps, depth_system, &n))) + return r; + + /* Fill the ramps */ + if ((r = fun(this, &ramps_sys))) { + free(ramps_sys.ANY.red); + 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 */ + libgamma_internal_translate_to_64(depth_system, n, ramps_full, ramps_sys); + free(ramps_sys.ANY.red); + + /* Translate ramps to the user's format */ + libgamma_internal_translate_from_64(depth_user, n, *ramps, ramps_full); + free(ramps_full); + return 0; +} diff --git a/libgamma_internal_translated_ramp_set_.c b/libgamma_internal_translated_ramp_set_.c new file mode 100644 index 0000000..4b4a9af --- /dev/null +++ b/libgamma_internal_translated_ramp_set_.c @@ -0,0 +1,57 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Just an arbitrary version + */ +#define ANY bits64 + + +/** + * Set the gamma ramps for a CRTC, re-encoding version + * + * @param this The CRTC state + * @param ramps The gamma ramps to apply + * @param depth_user The depth of the gamma ramps that are provided by the user, + * `-1` for `float`, `-2` for `double` + * @param depth_system The depth of the gamma ramps as required by the adjustment method, + * `-1` for `float`, `-2` for `double` + * @param fun Function that is to be used write the ramps, its parameters have + * the same function as those of this function with the same names, + * and the return value too is identical + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_internal_translated_ramp_set_(libgamma_crtc_state_t *restrict this, gamma_ramps_any_t ramps, + signed depth_user, signed depth_system, set_ramps_any_fun *fun) +{ + size_t n; + int r; + gamma_ramps_any_t ramps_sys; + uint64_t *restrict ramps_full; + + /* Allocate ramps with proper data type */ + if ((r = libgamma_internal_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. */ + libgamma_internal_translate_to_64(depth_user, n, ramps_full, ramps); + /* Translate ramps to the proper format. */ + libgamma_internal_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; +} diff --git a/libgamma_is_method_available.c b/libgamma_is_method_available.c new file mode 100644 index 0000000..c114295 --- /dev/null +++ b/libgamma_is_method_available.c @@ -0,0 +1,24 @@ +#include "common.h" + + +/** + * Check whether an adjustment method is available, + * non-existing (invalid) methods will be identified + * as not available under the rationale that the + * library may be out of date + * + * @param method The adjustment method + * @return Whether the adjustment method is available + */ +int +libgamma_is_method_available(int method) +{ + switch (method) { +#define X(CONST, NAME, CNAME, ENABLED)\ + case CONST: return ENABLED; + LIST_METHODS(X) +#undef X + default: + return 0; + } +} diff --git a/libgamma_list_methods.c b/libgamma_list_methods.c new file mode 100644 index 0000000..6b912e1 --- /dev/null +++ b/libgamma_list_methods.c @@ -0,0 +1,135 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Test whether a file descriptor refers to a VT + * + * @param fd The file descriptor + * @return Whether the file descriptor refers to a VT + */ +static int +is_vt_proper(int fd) +{ + char buf[32], digit0; + + /* Get TTY */ + if (ttyname_r(fd, buf, sizeof(buf) / sizeof(char))) + return 0; + + /* Validate TTY path */ + if (!strcmp(buf, "/dev/console")) + return 1; + if (strncmp(buf, "/dev/tty", sizeof("/dev/tty") - 1)) + return 0; + + /* Validate TTY name */ + digit0 = buf[sizeof("/dev/tty") - 1]; + return '1' <= digit0 && digit0 <= '9'; +} + + +/** + * Test the availability of an adjustment method + * + * @param method The adjustment method + * @param operation Allowed values: + * 0: Pass if the environment suggests it will work but is not fake + * 1: Pass if the environment suggests it will work + * 2: Pass if real and not fake + * 3: Pass if real + * 4: Always pass + * Other values invoke undefined behaviour + * @return Whether the test passed + */ +static int +list_method_test(int method, int operation) +{ + libgamma_method_capabilities_t caps; + int fd, r, saved_errno; + + 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) { + saved_errno = errno; + if (is_vt_proper(STDIN_FILENO)) { + r = 1; + } else if (is_vt_proper(STDOUT_FILENO)) { + r = 1; + } else if (is_vt_proper(STDERR_FILENO)) { + r = 1; + } else { + r = 0; + fd = open("/dev/tty", O_RDONLY); + if (fd >= 0) { + r = is_vt_proper(fd); + close(fd); + } + } + errno = saved_errno; + return r; + } +#endif +#ifdef HAVE_LIBGAMMA_METHOD_DUMMY + if (method == LIBGAMMA_METHOD_DUMMY) + return 0; +#endif + return caps.default_site_known; + + case 2: + /* All real non-fake methods */ + return caps.real && !caps.fake; + + case 3: + /* All real methods */ + return caps.real; + + default: + /* All methods */ + return 1; + } +} + + +/** + * List available adjustment methods by their order of preference based on the environment + * + * @param methods Output array of methods, should be able to hold `LIBGAMMA_METHOD_COUNT` elements + * @param buf_size The number of elements that fits in `methods`, it should be `LIBGAMMA_METHOD_COUNT`, + * This is used to avoid writing outside the output buffer if this library adds new + * adjustment methods without the users of the library recompiling + * @param operation Allowed values: + * 0: Methods that the environment suggests will work, excluding fake + * 1: Methods that the environment suggests will work, including fake + * 2: All real non-fake methods + * 3: All real methods + * 4: All methods + * Other values invoke undefined behaviour + * @return The number of element that have been stored in `methods`, or should + * have been stored if the buffer was large enough + */ +size_t +libgamma_list_methods(int *restrict methods, size_t buf_size, int operation) +{ + size_t n = 0; + +#define X(CONST, CNAME, DEPTH, RAMPS)\ + if (list_method_test(CONST, operation) && n++ < buf_size)\ + methods[n - 1] = CONST; + LIST_AVAILABLE_METHODS(X) +#undef X + + return n; +} diff --git a/libgamma_method_capabilities.c b/libgamma_method_capabilities.c new file mode 100644 index 0000000..34623af --- /dev/null +++ b/libgamma_method_capabilities.c @@ -0,0 +1,28 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * 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) + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_method_capabilities(libgamma_method_capabilities_t *restrict this, int method) +{ + memset(this, 0, sizeof(*this)); + + switch (method) { +#define X(CONST, CNAME, DEPTH, RAMPS)\ + case CONST:\ + libgamma_##CNAME##_method_capabilities(this);\ + return 0; + LIST_AVAILABLE_METHODS(X) +#undef X + default: + return LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD; + } +} diff --git a/libgamma_method_count.c b/libgamma_method_count.c new file mode 100644 index 0000000..34dd4fa --- /dev/null +++ b/libgamma_method_count.c @@ -0,0 +1,13 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * The number adjustment methods provided by the + * version this library the program is linked + * against + * + * This number includes both compile-time enabled + * and compile-time disabled adjustment methods + */ +const int libgamma_method_count = LIBGAMMA_METHOD_COUNT; diff --git a/libgamma_method_default_site.c b/libgamma_method_default_site.c new file mode 100644 index 0000000..5da2c72 --- /dev/null +++ b/libgamma_method_default_site.c @@ -0,0 +1,31 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Return the default site for an adjustment method + * + * @param method The adjustment method (display server and protocol) + * @return The default site, `NULL` if it cannot be determined or + * if multiple sites are not supported by the adjustment + * method + */ +const char * +libgamma_method_default_site(int method) +{ + const char *restrict var = libgamma_method_default_site_variable(method); + const char *restrict env; + + /* Return `NULL` if there is no 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; +} diff --git a/libgamma_method_default_site_variable.c b/libgamma_method_default_site_variable.c new file mode 100644 index 0000000..146041f --- /dev/null +++ b/libgamma_method_default_site_variable.c @@ -0,0 +1,25 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Return the default variable that determines + * the default site for an adjustment method + * + * @param method The adjustment method (display server and protocol) + * @return The environ variables that is used to determine the + * default site, `NULL` if there is none, that is, if + * the method does not support multiple sites + */ +const char * +libgamma_method_default_site_variable(int method) +{ + switch (method) { + case LIBGAMMA_METHOD_X_RANDR: + return "DISPLAY"; + case LIBGAMMA_METHOD_X_VIDMODE: + return "DISPLAY"; + default: + return NULL; + } +} diff --git a/libgamma_name_of_connector_type.c b/libgamma_name_of_connector_type.c new file mode 100644 index 0000000..2f9b377 --- /dev/null +++ b/libgamma_name_of_connector_type.c @@ -0,0 +1,28 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Get the name of a connector type, + * for example "VGA" for `LIBGAMMA_CONNECTOR_TYPE_VGA` + * + * "Unknown" is returned for `LIBGAMMA_CONNECTOR_TYPE_Unknown`, + * "TV" is returned for `LIBGAMMA_CONNECTOR_TYPE_TV`, + * "Virtual" is returned for `LIBGAMMA_CONNECTOR_TYPE_Virtual` + * + * @param connector The connector type + * @return The name connector type, `NULL` if not + * recognised (errno is not changed) + */ +const char * +libgamma_name_of_connector_type(int connector) +{ + switch (connector) { +#define X(CONST, NAME)\ + case CONST: return NAME; + LIST_CONNECTOR_TYPES(X) +#undef X + default: + return NULL; + } +} diff --git a/libgamma_name_of_error.c b/libgamma_name_of_error.c new file mode 100644 index 0000000..56de28a --- /dev/null +++ b/libgamma_name_of_error.c @@ -0,0 +1,23 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Get the name of the definition associated with a `libgamma` error code + * + * @param value The error code + * @return The name of the definition associated with the error code, + * `NULL` if the error code does not exist + */ +const char * +libgamma_name_of_error(int value) +{ + switch (value) { +#define X(NAME, DESC)\ + case NAME: return #NAME; + LIST_ERRORS(X) +#undef X + default: + return NULL; + } +} diff --git a/libgamma_name_of_method.c b/libgamma_name_of_method.c new file mode 100644 index 0000000..3e1843d --- /dev/null +++ b/libgamma_name_of_method.c @@ -0,0 +1,23 @@ +#include "common.h" + + +/** + * Get the name of an adjustment method, + * for example "randr" for `LIBGAMMA_METHOD_X_RANDR` + * + * @param method The adjustment method + * @return The name adjustment method, `NULL` if not + * recognised (errno is not changed) + */ +const char * +libgamma_name_of_method(int method) +{ + switch (method) { +#define X(CONST, NAME, CNAME, ENABLED)\ + case CONST: return #NAME; + LIST_METHODS(X) +#undef X + default: + return NULL; + } +} diff --git a/libgamma_name_of_subpixel_order.c b/libgamma_name_of_subpixel_order.c new file mode 100644 index 0000000..f8c0bf8 --- /dev/null +++ b/libgamma_name_of_subpixel_order.c @@ -0,0 +1,27 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Get the name of a subpixel order, for example + * "Horizontal RGB" for `LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_RGB` + * + * "Unknown" is returned for `LIBGAMMA_SUBPIXEL_ORDER_UNKNOWN`, + * "None" is returned for `LIBGAMMA_SUBPIXEL_ORDER_NONE` + * + * @param order The subpixel order + * @return The name subpixel order, `NULL` if not + * recognised (errno is not changed) + */ +const char * +libgamma_name_of_subpixel_order(int order) +{ + switch (order) { +#define X(CONST, NAME)\ + case CONST: return NAME; + LIST_SUBPIXEL_ORDERS(X) +#undef X + default: + return NULL; + } +} diff --git a/libgamma_partition_destroy.c b/libgamma_partition_destroy.c new file mode 100644 index 0000000..9709865 --- /dev/null +++ b/libgamma_partition_destroy.c @@ -0,0 +1,23 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Release all resources held by a partition state + * + * @param this The partition state + */ +void +libgamma_partition_destroy(libgamma_partition_state_t *restrict this) +{ + switch (this->site->method) { +#define X(CONST, CNAME, DEPTH, RAMPS)\ + case CONST:\ + libgamma_##CNAME##_partition_destroy(this);\ + break; + LIST_AVAILABLE_METHODS(X) +#undef X + default: + break; + } +} diff --git a/libgamma_partition_free.c b/libgamma_partition_free.c new file mode 100644 index 0000000..787d3f4 --- /dev/null +++ b/libgamma_partition_free.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Release all resources held by a partition state + * and free the partition state pointer + * + * @param this The partition state + */ +extern inline void libgamma_partition_free(libgamma_partition_state_t *restrict); diff --git a/libgamma_partition_initialise.c b/libgamma_partition_initialise.c new file mode 100644 index 0000000..80b5976 --- /dev/null +++ b/libgamma_partition_initialise.c @@ -0,0 +1,29 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Initialise an allocated partition state + * + * @param this The partition state to initialise + * @param site The site state for the site that the partition belongs to + * @param partition The index of the partition within the site + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_partition_initialise(libgamma_partition_state_t *restrict this, libgamma_site_state_t *restrict site, size_t partition) +{ + this->site = site; + this->partition = partition; + + switch (site->method) { +#define X(CONST, CNAME, DEPTH, RAMPS)\ + case CONST:\ + return libgamma_##CNAME##_partition_initialise(this, site, partition); + LIST_AVAILABLE_METHODS(X) +#undef X + default: + return LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD; + } +} diff --git a/libgamma_partition_restore.c b/libgamma_partition_restore.c new file mode 100644 index 0000000..0b20626 --- /dev/null +++ b/libgamma_partition_restore.c @@ -0,0 +1,24 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Restore the gamma ramps all CRTC:s with a partition to the system settings + * + * @param this The partition state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_partition_restore(libgamma_partition_state_t *restrict this) +{ + switch (this->site->method) { +#define X(CONST, CNAME, DEPTH, RAMPS)\ + case CONST:\ + return libgamma_##CNAME##_partition_restore(this); + LIST_AVAILABLE_METHODS(X) +#undef X + default: + return LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD; + } +} diff --git a/libgamma_perror.c b/libgamma_perror.c new file mode 100644 index 0000000..92060d8 --- /dev/null +++ b/libgamma_perror.c @@ -0,0 +1,40 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Prints an error to stderr in a `perror` fashion + * + * @param name The text to add at the beginning + * @param value The error code, may be an `errno` value + */ +void +libgamma_perror(const char *name, int error_code) +{ + const char *desc, *gname; + char buf[1024]; + long int gid; + + desc = libgamma_strerror_r(error_code, buf, sizeof(buf)); + + if (error_code == LIBGAMMA_DEVICE_REQUIRE_GROUP) { + gid = (intmax_t)libgamma_group_gid_get(); + gname = libgamma_group_name_get(); + if (!gname) { + if (name && *name) + fprintf(stderr, "%s: %s in group %ji\n", name, desc, gid); + else + fprintf(stderr, "%s in group %ji\n", desc, gid); + } else { + if (name && *name) + fprintf(stderr, "%s: %s in group %s (%ji)\n", name, desc, gname, gid); + else + fprintf(stderr, "%s in group %s (%ji)\n", desc, gname, gid); + } + } else { + if (name && *name) + fprintf(stderr, "%s: %s\n", name, desc); + else + fprintf(stderr, "%s\n", desc); + } +} diff --git a/libgamma_site_destroy.c b/libgamma_site_destroy.c new file mode 100644 index 0000000..c3e060a --- /dev/null +++ b/libgamma_site_destroy.c @@ -0,0 +1,24 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Release all resources held by a site state + * + * @param this The site state + */ +void +libgamma_site_destroy(libgamma_site_state_t *restrict this) +{ + switch (this->method) { +#define X(CONST, CNAME, DEPTH, RAMPS)\ + case CONST:\ + libgamma_##CNAME##_site_destroy(this);\ + break; + LIST_AVAILABLE_METHODS(X) +#undef X + default: + break; + } + free(this->site); +} diff --git a/libgamma_site_free.c b/libgamma_site_free.c new file mode 100644 index 0000000..142ce70 --- /dev/null +++ b/libgamma_site_free.c @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Release all resources held by a site state + * and free the site state pointer + * + * @param this The site state + */ +extern inline void libgamma_site_free(libgamma_site_state_t *restrict this); diff --git a/libgamma_site_initialise.c b/libgamma_site_initialise.c new file mode 100644 index 0000000..23acc2d --- /dev/null +++ b/libgamma_site_initialise.c @@ -0,0 +1,34 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Initialise an allocated site state + * + * @param this The site state to initialise + * @param method The adjustment method (display server and protocol) + * @param site The site identifier, unless it is `NULL` it must a + * `free`:able. Once the state is destroyed the library + * will attempt to free it. There you should not free + * it yourself, and it must not be a string constant + * or allocated on the stack. Note however that it will + * not be `free`:d if this function fails. + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_site_initialise(libgamma_site_state_t *restrict this, int method, char *restrict site) +{ + this->method = method; + this->site = site; + + switch (method) { +#define X(CONST, CNAME, DEPTH, RAMPS)\ + case CONST:\ + return libgamma_##CNAME##_site_initialise(this, site); + LIST_AVAILABLE_METHODS(X) +#undef X + default: + return LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD; + } +} diff --git a/libgamma_site_restore.c b/libgamma_site_restore.c new file mode 100644 index 0000000..b6c936a --- /dev/null +++ b/libgamma_site_restore.c @@ -0,0 +1,24 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Restore the gamma ramps all CRTC:s with a site to the system settings + * + * @param this The site state + * @return Zero on success, otherwise (negative) the value of an + * error identifier provided by this library + */ +int +libgamma_site_restore(libgamma_site_state_t *restrict this) +{ + switch (this->method) { +#define X(CONST, CNAME, DEPTH, RAMPS)\ + case CONST:\ + return libgamma_##CNAME##_site_restore(this); + LIST_AVAILABLE_METHODS(X) +#undef X + default: + return LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD; + } +} diff --git a/libgamma_strerror.c b/libgamma_strerror.c new file mode 100644 index 0000000..c252a49 --- /dev/null +++ b/libgamma_strerror.c @@ -0,0 +1,32 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Get a description of an error + * + * @param error_code The error code, may be an `errno` value, if + * `LIBGAMMA_ERRNO_SET`, the current value of `errno` + * will be used + * @return The description associated with the error code, + * `NULL` if the error code is not recognised + */ +const char * +libgamma_strerror(int error_code) +{ + const char *desc; + if (error_code == LIBGAMMA_ERRNO_SET) + error_code = errno; + switch (error_code) { +#define X(NAME, DESC)\ + case NAME:\ + desc = #NAME;\ + break; + LIST_ERRORS(X) +#undef X + default: + desc = NULL; + break; + } + return desc ? desc : error_code < 0 ? NULL : strerror(error_code); +} diff --git a/libgamma_strerror_r.c b/libgamma_strerror_r.c new file mode 100644 index 0000000..42f396e --- /dev/null +++ b/libgamma_strerror_r.c @@ -0,0 +1,61 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Get a description of an error + * + * @param error_code The error code, may be an `errno` value, if + * `LIBGAMMA_ERRNO_SET`, the current value of `errno` + * will be used + * @param buf Buffer that shall be used if a description must be generated + * @param bufsize The size of `buf`, 1024 is recommended + * @return The description associated with the error code; + * can only be `NULL` if `buf` is `NULL`. If the buffer + * is insufficient, a truncated but NUL-terminated + * description is returned and `errno` is set to `ERANGE`; + * `errno` is otherwise unmodified + */ +const char * +libgamma_strerror_r(int error_code, char buf[], size_t bufsize) +{ + const char *desc; + int saved_errno; + if (error_code == LIBGAMMA_ERRNO_SET) + error_code = errno; + switch (error_code) { +#define X(NAME, DESC)\ + case NAME:\ + desc = #NAME;\ + break; + LIST_ERRORS(X) +#undef X + default: + desc = NULL; + break; + } + if (desc) + return desc; + if (error_code >= 0) { + saved_errno = errno; + desc = _Generic(strerror_r(error_code, buf, bufsize), + /* XSI strerror_r */ + int: (errno = (int)(intptr_t)strerror_r(error_code, buf, bufsize)) ? NULL : buf, + /* GNU strerror_r */ + char *: (desc = (char *)(uintptr_t)strerror_r(error_code, buf, bufsize))); + if (desc) { + errno = saved_errno; + if (!buf || strcmp(buf, "No error information")) + return buf; + } else if (errno == ERANGE) { + return buf; + } else { + errno = saved_errno; + } + } + if (!buf) + return buf; + *buf = '\0'; /* TODO what happended here? */ + snprintf(buf, bufsize, "Unknown error #%i\n", error_code); + return buf; +} diff --git a/libgamma_subpixel_order_count.c b/libgamma_subpixel_order_count.c new file mode 100644 index 0000000..d77a748 --- /dev/null +++ b/libgamma_subpixel_order_count.c @@ -0,0 +1,9 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * The number of values defined in `libgamma_subpixel_order_t` + * in the version of the library the program is linked against + */ +const int libgamma_subpixel_order_count = LIBGAMMA_SUBPIXEL_ORDER_COUNT; diff --git a/libgamma_unhex_edid.c b/libgamma_unhex_edid.c new file mode 100644 index 0000000..37c9681 --- /dev/null +++ b/libgamma_unhex_edid.c @@ -0,0 +1,55 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Convert an hexadecimal representation of an EDID to a raw representation + * + * @param edid The EDID in hexadecimal representation + * @return The EDID in raw representation, it will be half the length + * of `edid` (the input value); `NULL` on allocation error or + * if the EDID is malformated, `errno` will be set accordingly + */ +unsigned char * +libgamma_unhex_edid(const char *restrict edid) +{ + unsigned char *restrict out; + size_t i, n = strlen(edid); + char a, b; + + /* Check that the length of the strings is even, + * otherwise it cannot represent bytes */ + if (n & 1) { + errno = EINVAL; + return NULL; + } + + /* Allocate memory area for output octet array */ + n /= 2 * sizeof(unsigned char); + out = malloc(n); + if (!out) + return NULL; + + /* Convert to raw octet array */ + for (i = 0; i < n; i++) { + /* Get the next character pair that, it represents an octet. */ + a = edid[i * 2 + 0]; + b = edid[i * 2 + 1]; + + /* Verify that the input is in hexadecimal */ + if (!isxdigit(a) || !isxdigit(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; +} diff --git a/libgamma_value_of_connector_type.c b/libgamma_value_of_connector_type.c new file mode 100644 index 0000000..ff81bfc --- /dev/null +++ b/libgamma_value_of_connector_type.c @@ -0,0 +1,29 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Get the value of a connector type + * + * @param connector The name of the connector type, for example + * "VGA" or "LIBGAMMA_CONNECTOR_TYPE_VGA" + * @return The connector type; for example `LIBGAMMA_CONNECTOR_TYPE_VGA` + * for "VGA" and "LIBGAMMA_CONNECTOR_TYPE_VGA" + */ +int +libgamma_value_of_connector_type(const char *connector) +{ +#define X(CONST, NAME)\ + if (!strcmp(connector, NAME))\ + return CONST; + LIST_CONNECTOR_TYPES(X) +#undef X + +#define X(CONST, NAME)\ + if (!strcmp(connector, #CONST))\ + return CONST; + LIST_CONNECTOR_TYPES(X) +#undef X + + return 0; +} diff --git a/libgamma_value_of_error.c b/libgamma_value_of_error.c new file mode 100644 index 0000000..6d2f878 --- /dev/null +++ b/libgamma_value_of_error.c @@ -0,0 +1,23 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Get the value of a `libgamma` error definition refered to by name + * + * @param name The name of the definition associated with the error code + * @return The error code, zero if the name does is `NULL` + * or does not refer to a `libgamma` error + */ +int +libgamma_value_of_error(const char *name) +{ + if (!name) + return 0; +#define X(NAME, DESC)\ + if (!strcmp(name, #NAME))\ + return NAME; + LIST_ERRORS(X) + #undef X + return 0; +} diff --git a/libgamma_value_of_method.c b/libgamma_value_of_method.c new file mode 100644 index 0000000..ffd69c8 --- /dev/null +++ b/libgamma_value_of_method.c @@ -0,0 +1,28 @@ +#include "common.h" + + +/** + * Get the value of an adjustment method + * + * @param method The name of the adjustment method, for example + * "randr" or "LIBGAMMA_METHOD_X_RANDR" + * @return The adjustment method; for example `LIBGAMMA_METHOD_X_RANDR` + * for "randr" and "LIBGAMMA_METHOD_X_RANDR" + */ +int +libgamma_value_of_method(const char *method) +{ +#define X(CONST, NAME, CNAME, ENABLED)\ + if (!strcmp(method, #NAME))\ + return CONST; + LIST_METHODS(X) +#undef X + +#define X(CONST, NAME, CNAME, ENABLED)\ + if (!strcmp(method, #CONST))\ + return CONST; + LIST_METHODS(X) +#undef X + + return 0; +} diff --git a/libgamma_value_of_subpixel_order.c b/libgamma_value_of_subpixel_order.c new file mode 100644 index 0000000..02a1fa7 --- /dev/null +++ b/libgamma_value_of_subpixel_order.c @@ -0,0 +1,29 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Get the value of a subpixel order + * + * @param order The name of the subpixel order, for example + * "Horizontal RGB" or "LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_RGB" + * @return The subpixel order; for example `LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_RGB` + * for "Horizontal RGB" and "LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_RGB" + */ +int +libgamma_value_of_subpixel_order(const char *order) +{ +#define X(CONST, NAME)\ + if (!strcmp(order, NAME))\ + return CONST; + LIST_SUBPIXEL_ORDERS(X) +#undef X + +#define X(CONST, NAME)\ + if (!strcmp(order, #CONST))\ + return CONST; + LIST_SUBPIXEL_ORDERS(X) +#undef X + + return 0; +} diff --git a/mk/linux.mk b/mk/linux.mk new file mode 100644 index 0000000..32d9953 --- /dev/null +++ b/mk/linux.mk @@ -0,0 +1,8 @@ +LINUX_DRM_METHOD = yes +X_RANDR_METHOD = yes +X_VIDMODE_METHOD = yes + +LIBEXT = so +LIBFLAGS = -shared -Wl,-soname,libgamma.$(LIBEXT).$(LIB_MAJOR) +LIBMAJOREXT = $(LIBEXT).$(LIB_MAJOR) +LIBMINOREXT = $(LIBEXT).$(LIB_VERSION) diff --git a/mk/macos.mk b/mk/macos.mk new file mode 100644 index 0000000..3a670b1 --- /dev/null +++ b/mk/macos.mk @@ -0,0 +1,6 @@ +QUARTZ_CG_METHOD = yes + +LIBEXT = dylib +LIBFLAGS = -dynamiclib +LIBMAJOREXT = $(LIB_MAJOR).$(LIBEXT) +LIBMINOREXT = $(LIB_VERSION).$(LIBEXT) diff --git a/mk/method-dummy=no.mk b/mk/method-dummy=no.mk new file mode 100644 index 0000000..e69de29 diff --git a/mk/method-dummy=yes.mk b/mk/method-dummy=yes.mk new file mode 100644 index 0000000..18773cf --- /dev/null +++ b/mk/method-dummy=yes.mk @@ -0,0 +1,3 @@ +HDR_METHODS += gamma-dummy.h +METHODS_PARAMS += LIBGAMMA_METHOD_DUMMY dummy 0 ramps16 +CPPFLAGS_METHODS += -DHAVE_LIBGAMMA_METHOD_DUMMY diff --git a/mk/method-linux-drm=no.mk b/mk/method-linux-drm=no.mk new file mode 100644 index 0000000..e69de29 diff --git a/mk/method-linux-drm=yes.mk b/mk/method-linux-drm=yes.mk new file mode 100644 index 0000000..8799388 --- /dev/null +++ b/mk/method-linux-drm=yes.mk @@ -0,0 +1,5 @@ +HDR_METHODS += gamma-linux-drm.h +METHODS_PARAMS += LIBGAMMA_METHOD_LINUX_DRM linux_drm 16 ramps16 +CPPFLAGS_METHODS += -DHAVE_LIBGAMMA_METHOD_LINUX_DRM +CFLAGS_METHODS += $$(pkg-config --cflags libdrm) +LDFLAGS_METHODS += $$(pkg-config --libs libdrm) diff --git a/mk/method-quartz-cg=fake.mk b/mk/method-quartz-cg=fake.mk new file mode 100644 index 0000000..e33253a --- /dev/null +++ b/mk/method-quartz-cg=fake.mk @@ -0,0 +1,3 @@ +HDR_METHODS += gamma-quartz-cg.h +METHODS_PARAMS += LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS quartz_cg -1 rampsf +CPPFLAGS_METHODS += -DHAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS -DFAKE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS diff --git a/mk/method-quartz-cg=no.mk b/mk/method-quartz-cg=no.mk new file mode 100644 index 0000000..e69de29 diff --git a/mk/method-quartz-cg=yes.mk b/mk/method-quartz-cg=yes.mk new file mode 100644 index 0000000..b98d03d --- /dev/null +++ b/mk/method-quartz-cg=yes.mk @@ -0,0 +1,8 @@ +F_APPLICATION_SERVICES = /System/Library/Frameworks/ApplicationServices.framework +I_APPLICATION_SERVICES = $(F_APPLICATION_SERVICES)/Versions/A/Frameworks/CoreGraphics.framework/Versions/A/Headers + +HDR_METHODS += gamma-quartz-cg.h +METHODS_PARAMS += LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS quartz_cg -1 rampsf +CPPFLAGS_METHODS += -DHAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS +CFLAGS_METHODS += -I$(I_APPLICATION_SERVICES) -F$(F_APPLICATION_SERVICES) +LDFLAGS_METHODS += -framework ApplicationServices diff --git a/mk/method-w32-gdi=fake.mk b/mk/method-w32-gdi=fake.mk new file mode 100644 index 0000000..add5f79 --- /dev/null +++ b/mk/method-w32-gdi=fake.mk @@ -0,0 +1,3 @@ +HDR_METHODS += gamma-w32-gdi.h +METHODS_PARAMS += LIBGAMMA_METHOD_W32_GDI w32_gdi 16 ramps16 +CPPFLAGS_METHODS += -DHAVE_LIBGAMMA_METHOD_W32_GDI -DFAKE_LIBGAMMA_METHOD_W32_GDI diff --git a/mk/method-w32-gdi=no.mk b/mk/method-w32-gdi=no.mk new file mode 100644 index 0000000..e69de29 diff --git a/mk/method-w32-gdi=yes.mk b/mk/method-w32-gdi=yes.mk new file mode 100644 index 0000000..348b71f --- /dev/null +++ b/mk/method-w32-gdi=yes.mk @@ -0,0 +1,3 @@ +HDR_METHODS += gamma-w32-gdi.h +METHODS_PARAMS += LIBGAMMA_METHOD_W32_GDI w32_gdi 16 ramps16 +CPPFLAGS_METHODS += -DHAVE_LIBGAMMA_METHOD_W32_GDI diff --git a/mk/method-x-randr=no.mk b/mk/method-x-randr=no.mk new file mode 100644 index 0000000..e69de29 diff --git a/mk/method-x-randr=yes.mk b/mk/method-x-randr=yes.mk new file mode 100644 index 0000000..91a935b --- /dev/null +++ b/mk/method-x-randr=yes.mk @@ -0,0 +1,5 @@ +HDR_METHODS += gamma-x-randr.h +METHODS_PARAMS += LIBGAMMA_METHOD_X_RANDR x_randr 16 ramps16 +CPPFLAGS_METHODS += -DHAVE_LIBGAMMA_METHOD_X_RANDR +CFLAGS_METHODS += $$(pkg-config --cflags xcb xcb-randr) +LDFLAGS_METHODS += $$(pkg-config --libs xcb xcb-randr) diff --git a/mk/method-x-vidmode=no.mk b/mk/method-x-vidmode=no.mk new file mode 100644 index 0000000..e69de29 diff --git a/mk/method-x-vidmode=yes.mk b/mk/method-x-vidmode=yes.mk new file mode 100644 index 0000000..e54d98a --- /dev/null +++ b/mk/method-x-vidmode=yes.mk @@ -0,0 +1,5 @@ +HDR_METHODS += gamma-x-vidmode.h +METHODS_PARAMS += LIBGAMMA_METHOD_X_VIDMODE x_vidmode 16 ramps16 +CPPFLAGS_METHODS += -DHAVE_LIBGAMMA_METHOD_X_VIDMODE +CFLAGS_METHODS += $$(pkg-config --cflags x11 xxf86vm) +LDFLAGS_METHODS += $$(pkg-config --libs x11 xxf86vm) diff --git a/mk/windows.mk b/mk/windows.mk new file mode 100644 index 0000000..4520743 --- /dev/null +++ b/mk/windows.mk @@ -0,0 +1,6 @@ +W32_GDI_METHOD = yes + +LIBEXT = dll +LIBFLAGS = -mdll +LIBMAJOREXT = $(LIB_MAJOR).$(LIBEXT) +LIBMINOREXT = $(LIB_VERSION).$(LIBEXT) diff --git a/set_ramps.h b/set_ramps.h new file mode 100644 index 0000000..b396450 --- /dev/null +++ b/set_ramps.h @@ -0,0 +1,25 @@ +/* See LICENSE file for copyright and license details. */ + +/* + * This file is intended to be included from + * libgamma_crtc_set_gamma_ramps{8,16,32,64,f,d} + */ + + +gamma_ramps_any_t ramps_; +switch (this->partition->site->method) { +#define X(CONST, CNAME, MDEPTH, MRAMPS)\ +case CONST:\ + if (!(MDEPTH)) {\ + return APPEND_RAMPS(libgamma_dummy_crtc_set_gamma_)(this, ramps);\ + } else if ((DEPTH) == (MDEPTH)) {\ + return libgamma_##CNAME##_crtc_set_gamma_##MRAMPS(this, *(libgamma_gamma_##MRAMPS##_t *)&ramps); \ + } else {\ + ramps_.TYPE = ramps;\ + return libgamma_internal_translated_ramp_set(this, ramps_, DEPTH, MDEPTH, libgamma_crtc_set_gamma_##MRAMPS);\ + } +LIST_AVAILABLE_METHODS(X) +#undef X +default: + return LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD; +} diff --git a/set_ramps_fun.h b/set_ramps_fun.h new file mode 100644 index 0000000..bdd1fe8 --- /dev/null +++ b/set_ramps_fun.h @@ -0,0 +1,50 @@ +/* See LICENSE file for copyright and license details. */ + +/* + * This file is intended to be included from + * libgamma_crtc_set_gamma_ramps{8,16,32,64,f,d}_f + */ + + +libgamma_crtc_information_t info; +struct APPEND_RAMPS(libgamma_gamma_) 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(TYPE)); +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 channel */ +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 channel */ +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 channel */ +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 = APPEND_RAMPS(libgamma_crtc_set_gamma_)(this, ramps); +free(ramps.red); +return e; diff --git a/src/lib/edid.c b/src/lib/edid.c deleted file mode 100644 index fc7b498..0000000 --- a/src/lib/edid.c +++ /dev/null @@ -1,105 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include "edid.h" - -#include "libgamma-method.h" -#include "libgamma-error.h" - -#include - - -/* - * EDID structure revision 1.1: - * http://en.wikipedia.org/w/index.php?title=Extended_display_identification_data&oldid=46295569 - * - * EDID structure revision 1.3: - * http://en.wikipedia.org/wiki/Extended_display_identification_data - */ - - -/** - * Parse the EDID of a monitor - * - * @param this Instance of a data structure to fill with the information about the EDID; - * it must contain the EDID and its length - * @param fields OR:ed identifiers for the information about the EDID that should be parsed; - * fields that do not have to do with EDID are ignored - * @return Non-zero on error - */ -int -libgamma_parse_edid(libgamma_crtc_information_t *restrict this, int32_t fields) -{ -#define __test_version(edid, major, minor_min, minor_max)\ - ((edid)[18] == (major) && (minor_min) <= (edid)[19] && (edid)[19] <= (minor_max)) -#define __m(value)\ - (this->edid[index++] != (value)) - - int error = 0, checksum = 0; - size_t i, index = 0; - - /* If the length of the EDID is not 128 bytes, we know that it is not of EDID - structure revision 1.0–1.3, and thus we do not support it. Additionally - this make sure we do not do segmentation violation on the next test. */ - if (this->edid_length != 128) - error = LIBGAMMA_EDID_LENGTH_UNSUPPORTED; - /* Check that the magic number of that of the EDID structure. */ - else if (__m(0x00) || __m(0xFF) || __m(0xFF) || __m(0xFF) || __m(0xFF) || __m(0xFF) || __m(0xFF) || __m(0x00)) - error = LIBGAMMA_EDID_WRONG_MAGIC_NUMBER; - /* Check that EDID structure revision 1.1–1.3 is used, those are the only - version we support. EDID structure revision 1.3 is also by far the most - commonly use revision and it is currently the newest revision. We know - that parsing works for both revision 1.1 and revision 1.3, because of - this we assume it is also correct for revision 1.2. However, we are not - assuming this for revision 1.0 which appeared in August 1994 and was - replaced by revision 1.1 in April 1996. */ - else if (!__test_version(this->edid, 1, 1, 3)) - error = LIBGAMMA_EDID_REVISION_UNSUPPORTED; - - /* If we have encountered an error, report it for the fields that require - the EDID to be parsed. Note that it is not stored for the EDID field - itself because it is not considered an error just because we do not - support the used version. */ - this->width_mm_edid_error = this->height_mm_edid_error = this->gamma_error = error; - - /* Retrieve the size of the viewport. This is done even if it is not - requested because it is not worth it branch. */ - this->width_mm_edid = (size_t)this->edid[21] * 10; - this->height_mm_edid = (size_t)this->edid[22] * 10; - - /* Retrieve the monitor's gamma characteristics. */ - if ((fields & LIBGAMMA_CRTC_INFO_GAMMA) && !error) { - if (this->edid[23] == 0xFF) { - /* If the gamma charactistics is FFh (3,55) it should be interpreted as not specified. */ - this->gamma_error = LIBGAMMA_GAMMA_NOT_SPECIFIED; - } else { - this->gamma_blue = (float)((int)this->edid[23] + 100) / 100.f; - this->gamma_red = this->gamma_green = this->gamma_blue; - } - } - - /* If not error has occurred, calculate and test the checksum. - It is not considered an error that the gamma characteristics - is left unspecified in the EDID. */ - if (!error) { - for (i = 0; i < this->edid_length; i++) - checksum += (int)this->edid[i]; - } - /* The checksum should be zero. */ - if (checksum & 255) { - /* Store the error in all fields that require the EDID to be parsed, - as well as the EDID field itself. */ - error = LIBGAMMA_EDID_CHECKSUM_ERROR; - this->edid_error = this->width_mm_edid_error = this->height_mm_edid_error = error; - /* If the gamma characteristics is not specified, that is kept, - and the checksum error is augmented. */ - this->gamma_error = this->gamma_error == LIBGAMMA_GAMMA_NOT_SPECIFIED - ? LIBGAMMA_GAMMA_NOT_SPECIFIED_AND_EDID_CHECKSUM_ERROR : error; - } - - /* Return whether or not we encountered an error or if - the gamma characteristics was requested but is not - specified in the monitor's EDID. */ - return error | this->gamma_error; - -#undef __m -#undef __test_version -} diff --git a/src/lib/edid.h b/src/lib/edid.h deleted file mode 100644 index 51529ae..0000000 --- a/src/lib/edid.h +++ /dev/null @@ -1,22 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_EDID_H -#define LIBGAMMA_EDID_H - -#include "libgamma-method.h" - -#include - - -/** - * Parse the EDID of a monitor - * - * @param this Instance of a data structure to fill with the information about the EDID; - * it must contain the EDID and its length - * @param fields OR:ed identifiers for the information about the EDID that should be parsed; - * fields that do not have to do with EDID are ignored - * @return Non-zero on error - */ -int libgamma_parse_edid(libgamma_crtc_information_t *restrict, int32_t); - - -#endif diff --git a/src/lib/fake-quartz-cg.c b/src/lib/fake-quartz-cg.c deleted file mode 100644 index 75f1fb2..0000000 --- a/src/lib/fake-quartz-cg.c +++ /dev/null @@ -1,447 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef FAKE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS -# error Compiling fake-quartz-cg.c without FAKE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS -#endif - -/* This file very sloppily translates Mac OS X Quartz calls to X RandR calls. - * It should by no means be used, without additional modification, as a - * part of a compatibility layer. The purpose of this file is only to make - * it possible to test for logical errors in Max OS X specific code on - * a GNU/Linux system under X. */ - - -#include "fake-quartz-cg.h" - -#include -#include -#include - - -#ifndef HAVE_LIBGAMMA_METHOD_X_RANDR -/* Use dummy translation */ - - -/** - * Get a list of all online displays on the system - * - * @param max_size The number of elements allocated for `displays_out` - * @param displays_out List ot fill with the ID for each online display on the system - * @param count_out Output parameter for the number of elements stored in `displays_out` - * if `displays_out` is too small to fit all display ID:s, - * `*count_out` will be `max_size` - * @return `kCGErrorSuccess` on success, and error number of failure - */ -CGError -CGGetOnlineDisplayList(uint32_t max_size, CGDirectDisplayID *restrict displays_out, uint32_t *restrict count_out) -{ - /* Pretend that we have 2 CRTC:s */ - uint32_t i; - for (i = 0; i < max_size && i < 2; i++) - displays_out[i] = (CGDirectDisplayID)i; - *count_out = i; - return kCGErrorSuccess; -} - - -/** - * Set the gamma ramps for a display - * - * @param display The ID of the display - * @param gamma_size The number of stops in gamma ramps - * @param red The gamma ramp for the red channel - * @param green The gamma ramp for the green channel - * @param blue The gamma ramp for the blue channel - * @return `kCGErrorSuccess` on success, and error number of failure - */ -CGError -CGSetDisplayTransferByTable(CGDirectDisplayID display, uint32_t gamma_size, const CGGammaValue *red, - const CGGammaValue *green, const CGGammaValue *blue) -{ - (void) display; - (void) red; - (void) green; - (void) blue; - - /* We pretend that our gamma ramps are of size 256 */ - if (gamma_size != 256) { - fprintf(stderr, "Gamma size should be 256\n"); - abort(); - } - return kCGErrorSuccess; -} - - -/** - * Get the current gamma ramps for a display - * - * @param display The ID of the display - * @param gamma_size The number of stops you have allocated for the gamma ramps - * @param red Table allocated for the gamma ramp for the red channel - * @param green Table allocated for the gamma ramp for the green channel - * @param blue Table allocated for the gamma ramp for the blue channel - * @param gamma_size_out Output parameter for the actual number of stops in the gamma ramps - * @return `kCGErrorSuccess` on success, and error number of failure - */ -CGError -CGGetDisplayTransferByTable(CGDirectDisplayID display, uint32_t gamma_size, CGGammaValue *restrict red, - CGGammaValue *restrict green, CGGammaValue *restrict blue, uint32_t *restrict gamma_size_out) -{ - long i; - - (void) display; - - /* We pretend that our gamma ramps are of size 256 */ - if (gamma_size != 256) { - fprintf(stderr, "Gamma size should be 256\n"); - abort(); - } - - /* We pretend that our gamma ramps are of size 256 */ - *gamma_size_out = 256; - - /* Pretend that our gamma ramps are identity mappings */ - for (i = 0; i < 256; i++) - red[i] = green[i] = blue[i] = (CGGammaValue)i / 255; - - return kCGErrorSuccess; -} - - -/** - * Restore each display's gamma ramps to the settings in ColorSync - */ -void -CGDisplayRestoreColorSyncSettings(void) -{ - /* Do nothing */ -} - - -/** - * Get the number of stops in the gamma ramps for a display - * - * @param display The ID of the display - * @return The number of stops in the gamma ramps - */ -uint32_t -CGDisplayGammaTableCapacity(CGDirectDisplayID display) -{ - /* We pretend that our gamma ramps are of size 256 */ - (void) display; - return 256; -} - - -/** - * Release resources used by the backend - */ -void -close_fake_quartz(void) -{ - /* Do nothing */ -} - - -#else -/* Use translation to X RandR */ - - -#include -#include - - - -/** - * Connection to the X RandR display - */ -static xcb_connection_t *restrict connection = NULL; - -/** - * Resouces for the screen - * - * We only have one screen, again this - * is a very sloppy compatibility layer - */ -static xcb_randr_get_screen_resources_current_reply_t *restrict res_reply = NULL; - -/** - * The number of available CRTC:s - */ -static uint32_t crtc_count = 0; - -/** - * List of X RandR CRTC:s - */ -static xcb_randr_crtc_t *restrict crtcs = NULL; - -/** - * The original gamma ramps, used to emulate gamma ramp restoration to system settings - */ -static uint16_t *restrict original_ramps = NULL; - - - -/* xcb violates the rule to never return struct:s */ -#ifdef __GNUC__ -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Waggregate-return" -#endif - - -/** - * Get a list of all online displays on the system - * - * @param max_size The number of elements allocated for `displays_out` - * @param displays_out List ot fill with the ID for each online display on the system - * @param count_out Output parameter for the number of elements stored in `displays_out`, - * if `displays_out` is too small to fit all display ID:s, - * `*count_out` will be `max_size` - * @return `kCGErrorSuccess` on success, and error number of failure - */ -CGError -CGGetOnlineDisplayList(uint32_t max_size, CGDirectDisplayID *restrict displays_out, uint32_t *restrict count_out) -{ - xcb_generic_error_t *error; - xcb_screen_iterator_t iter; - xcb_randr_get_screen_resources_current_cookie_t res_cookie; - xcb_randr_get_crtc_gamma_cookie_t gamma_cookie; - xcb_randr_get_crtc_gamma_reply_t *restrict gamma_reply; - uint32_t i; - - /* Connect to the display and get screen data if not already done so */ - if (!connection) { - /* Connect to the display */ - connection = xcb_connect(NULL, NULL); - /* Get the first screen */ - iter = xcb_setup_roots_iterator(xcb_get_setup(connection)); - /* Get the resources of the screen */ - res_cookie = xcb_randr_get_screen_resources_current(connection, iter.data->root); - res_reply = xcb_randr_get_screen_resources_current_reply(connection, res_cookie, &error); - if (error) { - fprintf(stderr, "Failed to open X connection\n"); - xcb_disconnect(connection); - crtc_count = 0; - return ~kCGErrorSuccess; - } - - /* Get the number of CRTC:s */ - crtc_count = (uint32_t)(res_reply->num_crtcs); - /* Get the CRTC ID:s */ - crtcs = xcb_randr_get_screen_resources_current_crtcs(res_reply); - - /* Allocate memory where we store the gamma ramps as - * they looked when this adjustment method was first - * used. This is used to emulate the functionality of - * `CGDisplayRestoreColorSyncSettings` which restore the - * all gamma ramps on the system to the system settnigs. - */ - original_ramps = malloc(crtc_count * 3 * 256 * sizeof(uint16_t)); - if (!original_ramps) { - perror("malloc"); - xcb_disconnect(connection); - crtc_count = 0; - return ~kCGErrorSuccess; - } - - /* Fill the gamma ramps we just allocated */ - for (i = 0; i < crtc_count; i++) { - /* Read current gamma ramps */ - gamma_cookie = xcb_randr_get_crtc_gamma(connection, crtcs[i]); - gamma_reply = xcb_randr_get_crtc_gamma_reply(connection, gamma_cookie, &error); - if (error) { - fprintf(stderr, "Failed to read gamma ramps.\n"); - xcb_disconnect(connection); - crtc_count = 0; - return ~kCGErrorSuccess; - } - - /* Copy over the gamma ramps to the memory area we have allocated */ -#define __DEST(C) original_ramps + (C + 3 * i) * 256 -#define __SRC(C) xcb_randr_get_crtc_gamma_##C(gamma_reply) - memcpy(__DEST(0), __SRC(red), 256 * sizeof(uint16_t)); - memcpy(__DEST(1), __SRC(green), 256 * sizeof(uint16_t)); - memcpy(__DEST(2), __SRC(blue), 256 * sizeof(uint16_t)); -#undef __SRC -#undef __DEST - - /* Release resouces */ - free(gamma_reply); - } - } - - /* Return CRTC ID:s */ - for (i = 0; i < max_size && i < crtc_count; i++) - displays_out[i] = (CGDirectDisplayID)i; - - /* Return the number of CRTC ID:s we returned */ - *count_out = i; - return kCGErrorSuccess; -} - - -/** - * Set the gamma ramps for a display - * - * @param display The ID of the display - * @param gamma_size The number of stops in gamma ramps - * @param red The gamma ramp for the red channel - * @param green The gamma ramp for the green channel - * @param blue The gamma ramp for the blue channel - * @return `kCGErrorSuccess` on success, and error number of failure - */ -CGError -CGSetDisplayTransferByTable(CGDirectDisplayID display, uint32_t gamma_size, const CGGammaValue *red, - const CGGammaValue *green, const CGGammaValue *blue) -{ - xcb_void_cookie_t gamma_cookie; - uint16_t r_int[256], g_int[256], b_int[256]; - int32_t v; - long i; - - /* This is a sloppy compatibility layer that assumes the gamma ramp size is 256 */ - if (gamma_size != 256) { - fprintf(stderr, "Gamma size should be 256.\n"); - abort(); - } - - /* Translate the gamma ramps from float (CoreGraphics) to 16-bit unsigned integer (X RandR) */ - for (i = 0; i < 256; i++) { - /* Red channel */ - v = (int32_t)(red[i] * UINT16_MAX); - r_int[i] = (uint16_t)(v < 0 ? 0 : v > UINT16_MAX ? UINT16_MAX : v); - - /* Green channel */ - v = (int32_t)(green[i] * UINT16_MAX); - g_int[i] = (uint16_t)(v < 0 ? 0 : v > UINT16_MAX ? UINT16_MAX : v); - - /* Blue channel */ - v = (int32_t)(blue[i] * UINT16_MAX); - b_int[i] = (uint16_t)(v < 0 ? 0 : v > UINT16_MAX ? UINT16_MAX : v); - } - - /* Apply gamma ramps */ - gamma_cookie = xcb_randr_set_crtc_gamma_checked(connection, crtcs[display], (uint16_t)gamma_size, r_int, g_int, b_int); - /* Check for errors */ - return !xcb_request_check(connection, gamma_cookie) ? kCGErrorSuccess : ~kCGErrorSuccess; -} - - -/** - * Get the current gamma ramps for a display - * - * @param display The ID of the display - * @param gamma_size The number of stops you have allocated for the gamma ramps - * @param red Table allocated for the gamma ramp for the red channel - * @param green Table allocated for the gamma ramp for the green channel - * @param blue Table allocated for the gamma ramp for the blue channel - * @param gamma_size_out Output parameter for the actual number of stops in the gamma ramps - * @return `kCGErrorSuccess` on success, and error number of failure - */ -CGError -CGGetDisplayTransferByTable(CGDirectDisplayID display, uint32_t gamma_size, CGGammaValue *restrict red, - CGGammaValue *restrict green, CGGammaValue *restrict blue, uint32_t *restrict gamma_size_out) -{ - xcb_randr_get_crtc_gamma_cookie_t gamma_cookie; - xcb_randr_get_crtc_gamma_reply_t *restrict gamma_reply; - xcb_generic_error_t *error; - uint16_t *restrict r_int; - uint16_t *restrict g_int; - uint16_t *restrict b_int; - long i; - - /* This is a sloppy compatibility layer that assumes the gamma ramp size is 256 */ - if (gamma_size != 256) { - fprintf(stderr, "Gamma size should be 256\n"); - abort(); - } - - /* The gamma ramp size should be returned to the caller */ - *gamma_size_out = 256; - - /* Read current gamma ramps */ - gamma_cookie = xcb_randr_get_crtc_gamma(connection, crtcs[display]); - gamma_reply = xcb_randr_get_crtc_gamma_reply(connection, gamma_cookie, &error); - if (error) { - fprintf(stderr, "Failed to write gamma ramps\n"); - return ~kCGErrorSuccess; - } - - /* Get gamma ramp values */ - r_int = xcb_randr_get_crtc_gamma_red(gamma_reply); - g_int = xcb_randr_get_crtc_gamma_green(gamma_reply); - b_int = xcb_randr_get_crtc_gamma_blue(gamma_reply); - - /* Translate gamma ramps to float format, that is what CoreGraphics uses */ - for (i = 0; i < 256; i++) { - red[i] = (CGGammaValue)r_int[i] / UINT16_MAX; - green[i] = (CGGammaValue)g_int[i] / UINT16_MAX; - blue[i] = (CGGammaValue)b_int[i] / UINT16_MAX; - } - - free(gamma_reply); - return kCGErrorSuccess; -} - - -/** - * Restore each display's gamma ramps to the settings in ColorSync - */ -void -CGDisplayRestoreColorSyncSettings(void) -{ - xcb_generic_error_t *error; - xcb_void_cookie_t gamma_cookie; - uint32_t i; - - /* Restore the gamma ramps on each monitor to - * the ramps that we used when we started */ - for (i = 0; i < crtc_count; i++) { - /* We assume that our gamma ramps are of the size - * 256 (this is a sloppy compatibility layer) */ - gamma_cookie = xcb_randr_set_crtc_gamma_checked(connection, crtcs[i], 256, - &original_ramps[(0 + 3 * i) * 256], - &original_ramps[(1 + 3 * i) * 256], - &original_ramps[(2 + 3 * i) * 256]); - if ((error = xcb_request_check(connection, gamma_cookie))) - fprintf(stderr, "Quartz gamma reset emulation with RandR returned %i\n", error->error_code); - } -} - - -/** - * Get the number of stops in the gamma ramps for a display - * - * @param display The ID of the display - * @return The number of stops in the gamma ramps - */ -uint32_t -CGDisplayGammaTableCapacity(CGDirectDisplayID display) -{ - /* We assume that our gamma ramps are of the size - * 256 (this is a sloppy compatibility layer) */ - (void) display; - return 256; -} - - -#ifdef __GNUC__ -# pragma GCC diagnostic pop -#endif - - -/** - * Release resources used by the backend - */ -void close_fake_quartz_cg(void) -{ - free(res_reply); - res_reply = NULL; - if (connection) { - xcb_disconnect(connection); - connection = NULL; - } - free(original_ramps); - original_ramps = NULL; -} - -#endif diff --git a/src/lib/fake-quartz-cg.h b/src/lib/fake-quartz-cg.h deleted file mode 100644 index 361a73c..0000000 --- a/src/lib/fake-quartz-cg.h +++ /dev/null @@ -1,120 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_FAKE_QUARTZ_CG_H -#define LIBGAMMA_FAKE_QUARTZ_CG_H - -#ifndef FAKE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS -# error Including fake-quartz-cg.h without FAKE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS -#endif - - -#ifndef __GNUC__ -# define __attribute__(x) -#endif - - -/* This header file contains some capabilities of - * and , - * 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 - - -/** - * Numerical `typedef` for errors that occur in CoreGraphics calls - */ -typedef int32_t CGError; - -/** - * The call was successful - */ -#define kCGErrorSuccess 0 - -/** - * The data type that is used for the values in the gamma ramps - */ -typedef float CGGammaValue; - -/** - * The data type for display ID:s - */ -typedef uint32_t CGDirectDisplayID; - - -/** - * Get a list of all online displays on the system - * - * @param max_size The number of elements allocated for `displays_out` - * @param displays_out List ot fill with the ID for each online display on the system - * @param count_out Output parameter for the number of elements stored in `displays_out` - * if `displays_out` is too small to fit all display ID:s, - * `*count_out` will be `max_size` - * @return `kCGErrorSuccess` on success, and error number of failure - */ -CGError CGGetOnlineDisplayList(uint32_t, CGDirectDisplayID *restrict, uint32_t *restrict); - -/** - * Set the gamma ramps for a display - * - * @param display The ID of the display - * @param gamma_size The number of stops in gamma ramps - * @param red The gamma ramp for the red channel - * @param green The gamma ramp for the green channel - * @param blue The gamma ramp for the blue channel - * @return `kCGErrorSuccess` on success, and error number of failure - */ -CGError CGSetDisplayTransferByTable(CGDirectDisplayID, uint32_t, const CGGammaValue *, const CGGammaValue *, const CGGammaValue *); - -/** - * Get the current gamma ramps for a display - * - * @param display The ID of the display - * @param gamma_size The number of stops you have allocated for the gamma ramps - * @param red Table allocated for the gamma ramp for the red channel - * @param green Table allocated for the gamma ramp for the green channel - * @param blue Table allocated for the gamma ramp for the blue channel - * @param gamma_size_out Output parameter for the actual number of stops in the gamma ramps - * @return `kCGErrorSuccess` on success, and error number of failure - */ -CGError CGGetDisplayTransferByTable(CGDirectDisplayID, uint32_t, CGGammaValue *restrict, - CGGammaValue *restrict, CGGammaValue *restrict, uint32_t *restrict); - -/** - * Restore each display's gamma ramps to the settings in ColorSync - */ -void CGDisplayRestoreColorSyncSettings(void); - -/** - * Get the number of stops in the gamma ramps for a display - * - * @param display The ID of the display - * @return The number of stops in the gamma ramps - */ -uint32_t CGDisplayGammaTableCapacity(CGDirectDisplayID) __attribute__((const)); - - -/* The follow part most only be used when this module is used, - * it cannot be used when the real CoreGraphics is used. - * CoreGraphics does not have this function, it is added so - * that there is a way to cleanly close the X connection - * and free resources needed by this module. */ - -/** - * Release resources used by the backend - */ -void close_fake_quartz_cg(void); - - - -#ifndef __GNUC__ -# undef __attribute__ -#endif - -#endif diff --git a/src/lib/fake-w32-gdi.c b/src/lib/fake-w32-gdi.c deleted file mode 100644 index 68847ca..0000000 --- a/src/lib/fake-w32-gdi.c +++ /dev/null @@ -1,479 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef HAVE_LIBGAMMA_METHOD_W32_GDI -# error Compiling fake-w32-gdi.c without FAKE_LIBGAMMA_METHOD_W32_GDI -#endif - -/* This file very sloppily translates Windows GDI calls to X RandR calls. - * It should by no means be used, without additional modification, as a - * part of a compatibility layer. The purpose of this file is only to make - * it possible to test for logical errors in Windows specific code on - * a GNU/Linux system under X. */ - - -#include "fake-w32-gdi.h" - -#include -#include -#include - - -#ifndef HAVE_LIBGAMMA_METHOD_X_RANDR -/* Use dummy translation */ - - - -/** - * Get the device context for a window or the entire screen - * - * @param hWnd The windows, `NULL` for the entire screen - * @return The device context - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144871(v=vs.85).aspx - */ -HDC -GetDC(HWND hWnd) -{ - /* Just a non-NULL value */ - (void) hWnd; - return (HDC *)16; -} - - -/** - * Free a device context - * - * @param hWnd The window whose device context is `hDC`, `NULL` for the entire screen - * @param hDC The device context to free - * @return Whether the call was successful - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd162920(v=vs.85).aspx - */ -int -ReleaseDC(HWND hWnd, HDC hDC) -{ - /* Always successful */ - (void) hWnd; - (void) hDC; - return 1; -} - - - -/** - * Get information (capabilities) for a device context - * - * @param hDC The device context - * @param nIndex The information to retrieve, may be `COLORMGMTCAPS` - * @return The details of the queried information, can return `CM_GAMMA_RAMP` - * if `nIndex` is `COLORMGMTCAPS` - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877(v=vs.85).aspx - */ -int -GetDeviceCaps(HDC hDC, int nIndex) -{ - /* We have gamma ramps if the user asks for colour management capabilities */ - (void) hDC; - return CM_GAMMA_RAMP + nIndex - COLORMGMTCAPS; -} - - -/** - * Set the gamma ramps for a device - * - * @param hDC The device context - * @param lpRamp The gamma ramps joined in the order: red, green, blue - * This is a `WORD*` casted to `void*` - * @return Whether the call was successful - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd372194(v=vs.85).aspx - */ -BOOL -SetDeviceGammaRamp(HDC hDC, LPVOID restrict lpRamp) -{ - /* Always successful */ - (void) hDC; - (void) lpRamp; - return TRUE; -} - - -/** - * Get the current gamma ramps for a device - * - * @param hDC The device context - * @param lpRamp The output for the gamma ramps joined in the order: red, green, blue - * This is a `WORD *` casted to `void *` - * @return Whether the call was successful - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd316946(v=vs.85).aspx - */ -BOOL -GetDeviceGammaRamp(HDC hDC, LPVOID restrict lpRamp) -{ - /* Always successful */ - (void) hDC; - (void) lpRamp; - return TRUE; -} - - - -/** - * Get the context for a device - * - * @param lpszDriver The driver or a display device, use "DISPLAY" if you want a display - * @param lpszDevice The name of the device. If you want a display use can use the member - * name `DeviceName` in the third parameter, an output parameter, of - * `EnumDisplayDevices` - * @param lpszOutput We will always use `NULL` here - * @param lpInitData We will always use `NULL` here; this should actually by a `const DEVMODE *` - * @return The context for the device - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183490(v=vs.85).aspx - */ -HDC -CreateDC(LPCTSTR restrict lpszDriver, LPCTSTR restrict lpszDevice, LPCTSTR restrict lpszOutput, const void *restrict lpInitData) -{ - /* `NULL` if not asking for a CRTC, otherwise a non-NULL value */ - (void) lpszOutput; - (void) lpInitData; - (void) lpszDevice; - return !strcmp(lpszDriver, "DISPLAY") ? (HDC *)16 : NULL; -} - - -/** - * Get a display device by its name or index - * - * @param lpDevice The name of the device, use `NULL` to base the call on `iDevNum` instead - * @param iDevNum The index of the device - * @param lpDisplayDevice Output for the found device - * @param dwFlags Flags, we will always use zero - * @return Whether the call was successful; zero is also returned if `iDevNum` - * is greater than the largest device index on the system - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd162609(v=vs.85).aspx - */ -BOOL -EnumDisplayDevices(LPCTSTR lpDevice, restrict DWORD iDevNum, PDISPLAY_DEVICE restrict lpDisplayDevice, DWORD dwFlags) -{ - (void) dwFlags; - /* Check correctness of `lpDevice` */ - if (lpDevice) { - fprintf(stderr, "lpDevice (argument 1) for EnumDisplayDevices should be NULL\n"); - abort(); - return FALSE; - } - /* Pretend that we have two CRTC:s */ - if (iDevNum >= 2) - return FALSE; - /* Check correctness of `lpDisplayDevice` */ - if (lpDisplayDevice->cb != sizeof(DISPLAY_DEVICE)) { - fprintf(stderr, "lpDisplayDevice->cb for EnumDisplayDevices is not sizeof(DISPLAY_DEVICE)\n"); - abort(); - return FALSE; - } - /* Store an arbitrary name for the monitor */ - strcmp(lpDisplayDevice->DeviceName, "some monitor"); - /* The connector is always enabled */ - lpDisplayDevice->StateFlags = DISPLAY_DEVICE_ACTIVE; - return TRUE; -} - - -#else -/* Use translation to X RandR */ - - -#include -#include - - -/** - * The gamma ramp size that devices will - * always have in Windows GDI - */ -#define GAMMA_RAMP_SIZE 256 - - -/** - * Connection to the X RandR display - */ -static xcb_connection_t *restrict connection = NULL; - -/** - * Resouces for the screen - * - * We only have one screen, again this - * is a very sloppy compatibility layer - */ -static xcb_randr_get_screen_resources_current_reply_t *restrict res_reply = NULL; - -/** - * The number of available CRTC:s, -1 if not known yet - */ -static ssize_t crtc_count = -1; - -/** - * List of X RandR CRTC:s - */ -static xcb_randr_crtc_t *restrict crtcs = NULL; - -/** - * The number of opened CRTC:s - */ -static size_t dc_count = 0; - - - -/** - * Get the device context for a window or the entire screen - * - * @param hWnd The windows, `NULL` for the entire screen - * @return The device context - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144871(v=vs.85).aspx - */ -HDC -GetDC(HWND hWnd) -{ - /* Return the primary CRTC */ - (void) hWnd; - return CreateDC(TEXT("DISPLAY"), "0", NULL, NULL); -} - - -/** - * Free a device context - * - * @param hWnd The window whose device context is `hDC`, `NULL` for the entire screen - * @param hDC The device context to free - * @return Whether the call was successful - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd162920(v=vs.85).aspx - */ -int -ReleaseDC(HWND hWnd, HDC hDC) -{ - /* Disconnect from the RandR display when all monitors have been closed */ - (void) hWnd; - (void) hDC; - if (!dc_count--) { - if (connection) { - xcb_disconnect(connection); - connection = NULL; - } - free(res_reply); - res_reply = NULL; - } - return 1; -} - - - -/** - * Get information (capabilities) for a device context - * - * @param hDC The device context - * @param nIndex The information to retrieve, may be `COLORMGMTCAPS` - * @return The details of the queried information, can return `CM_GAMMA_RAMP` - * if `nIndex` is `COLORMGMTCAPS` - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877(v=vs.85).aspx - */ -int -GetDeviceCaps(HDC hDC, int nIndex) -{ - /* We have gamma ramps if the user asks for colour management capabilities */ - (void) hDC; - return CM_GAMMA_RAMP + nIndex - COLORMGMTCAPS; -} - - -/* xcb violates the rule to never return `struct`:s */ -#ifdef __GNUC__ -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Waggregate-return" -#endif - - -/** - * Set the gamma ramps for a device - * - * @param hDC The device context - * @param lpRamp The gamma ramps joined in the order: red, green, blue - * This is a `WORD *` casted to `void *` - * @return Whether the call was successful - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd372194(v=vs.85).aspx - */ -BOOL -SetDeviceGammaRamp(HDC hDC, LPVOID restrict lpRamp) -{ - /* We assume that our gamma ramps are of the same size - * as used by Windows GDI (we are so sloppy) */ - xcb_void_cookie_t gamma_cookie = - xcb_randr_set_crtc_gamma_checked(connection, *(xcb_randr_crtc_t *)hDC, GAMMA_RAMP_SIZE, - &((uint16_t *)lpRamp)[0 * GAMMA_RAMP_SIZE], - &((uint16_t *)lpRamp)[1 * GAMMA_RAMP_SIZE], - &((uint16_t *)lpRamp)[2 * GAMMA_RAMP_SIZE)]; - xcb_generic_error_t *error = xcb_request_check(connection, gamma_cookie); - return !error ? TRUE : FALSE; -} - - -/** - * Get the current gamma ramps for a device - * - * @param hDC The device context - * @param lpRamp The output for the gamma ramps joined in the order: red, green, blue - * This is a `WORD *` casted to `void *` - * @return Whether the call was successful - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd316946(v=vs.85).aspx - */ -BOOL -GetDeviceGammaRamp(HDC hDC, LPVOID restrict lpRamp) -{ - xcb_randr_get_crtc_gamma_cookie_t gamma_cookie; - xcb_randr_get_crtc_gamma_reply_t *restrict gamma_reply; - xcb_generic_error_t *error; - - /* Read current gamma ramps */ - gamma_cookie = xcb_randr_get_crtc_gamma(connection, *(xcb_randr_crtc_t *)hDC); - gamma_reply = xcb_randr_get_crtc_gamma_reply(connection, gamma_cookie, &error); - if (error) - return FALSE; - -#define DEST_RAMP(I) (&((uint16_t *)lpRamp)[(I) * GAMMA_RAMP_SIZE]) -#define SRC_RAMP(C) (xcb_randr_get_crtc_gamma_##C(gamma_reply)) - - /* Copy the ramps into the output parameter (coalesced) */ - memcpy(DEST_RAMP(0), SRC_RAMP(red), GAMMA_RAMP_SIZE * sizeof(uint16_t)); - memcpy(DEST_RAMP(1), SRC_RAMP(green), GAMMA_RAMP_SIZE * sizeof(uint16_t)); - memcpy(DEST_RAMP(2), SRC_RAMP(blue), GAMMA_RAMP_SIZE * sizeof(uint16_t)); - -#undef SRC_RAMP -#undef DEST_RAMP - - free(gamma_reply); - return TRUE; -} - - - -/** - * Get the context for a device - * - * @param lpszDriver The driver or a display device, use "DISPLAY" if you want a display - * @param lpszDevice The name of the device. If you want a display use can use the member - * name `DeviceName` in the third parameter, an output parameter, of - * `EnumDisplayDevices` - * @param lpszOutput We will always use `NULL` here - * @param lpInitData We will always use `NULL` here; this should actually by a `const DEVMODE *` - * @return The context for the device - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183490(v=vs.85).aspx - */ -HDC -CreateDC(LPCTSTR restrict lpszDriver, LPCTSTR restrict lpszDevice, LPCTSTR restrict lpszOutput, const void *restrict lpInitData) -{ - int crtc_index = atoi(lpszDevice); - xcb_generic_error_t *error; - xcb_screen_iterator_t iter; - xcb_randr_get_screen_resources_current_cookie_t res_cookie; - - (void) lpszOutput; - (void) lpInitData; - - /* Check correctness of input */ - if (strcmp(lpszDriver, "DISPLAY")) - return NULL; - - /* Connect to the display and get screen data if not already done so */ - if (!dc_count) { - /* Connect to the display */ - connection = xcb_connect(NULL, NULL); - /* Get the first screen */ - iter = xcb_setup_roots_iterator(xcb_get_setup(connection)); - /* Get the resources of the screen */ - res_cookie = xcb_randr_get_screen_resources_current(connection, iter.data->root); - res_reply = xcb_randr_get_screen_resources_current_reply(connection, res_cookie, &error); - if (error) { - fprintf(stderr, "Failed to open X connection\n"); - xcb_disconnect(connection); - crtc_count = -1; - return NULL; - } - - /* Get the number of CRTC:s */ - crtc_count = res_reply->num_crtcs; - /* Get the CRTC ID:s */ - crtcs = xcb_randr_get_screen_resources_current_crtcs(res_reply); - } - - /* Was the index too high */ - if (crtc_index >= crtc_count) { - /* Disconnect and release resouces and mark that - we do not know the number of available CRTC:s - if we have not opened any monitors yet */ - if (!dc_count) { - xcb_disconnect(connection); - free(res_reply); - res_reply = NULL; - crtc_count = -1; - } - return NULL; - } - - /* We have opened a new CRTC */ - dc_count++; - /* Return the ID of the CRTC */ - return &crtcs[crtc_index]; -} - - -#ifdef __GNUC__ -# pragma GCC diagnostic pop -#endif - - -/** - * Get a display device by its name or index - * - * @param lpDevice The name of the device, use `NULL` to base the call on `iDevNum` instead - * @param iDevNum The index of the device - * @param lpDisplayDevice Output for the found device - * @param dwFlags Flags, we will always use zero - * @return Whether the call was successful; zero is also returned if `iDevNum` - * is greater than the largest device index on the system - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd162609(v=vs.85).aspx - */ -BOOL -EnumDisplayDevices(LPCTSTR restrict lpDevice, DWORD iDevNum, PDISPLAY_DEVICE restrict lpDisplayDevice, DWORD dwFlags) -{ - size_t count = (size_t)crtc_count; - - (void) dwFlags; - - /* Check the correctness of the input */ - if (lpDevice) { - fprintf(stderr, "lpDevice (argument 1) for EnumDisplayDevices should be NULL\n"); - abort(); - return FALSE; - } - /* Do we know how many CRTC:s are available? */ - if (crtc_count < 0) { - /* If not open the first CRTC so that will be figured out */ - if (!GetDC(NULL)) - return FALSE; - count = (size_t)crtc_count; - /* Close the primary monitor that we just closed */ - ReleaseDC(NULL, NULL); - } - /* Check that the request CRTC exists */ - if (iDevNum >= count) - return FALSE; - /* Check the correctness of the input */ - if (lpDisplayDevice->cb != sizeof(DISPLAY_DEVICE)) { - fprintf(stderr, "lpDisplayDevice->cb for EnumDisplayDevices is not sizeof(DISPLAY_DEVICE)\n"); - abort(); - return FALSE; - } - /* Store name for the CRTC */ - sprintf(lpDisplayDevice->DeviceName, "%i", iDevNum); - /* The connector that the CRTC belongs to is enabled */ - lpDisplayDevice->StateFlags = DISPLAY_DEVICE_ACTIVE; - return TRUE; -} - - -#endif diff --git a/src/lib/fake-w32-gdi.h b/src/lib/fake-w32-gdi.h deleted file mode 100644 index bcc2fa9..0000000 --- a/src/lib/fake-w32-gdi.h +++ /dev/null @@ -1,248 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_FAKE_W32_GDI_H -#define LIBGAMMA_FAKE_W32_GDI_H - -#ifndef FAKE_LIBGAMMA_METHOD_W32_GDI -# error Including fake-w32-gdi.h without FAKE_LIBGAMMA_METHOD_W32_GDI -#endif - - -#ifndef __GNUC__ -# define __attribute__(x) -#endif - -#include - - -/** - * One of the Windows `typedef`:s for unsigned 16-bit integer - * This is the one used in these functions - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#WORD - */ -typedef uint16_t WORD; - -/** - * One of the Windows `typedef`:s for unsigned 32-bit integer - * This is the one used in these functions - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#DWORD - */ -typedef uint32_t DWORD; - -/** - * One of the Windows `typedef` for booleanic values, the `int` variant - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#BOOL - */ -typedef int BOOL; - -/** - * Windows `typedef` for a device context (the 'h' stands for 'handle') - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#HDC - */ -typedef void *HDC; - -/** - * Windows `typedef` for a window (the 'h' stands for 'handle') - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#HWND - */ -typedef void *HWND; - -/** - * One of the Windows `typedef`:s for `void *`, a generic pointer - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#LPVOID - */ -typedef void *LPVOID; - -/** - * A silly Windows `typedef` for a `const char *`, a constant string - * - * It is also defined as NUL-terminated, but so are all C-strings - * and it makes no difference. However if `UNICODE` were to be defined - * it would be `const wchar_t *`. - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#LPCTSTR - */ -typedef const char *LPCTSTR; - -/** - * Even more silly. A Windows `typedef` for either `wchar_t` or - * `char`, depending on whether `UNICODE` is defined (we will - * assume `UNICODE` is not definied because that is just silly) - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#TCHAR - */ -typedef char TCHAR; - -/** - * Apperently we need `TRUE` to be defined to understand code, - * at least in Windows... - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#BOOL - */ -#define TRUE 1 - -/** - * Apperently we need `FALSE` to be defined to understand code, - * at least in Windows... - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx#BOOL - */ -#define FALSE 0 - - - -/** - * Get the device context for a window or the entire screen - * - * @param hWnd The window, `NULL` for the entire screen - * @return The device context - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144871(v=vs.85).aspx - */ -HDC GetDC(HWND); - -/** - * Free a device context - * - * @param hWnd The window whose device context is `hDC`, `NULL` for the entire screen - * @param hDC The device context to free - * @return Whether the call was successful - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd162920(v=vs.85).aspx - */ -int ReleaseDC(HWND, HDC); - - -/** - * Get information (capabilities) for a device context - * - * @param hDC The device context - * @param nIndex The information to retrieve, may be `COLORMGMTCAPS` - * @return The details of the queried information, can return `CM_GAMMA_RAMP` - * if `nIndex` is `COLORMGMTCAPS` - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877(v=vs.85).aspx - */ -int GetDeviceCaps(HDC, int) __attribute__((const)); - -/** - * Colour management capabilities - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877(v=vs.85).aspx - */ -#define COLORMGMTCAPS 1 - -/** - * Colour management capabilities: has gamma ramps - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877(v=vs.85).aspx - */ -#define CM_GAMMA_RAMP 1 - - -/** - * Set the gamma ramps for a device - * - * @param hDC The device context - * @param lpRamp The gamma ramps joined in the order: red, green, blue; - * this is a `WORD *` casted to `void *` - * @return Whether the call was successful - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd372194(v=vs.85).aspx - */ -BOOL SetDeviceGammaRamp(HDC, LPVOID restrict); - -/** - * Get the current gamma ramps for a device - * - * @param hDC The device context - * @param lpRamp The output for the gamma ramps joined in the order: red, green, blue - * This is a `WORD *` casted to `void *` - * @return Whether the call was successful - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd316946(v=vs.85).aspx - */ -BOOL GetDeviceGammaRamp(HDC, LPVOID restrict); - - -/** - * Get the context for a device - * - * @param lpszDriver The driver or a display device, use "DISPLAY" if you want a display - * @param lpszDevice The name of the device. If you want a display use can use the member - * name `DeviceName` in the third parameter, an output parameter, of - * `EnumDisplayDevices`. - * @param lpszOutput We will always use `NULL` here - * @param lpInitData We will always use `NULL` here; this should actually by a `const DEVMODE *` - * @return The context for the device - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183490(v=vs.85).aspx - */ -HDC CreateDC(LPCTSTR restrict, LPCTSTR restrict, LPCTSTR restrict, const void *restrict); - -/** - * This macro does not seem to have an official documentation that is correct - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183490(v=vs.85).aspx - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd374074(v=vs.85).aspx - */ -#define TEXT(X) ((LPCTSTR)(X)) - - -/** - * Information about a display device - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183569(v=vs.85).aspx - */ -typedef struct { - /** - * Set this to `sizeof(DISPLAY_DEVICE)` - */ - DWORD cb; - - /** - * The name of the device - */ - TCHAR DeviceName[32]; - - /** - * The status of the device, can include `DISPLAY_DEVICE_ACTIVE` - */ - DWORD StateFlags; - -} DISPLAY_DEVICE; - -/** - * Appearently we are incapable of putting asterisks at the - * end of types names, so istead we preprend them with 'P' - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183569(v=vs.85).aspx - */ -typedef DISPLAY_DEVICE *PDISPLAY_DEVICE; - -/** - * The monitor is "on" - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd183569(v=vs.85).aspx - */ -#define DISPLAY_DEVICE_ACTIVE 1 - -/** - * Get a display device by its name or index - * - * @param lpDevice The name of the device, use `NULL` to base the call on `iDevNum` instead - * @param iDevNum The index of the device - * @param lpDisplayDevice Output for the found device - * @param dwFlags Flags, we will always use zero - * @return Whether the call was successful; zero is also returned if `iDevNum` - * is greater than the largest device index on the system - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd162609(v=vs.85).aspx - */ -BOOL EnumDisplayDevices(LPCTSTR restrict, DWORD, PDISPLAY_DEVICE restrict, DWORD) - - - -#ifndef __GNUC__ -# undef __attribute__ -#endif - -#endif diff --git a/src/lib/gamma-dummy.c.gpp b/src/lib/gamma-dummy.c.gpp deleted file mode 100644 index a9aa724..0000000 --- a/src/lib/gamma-dummy.c.gpp +++ /dev/null @@ -1,993 +0,0 @@ -/* -*- c -*- */ -/* See LICENSE file for copyright and license details. */ -#ifndef HAVE_LIBGAMMA_METHOD_DUMMY -# error Compiling gamma-dummy.c without HAVE_LIBGAMMA_METHOD_DUMMY -#endif - -#include "gamma-dummy.h" - -#include "libgamma-error.h" -#include "libgamma-method.h" -#include "libgamma-facade.h" -#include "edid.h" -#include "gamma-helper.h" - -#include -#include -#include -#include - - - -/** - * Configuration set for the dummy adjustment method - */ -typedef struct libgamma_dummy_configurations { - /** - * The method's capabilities - * - * Some fields are ignored: - * - real - * - fake - */ - libgamma_method_capabilities_t capabilities; - - /** - * Template for CRTC:s information - * - * Some fields are ignored: - * - width_mm_edid - * - width_mm_edid_error - * - height_mm_edid - * - height_mm_edid_error - * - gamma_red - * - gamma_green - * - gamma_blue - * - gamma_error - */ - libgamma_crtc_information_t crtc_info_template; - - /** - * The adjustment method to use - */ - int real_method; - - /** - * The number of sites on the system - */ - size_t site_count; - - /** - * The number of paritions on a site before it has been configured - */ - size_t default_partition_count; - - /** - * The number of CRTC:s on a paritions before it has been configured - */ - size_t default_crtc_count; - - /** - * Whether the sites should be inherited from the real method - */ - unsigned inherit_sites : 1; - - /** - * Whether the partitions should be inherited from the real method - */ - unsigned inherit_partition_count : 1; - - /** - * Whether the CRTC:s should be inherited from the real method - */ - unsigned inherit_crtc_count : 1; - - /** - * When a site has been created, stall until the partition count has - * been configured - */ - unsigned stall_for_partition_count : 1; - - /** - * When a parition has been created, stall until the CRTC count has - * been configured - */ - unsigned stall_for_crtc_count : 1; - - /** - * Methods should stall until the system has been configured - * unless $LIBGAMMA_DUMMY_STALL is not true - */ - unsigned stalled_start : 1; - - /** - * Whether to print what is going on in the phony system - */ - unsigned verbose : 1; - -} libgamma_dummy_configurations_t; - - -/** - * Dummy adjustment method internal data for a CRTC - */ -typedef struct libgamma_dummy_crtc { - /** - * The gamma ramp for the red channel - */ - void *restrict gamma_red; - - /** - * The gamma ramp for the green channel - */ - void *restrict gamma_green; - - /** - * The gamma ramp for the blue channel - */ - void *restrict gamma_blue; - - /** - * Information about the CRTC and monitor - * - * Some fields are ignored: - * - width_mm_edid - * - width_mm_edid_error - * - height_mm_edid - * - height_mm_edid_error - * - gamma_red - * - gamma_green - * - gamma_blue - * - gamma_error - */ - libgamma_crtc_information_t info; - - /** - * Partition state that contains this information - */ - libgamma_crtc_state_t *state; - -} libgamma_dummy_crtc_t; - - -/** - * Dummy adjustment method internal data for a partition - */ -typedef struct libgamma_dummy_partition { - /** - * The CRTC:s on the system - */ - libgamma_dummy_crtc_t *crtcs; - - /** - * The number of CRTC:s on the system - */ - size_t crtc_count; - - /** - * Partition state that contains this information - */ - libgamma_partition_state_t *state; - -} libgamma_dummy_partition_t; - - -/** - * Dummy adjustment method internal data for a site - */ -typedef struct libgamma_dummy_site { - /** - * The partitions on the system - */ - libgamma_dummy_partition_t *partitions; - - /** - * The number of partitions on the system - */ - size_t partition_count; - - /** - * Site state that contains this information - */ - libgamma_site_state_t *state; - -} libgamma_dummy_site_t; - - - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * and ignore the method's capabilities - * - * @param this The CRTC data - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -static int libgamma_dummy_crtc_restore_forced(libgamma_dummy_crtc_t *restrict data); - - - -/** - * Configurations for the dummy adjustment method. - */ -static libgamma_dummy_configurations_t libgamma_dummy_configurations = { - .capabilities = { - .crtc_information = (1 << LIBGAMMA_CRTC_INFO_COUNT) - 1, - .default_site_known = 1, - .multiple_sites = 1, - .multiple_partitions = 1, - .multiple_crtcs = 1, - .partitions_are_graphics_cards = 1, - .site_restore = 1, - .partition_restore = 1, - .crtc_restore = 1, - .identical_gamma_sizes = 0, - .fixed_gamma_size = 0, - .fixed_gamma_depth = 0 - }, - .crtc_info_template = { - .edid = NULL, - .edid_length = 0, - .edid_error = LIBGAMMA_EDID_NOT_FOUND, - .width_mm = 400, - .width_mm_error = 0, - .height_mm = 300, - .height_mm_error = 0, - .red_gamma_size = 1024, - .green_gamma_size = 2048, - .blue_gamma_size = 512, - .gamma_size_error = 0, - .gamma_depth = 64, - .gamma_depth_error = 0, - .gamma_support = 1, - .gamma_support_error = 0, - .subpixel_order = LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_RGB, - .subpixel_order_error = 0, - .active = 1, - .active_error = 0, - .connector_name = NULL, - .connector_name_error = LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED, - .connector_type = LIBGAMMA_CONNECTOR_TYPE_Unknown, - .connector_type_error = 0 - }, - .real_method = LIBGAMMA_METHOD_DUMMY, - .site_count = 2, - .default_partition_count = 2, - .default_crtc_count = 2, - .inherit_sites = 1, - .inherit_partition_count = 1, - .inherit_crtc_count = 1, - .stall_for_partition_count = 0, - .stall_for_crtc_count = 0, - .stalled_start = 1, - .verbose = 0 -}; - - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void -libgamma_dummy_method_capabilities(libgamma_method_capabilities_t *restrict this) -{ - int real_method = libgamma_dummy_configurations.real_method; - *this = libgamma_dummy_configurations.capabilities; - this->real = real_method != LIBGAMMA_METHOD_DUMMY; - this->fake = this->real; - this->auto_restore = real_method == LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS; -} - - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_dummy_site_initialise(libgamma_site_state_t *restrict this, char *restrict site) -{ - libgamma_dummy_site_t *data = NULL; - size_t i, sites, crtcs; - - sites = libgamma_dummy_configurations.site_count; - if (!libgamma_dummy_configurations.capabilities.multiple_sites) - sites = !!sites; - this->data = NULL; - - if (site && *site && (atoll(site) < 0 || sites <= (unsigned long long)atoll(site))) - return LIBGAMMA_NO_SUCH_SITE; - - data = malloc(sizeof(libgamma_dummy_site_t)); - if (!data) - goto fail; - - this->data = data; - data->state = this; - - data->partition_count = libgamma_dummy_configurations.default_partition_count; - if (!libgamma_dummy_configurations.capabilities.multiple_partitions) - data->partition_count = !!data->partition_count; - - crtcs = libgamma_dummy_configurations.default_crtc_count; - if (!libgamma_dummy_configurations.capabilities.multiple_crtcs) - crtcs = !!crtcs; - - data->partitions = malloc(data->partition_count * sizeof(libgamma_dummy_partition_t)); - if (!data->partitions) - goto fail; - - for (i = 0; i < data->partition_count; i++) - data->partitions[i].crtc_count = crtcs; - - this->partitions_available = data->partition_count; - - return 0; - -fail: - free(data); - this->data = NULL; - return LIBGAMMA_ERRNO_SET; -} - - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void -libgamma_dummy_site_destroy(libgamma_site_state_t *restrict this) -{ - libgamma_dummy_site_t *data = this->data; - if (!data) - return; - - free(data->partitions); - free(data); -} - - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_dummy_site_restore(libgamma_site_state_t *restrict this) -{ - libgamma_dummy_site_t *data = this->data; - size_t i, j; - - if (!libgamma_dummy_configurations.capabilities.site_restore) { - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; - } - - for (j = 0; j < data->partition_count; j++) - for (i = 0; i < data->partitions[j].crtc_count; i++) - if (libgamma_dummy_crtc_restore_forced(data->partitions[j].crtcs + i) < 0) - return -1; - - return 0; -} - - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_dummy_partition_initialise(libgamma_partition_state_t *restrict this, - libgamma_site_state_t *restrict site, size_t partition) -{ - libgamma_crtc_information_t template = libgamma_dummy_configurations.crtc_info_template; - libgamma_dummy_site_t *site_data = site->data; - libgamma_dummy_partition_t *data = &site_data->partitions[partition]; - libgamma_dummy_crtc_t *crtc_data; - size_t i, n; - - this->data = NULL; - - if (partition >= site_data->partition_count) - return LIBGAMMA_NO_SUCH_PARTITION; - - this->data = data; - data->state = this; - - data->crtcs = calloc(data->crtc_count, sizeof(libgamma_dummy_crtc_t)); - if (!data->crtcs) - goto fail; - for (i = 0; i < data->crtc_count; i++) { - crtc_data = data->crtcs + i; - crtc_data->info = template; - - /* Duplicate strings */ - if (crtc_data->info.edid) { - crtc_data->info.edid = malloc(crtc_data->info.edid_length * sizeof(char)); - if (!crtc_data->info.edid) - goto fail; - memcpy(crtc_data->info.edid, template.edid, crtc_data->info.edid_length * sizeof(char)); - } - if (crtc_data->info.connector_name) { - n = strlen(crtc_data->info.connector_name); - crtc_data->info.connector_name = malloc((n + 1) * sizeof(char)); - if (crtc_data->info.connector_name == NULL) - goto fail; - memcpy(crtc_data->info.connector_name, template.connector_name, (n + 1) * sizeof(char)); - } - } - - this->crtcs_available = data->crtc_count; - - return 0; - -fail: - for (i = 0; i < data->crtc_count; i++) { - free(data->crtcs[i].info.edid); - free(data->crtcs[i].info.connector_name); - } - free(data->crtcs); - data->crtcs = NULL; - return LIBGAMMA_ERRNO_SET; -} - - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void -libgamma_dummy_partition_destroy(libgamma_partition_state_t *restrict this) -{ - libgamma_dummy_partition_t *data = this->data; - size_t i; - - if (data) { - for (i = 0; i < data->crtc_count; i++) { - free(data->crtcs[i].info.edid); - free(data->crtcs[i].info.connector_name); - } - free(data->crtcs); - data->crtcs = NULL; - } -} - - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_dummy_partition_restore(libgamma_partition_state_t *restrict this) -{ - libgamma_dummy_partition_t *data = this->data; - size_t i; - - if (!libgamma_dummy_configurations.capabilities.partition_restore) { - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; - } - - for (i = 0; i < data->crtc_count; i++) - if (libgamma_dummy_crtc_restore_forced(data->crtcs + i) < 0) - return -1; - - return 0; -} - - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_dummy_crtc_initialise(libgamma_crtc_state_t *restrict this, - libgamma_partition_state_t *restrict partition, size_t crtc) -{ - libgamma_dummy_partition_t *partition_data = partition->data; - libgamma_dummy_crtc_t *data = &partition_data->crtcs[crtc]; - size_t stop_size; - - this->data = NULL; - - if (crtc >= partition_data->crtc_count) - return LIBGAMMA_NO_SUCH_CRTC; - - this->data = data; - data->state = this; - - if (data->info.gamma_depth == -1) - stop_size = sizeof(float); - else if (data->info.gamma_depth == -2) - stop_size = sizeof(double); - else - stop_size = (size_t)data->info.gamma_depth / 8; - - data->gamma_red = malloc(data->info.red_gamma_size * stop_size); - if (!data->gamma_red) - goto fail; - data->gamma_green = malloc(data->info.green_gamma_size * stop_size); - if (!data->gamma_green) - goto fail; - data->gamma_blue = malloc(data->info.blue_gamma_size * stop_size); - if (!data->gamma_blue) - goto fail; - - return libgamma_dummy_crtc_restore_forced(data); - -fail: - free(data->gamma_red), data->gamma_red = NULL; - free(data->gamma_green), data->gamma_green = NULL; - free(data->gamma_blue), data->gamma_blue = NULL; - return LIBGAMMA_ERRNO_SET; -} - - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void -libgamma_dummy_crtc_destroy(libgamma_crtc_state_t *restrict this) -{ - libgamma_dummy_crtc_t *data = this->data; - if (data) { - free(data->gamma_red), data->gamma_red = NULL; - free(data->gamma_green), data->gamma_green = NULL; - free(data->gamma_blue), data->gamma_blue = NULL; - } -} - - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_dummy_crtc_restore(libgamma_crtc_state_t *restrict this) -{ - if (!libgamma_dummy_configurations.capabilities.crtc_restore) { - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; - } - return libgamma_dummy_crtc_restore_forced(this->data); -} - - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * and ignore the method's capabilities - * - * @param this The CRTC data - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -static int -libgamma_dummy_crtc_restore_forced(libgamma_dummy_crtc_t *restrict data) -{ - size_t rn = data->info. red_gamma_size; - size_t gn = data->info.green_gamma_size; - size_t bn = data->info. blue_gamma_size; - size_t i; - - if (!data->gamma_red) - return 0; - -#define __reset_ramps(TYPE, MAX)\ - do {\ - TYPE *red = data->gamma_red;\ - TYPE *green = data->gamma_green;\ - TYPE *blue = data->gamma_blue;\ - for (i = 0; i < rn; i++) red [i] = (TYPE)((double)(MAX) * ((double)i / (double)(rn - 1)));\ - for (i = 0; i < gn; i++) green[i] = (TYPE)((double)(MAX) * ((double)i / (double)(gn - 1)));\ - for (i = 0; i < bn; i++) blue [i] = (TYPE)((double)(MAX) * ((double)i / (double)(bn - 1)));\ - } while (0) - - if (data->info.gamma_depth == 8) __reset_ramps(uint8_t, INT8_MAX); - else if (data->info.gamma_depth == 16) __reset_ramps(uint16_t, INT16_MAX); - else if (data->info.gamma_depth == 32) __reset_ramps(uint32_t, INT32_MAX); - else if (data->info.gamma_depth == 64) __reset_ramps(uint64_t, INT64_MAX); - else if (data->info.gamma_depth == -1) __reset_ramps(float, 1); - else __reset_ramps(double, 1); - -#undef __reset_ramps - - return 0; -} - - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error. On error refer to the error reports in `this` - */ -int -libgamma_dummy_get_crtc_information(libgamma_crtc_information_t *restrict this, - libgamma_crtc_state_t *restrict crtc, int32_t fields) -{ - libgamma_dummy_crtc_t *restrict data = crtc->data; - int supported = libgamma_dummy_configurations.capabilities.crtc_information; - int e = 0; - size_t n; - - /* Copy over information */ - *this = data->info; - - /* Duplicate strings */ - if (this->edid) { - this->edid = malloc(this->edid_length * sizeof(char)); - if (!this->edid) - this->edid_error = errno; - memcpy(this->edid, data->info.edid, this->edid_length * sizeof(char)); - } - if (this->connector_name) { - n = strlen(this->connector_name); - this->connector_name = malloc((n + 1) * sizeof(char)); - if (!this->connector_name) - this->connector_name_error = errno; - memcpy(this->connector_name, data->info.connector_name, (n + 1) * sizeof(char)); - } - - /* Parse EDID */ - if (this->edid_error) - this->width_mm_edid_error = this->height_mm_edid_error = this->gamma_error = this->edid_error; - else if (fields & (LIBGAMMA_CRTC_INFO_MACRO_EDID ^ LIBGAMMA_CRTC_INFO_EDID)) - e |= libgamma_parse_edid(this, fields); - - /* Test errors */ -#define _E(FIELD, VAR)\ - ((fields & FIELD) ? ((supported & FIELD) ? VAR : (VAR = LIBGAMMA_CRTC_INFO_NOT_SUPPORTED)) : 0) - e |= _E(LIBGAMMA_CRTC_INFO_EDID, this->edid_error); - e |= _E(LIBGAMMA_CRTC_INFO_WIDTH_MM, this->width_mm_error); - e |= _E(LIBGAMMA_CRTC_INFO_HEIGHT_MM, this->height_mm_error); - e |= _E(LIBGAMMA_CRTC_INFO_GAMMA_SIZE, this->gamma_size_error); - e |= _E(LIBGAMMA_CRTC_INFO_GAMMA_DEPTH, this->gamma_depth_error); - e |= _E(LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT, this->gamma_support_error); - e |= _E(LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER, this->subpixel_order_error); - e |= _E(LIBGAMMA_CRTC_INFO_ACTIVE, this->active_error); - e |= _E(LIBGAMMA_CRTC_INFO_CONNECTOR_NAME, this->connector_name_error); - e |= _E(LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE, this->connector_type_error); - - if ((fields & LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID) && !(supported & LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID)) - e |= this->width_mm_edid_error = LIBGAMMA_CRTC_INFO_NOT_SUPPORTED; - if ((fields & LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID) && !(supported & LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID)) - e |= this->height_mm_edid_error = LIBGAMMA_CRTC_INFO_NOT_SUPPORTED; - if ((fields & LIBGAMMA_CRTC_INFO_GAMMA) && !(supported & LIBGAMMA_CRTC_INFO_GAMMA)) - e |= this->gamma_error = LIBGAMMA_CRTC_INFO_NOT_SUPPORTED; -#undef _E - - return e ? -1 : 0; -} - - -/** - * Get the current gamma ramps for a CRTC - * - * @param 1 The data type for the ramp stop elements - * @param 2 The `ramp*` pattern for the ramp structure and function to call. - * @param 3 Either of `bit8`, `bit16`, `bit32`, `bit64`, `float_single`, `float_double`; - * rather self-explanatory - * @param 4 The number of bits in the gamma depth, -1 for single precision float, - * (`float`) and -2 for double percition float (`double`) - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_get_gamma_ramps () -$>{ -int -libgamma_dummy_crtc_get_gamma_${2}(libgamma_crtc_state_t *restrict this, libgamma_gamma_${2}_t *restrict ramps) -{ - libgamma_dummy_crtc_t *data = this->data; - libgamma_gamma_ramps_any_t ramps_; - ${1}* r_ramp = data->gamma_red; - ${1}* g_ramp = data->gamma_green; - ${1}* b_ramp = data->gamma_blue; - size_t rn = data->info.red_gamma_size; - size_t gn = data->info.green_gamma_size; - size_t bn = data->info.blue_gamma_size; - size_t i; - -#ifdef DEBUG - /* Check gamma ramp sizes */ - if (libgamma_dummy_configurations.capabilities.identical_gamma_sizes) - if (ramps->red_size != ramps->green_size || ramps->red_size != ramps->blue_size) - return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; - if (ramps->red_size != rn || ramps->green_size != gn || ramps->blue_size != bn) - return LIBGAMMA_WRONG_GAMMA_RAMP_SIZE; -#endif - - if (!data->info.gamma_support) - return LIBGAMMA_GAMMA_RAMP_READ_FAILED; - -#define __trans(DEPTH, SUFFIX)\ - do {\ - if (data->info.gamma_depth == DEPTH) {\ - ramps_.${3} = *ramps;\ - return libgamma_translated_ramp_get(this, &ramps_, ${4}, DEPTH, libgamma_crtc_get_gamma_ramps ## SUFFIX);\ - }\ - } while (0) - -$>if [ ! ${4} = 8 ]; then - __trans(8, 8); -$>fi -$>if [ ! ${4} = 16 ]; then - __trans(16, 16); -$>fi -$>if [ ! ${4} = 32 ]; then - __trans(32, 32); -$>fi -$>if [ ! ${4} = 64 ]; then - __trans(64, 64); -$>fi -$>if [ ! ${4} = -1 ]; then - __trans(-1, f); -$>fi -$>if [ ! ${4} = -2 ]; then - __trans(-2, d); -$>fi - -#undef __trans - - for (i = 0; i < rn; i++) ramps->red[i] = r_ramp[i]; - for (i = 0; i < gn; i++) ramps->green[i] = g_ramp[i]; - for (i = 0; i < bn; i++) ramps->blue[i] = b_ramp[i]; - - return 0; -} -$>} - - -/** - * Set the gamma ramps for a CRTC - * - * @param 1 The data type for the ramp stop elements - * @param 2 The `ramp*` pattern for the ramp structure and function to call. - * @param 3 Either of `bit8`, `bit16`, `bit32`, `bit64`, `float_single`, `float_double`; - * rather self-explanatory - * @param 4 The number of bits in the gamma depth, -1 for single precision float, - * (`float`) and -2 for double percition float (`double`) - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_set_gamma_ramps () -$>{ -int -libgamma_dummy_crtc_set_gamma_${2}(libgamma_crtc_state_t *restrict this, libgamma_gamma_${2}_t ramps) -{ - libgamma_dummy_crtc_t *data = this->data; - libgamma_gamma_ramps_any_t ramps_; - ${1} *r_ramp = data->gamma_red; - ${1} *g_ramp = data->gamma_green; - ${1} *b_ramp = data->gamma_blue; - size_t rn = data->info.red_gamma_size; - size_t gn = data->info.green_gamma_size; - size_t bn = data->info.blue_gamma_size; - size_t i; - -#ifdef DEBUG - /* Check gamma ramp sizes */ - if (libgamma_dummy_configurations.capabilities.identical_gamma_sizes) - if (ramps.red_size != ramps.green_size || ramps.red_size != ramps.blue_size) - return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; - if (ramps.red_size != rn || ramps.green_size != gn || ramps.blue_size != bn) - return LIBGAMMA_WRONG_GAMMA_RAMP_SIZE; -#endif - - if (!data->info.gamma_support) - return LIBGAMMA_GAMMA_RAMP_READ_FAILED; - -#define __trans(DEPTH, SUFFIX)\ - do {\ - if (data->info.gamma_depth == DEPTH) {\ - ramps_.${3} = ramps;\ - return libgamma_translated_ramp_set(this, ramps_, ${4}, DEPTH, libgamma_crtc_set_gamma_ramps ## SUFFIX);\ - }\ - } while (0) - -$>if [ ! ${4} = 8 ]; then - __trans(8, 8); -$>fi -$>if [ ! ${4} = 16 ]; then - __trans(16, 16); -$>fi -$>if [ ! ${4} = 32 ]; then - __trans(32, 32); -$>fi -$>if [ ! ${4} = 64 ]; then - __trans(64, 64); -$>fi -$>if [ ! ${4} = -1 ]; then - __trans(-1, f); -$>fi -$>if [ ! ${4} = -2 ]; then - __trans(-2, d); -$>fi - -#undef __trans - - for (i = 0; i < rn; i++) r_ramp[i] = ramps.red[i]; - for (i = 0; i < gn; i++) g_ramp[i] = ramps.green[i]; - for (i = 0; i < bn; i++) b_ramp[i] = ramps.blue[i]; - - return 0; -} -$>} - - - -/** - * Get the current gamma ramps for a CRTC, 8-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_get_gamma_ramps uint8_t ramps8 bits8 8 - - -/** - * Set the gamma ramps for a CRTC, 8-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_set_gamma_ramps uint8_t ramps8 bits8 8 - - - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_get_gamma_ramps uint16_t ramps16 bits16 16 - - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_set_gamma_ramps uint16_t ramps16 bits16 16 - - - -/** - * Get the current gamma ramps for a CRTC, 32-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_get_gamma_ramps uint32_t ramps32 bits32 32 - - -/** - * Set the gamma ramps for a CRTC, 32-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_set_gamma_ramps uint32_t ramps32 bits32 32 - - - -/** - * Get the current gamma ramps for a CRTC, 64-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_get_gamma_ramps uint64_t ramps64 bits64 64 - - -/** - * Set the gamma ramps for a CRTC, 64-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_set_gamma_ramps uint64_t ramps64 bits64 64 - - - -/** - * Get the current gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_get_gamma_ramps float rampsf float_single -1 - - -/** - * Set the gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_set_gamma_ramps float rampsf float_single -1 - - - -/** - * Get the current gamma ramps for a CRTC, `double` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_get_gamma_ramps double rampsd float_double -2 - - -/** - * Set the gamma ramps for a CRTC, `double` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>libgamma_dummy_crtc_set_gamma_ramps double rampsd float_double -2 diff --git a/src/lib/gamma-dummy.h b/src/lib/gamma-dummy.h deleted file mode 100644 index a52b087..0000000 --- a/src/lib/gamma-dummy.h +++ /dev/null @@ -1,243 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_GAMMA_DUMMY_H -#define LIBGAMMA_GAMMA_DUMMY_H - -#ifndef HAVE_LIBGAMMA_METHOD_DUMMY -# error Including gamma-dummy.h without HAVE_LIBGAMMA_METHOD_DUMMY -#endif - - -#include "libgamma-method.h" - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void libgamma_dummy_method_capabilities(libgamma_method_capabilities_t *restrict); - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_site_initialise(libgamma_site_state_t *restrict, char *restrict); - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void libgamma_dummy_site_destroy(libgamma_site_state_t *restrict); - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_site_restore(libgamma_site_state_t *restrict); - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void libgamma_dummy_partition_destroy(libgamma_partition_state_t *restrict); - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_partition_restore(libgamma_partition_state_t *restrict); - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void libgamma_dummy_crtc_destroy(libgamma_crtc_state_t *restrict); - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_restore(libgamma_crtc_state_t *restrict); - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error; on error refer to the error reports in `this` - */ -int libgamma_dummy_get_crtc_information(libgamma_crtc_information_t *restrict, libgamma_crtc_state_t *restrict, int32_t); - -/** - * Get the current gamma ramps for a CRTC, 8-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_get_gamma_ramps8(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps8_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 8-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_set_gamma_ramps8(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps8_t); - - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t); - - -/** - * Get the current gamma ramps for a CRTC, 32-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_get_gamma_ramps32(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps32_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 32-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_set_gamma_ramps32(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps32_t); - - -/** - * Get the current gamma ramps for a CRTC, 64-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_get_gamma_ramps64(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps64_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 64-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_set_gamma_ramps64(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps64_t); - - -/** - * Get the current gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_get_gamma_rampsf(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsf_t *restrict); - -/** - * Set the gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_set_gamma_rampsf(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsf_t); - -/** - * Get the current gamma ramps for a CRTC, `double` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_get_gamma_rampsd(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsd_t *restrict); - -/** - * Set the gamma ramps for a CRTC, `double` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_dummy_crtc_set_gamma_rampsd(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsd_t); - - -#endif diff --git a/src/lib/gamma-helper.c b/src/lib/gamma-helper.c deleted file mode 100644 index f67e107..0000000 --- a/src/lib/gamma-helper.c +++ /dev/null @@ -1,372 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include "gamma-helper.h" - -#include "libgamma-method.h" -#include "libgamma-error.h" - -#include -#include -#include - - -/** - * Just an arbitrary version - */ -#define ANY bits64 - -/** - * Concatenation of all ramps - */ -#define ALL red - - -/** - * Preform installation in an `for (i = 0; i < n; i++)` - * loop and do a `break` afterwords - */ -#define __translate(instruction) for (i = 0; i < n; i++) instruction; break - - -/** - * Convert a [0, 1] `float` to a full range `uint64_t` - * and mark sure rounding errors does not cause the - * value be 0 instead of ~0 and vice versa - * - * @param value To `float` to convert - * @return The value as an `uint64_t` - */ -static inline uint64_t -float_to_64(float value) -{ - /* TODO Which is faster? */ - -#if defined(HAVE_INT128) && __WORDSIZE == 64 - /* `__int128` is a GNU C extension, which - (because it is not ISO C) emits a warning - under -pedantic */ -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wpedantic" - - /* In GCC we can use `__int128`, this is - a signed 128-bit integer. It fits all - uint64_t values but also native values, - which is a nice because it eleminates - some overflow condition tests. It is - also more readable. */ - - /* Convert to integer */ - __int128 product = (__int128)(value * (float)UINT64_MAX); - /* Negative overflow */ - if (product > UINT64_MAX) - return UINT64_MAX; - /* Positive overflow */ - if (product < 0) - return 0; - /* Did not overflow */ - return (uint64_t)product; - -# pragma GCC diagnostic pop -#else - - /* If we are not using GCC we cannot be - sure that we have `__int128` so we have - to use `uint64_t` and perform overflow - checkes based on the input value */ - - /* Convert to integer. */ - uint64_t product = (uint64_t)(value * (float)UINT64_MAX); - /* Negative overflow, - if the input is less than 0.5 but - the output is greater then we got - -1 when we should have gotten 0 */ - if (value < 0.1f && product > 0xF000000000000000ULL) - return 0; - /* Positive overflow, - if the input is greater than 0.5 - but the output is less then we got - 0 when we should have gotten ~0 */ - else if (value > 0.9f && product < 0x1000000000000000ULL) - return (uint64_t)~0; - /* Did not overflow */ - return product; - -#endif -} - - -/** - * Convert a [0, 1] `double` to a full range `uint64_t` - * and mark sure rounding errors does not cause the - * value be 0 instead of ~0 and vice versa - * - * @param value To `double` to convert - * @return The value as an `uint64_t` - */ -static inline uint64_t -double_to_64(double value) -{ - /* XXX Which is faster? */ - -#if defined(HAVE_INT128) && __WORDSIZE == 64 - /* `__int128` is a GNU C extension, which - (because it is not ISO C) emits a warning - under -pedantic */ -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wpedantic" - - /* In GCC we can use `__int128`, this is - a signed 128-bit integer. It fits all - uint64_t values but also native values, - which is a nice because it eleminates - some overflow condition tests. It is - also more readable. */ - - /* Convert to integer */ - __int128 product = (__int128)(value * (double)UINT64_MAX); - /* Negative overflow */ - if (product > UINT64_MAX) - return UINT64_MAX; - /* Positive overflow */ - if (product < 0) - return 0; - /* Did not overflow */ - return (uint64_t)product; - -# pragma GCC diagnostic pop -#else - - /* If we are not using GCC we cannot be - sure that we have `__int128` so we have - to use `uint64_t` and perform overflow - checkes based on the input value. */ - - /* Convert to integer. */ - uint64_t product = (uint64_t)(value * (double)UINT64_MAX); - /* Negative overflow, - if the input is less than 0.5 but - the output is greater then we got - -1 when we should have gotten 0 */ - if (value < (double)0.1f && product > 0xF000000000000000ULL) - product = 0; - /* Positive overflow, - if the input is greater than 0.5 - but the output is less then we got - 0 when we should have gotten ~0 */ - else if ((value > (double)0.9f) && (product < 0x1000000000000000ULL)) - product = (uint64_t)~0; - /* Did not overflow */ - return product; - -#endif -} - - -/** - * Convert any set of gamma ramps into a 64-bit integer array with all channels - * - * @param depth The depth of the gamma ramp, `-1` for `float`, `-2` for `double` - * @param n The grand size of gamma ramps (sum of all channels' sizes) - * @param out Output array - * @param in Input gamma ramps - */ -static void -translate_to_64(signed depth, size_t n, uint64_t *restrict out, libgamma_gamma_ramps_any_t in) -{ - size_t i; - switch (depth) { - /* Translate integer */ - case 8: __translate(out[i] = (uint64_t)(in.bits8. ALL[i]) * 0x0101010101010101ULL); - case 16: __translate(out[i] = (uint64_t)(in.bits16.ALL[i]) * 0x0001000100010001ULL); - case 32: __translate(out[i] = (uint64_t)(in.bits32.ALL[i]) * 0x0000000100000001ULL); - /* Identity translation */ - case 64: __translate(out[i] = in.bits64.ALL[i]); - /* Translate floating point */ - case -1: __translate(out[i] = float_to_64(in.float_single.ALL[i])); - case -2: __translate(out[i] = double_to_64(in.float_double.ALL[i])); - default: - /* This is not possible */ - abort(); - break; - } -} - - -/** - * Undo the actions of `translate_to_64` - * - * @param depth The depth of the gamma ramp, `-1` for `float`, `-2` for `double` - * @param n The grand size of gamma ramps (sum of all channels' sizes) - * @param out Output gamma ramps - * @param in Input array, may be modified - */ -static void -translate_from_64(signed depth, size_t n, libgamma_gamma_ramps_any_t out, uint64_t *restrict in) -{ - size_t i; - switch (depth) { - /* Translate integer */ - case 8: __translate(out.bits8. ALL[i] = (uint8_t)(in[i] / 0x0101010101010101ULL)); - case 16: __translate(out.bits16.ALL[i] = (uint16_t)(in[i] / 0x0001000100010001ULL)); - case 32: __translate(out.bits32.ALL[i] = (uint32_t)(in[i] / 0x0000000100000001ULL)); - /* Identity translation */ - case 64: __translate(out.bits64.ALL[i] = in[i]); - /* Translate floating point */ - case -1: __translate(out.float_single.ALL[i] = (float)(in[i]) / (float)UINT64_MAX); - case -2: __translate(out.float_double.ALL[i] = (double)(in[i]) / (double)UINT64_MAX); - default: - /* This is not possible */ - abort(); - break; - } -} - - -/** - * Allocate and initalise a gamma ramp with any depth - * - * @param ramps_sys Output gamma ramps - * @param ramps The gamma ramps whose sizes should be duplicated - * @param depth The depth of the gamma ramps to allocate, - * `-1` for `float`, `-2` for `double` - * @param elements Output reference for the grand size of the gamma ramps - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -static int -allocated_any_ramp(libgamma_gamma_ramps_any_t *restrict ramps_sys, - libgamma_gamma_ramps_any_t ramps, signed depth, size_t *restrict elements) -{ - /* Calculate the size of the allocation to do */ - size_t d, n = ramps.ANY.red_size + ramps.ANY.green_size + ramps.ANY.blue_size; - switch (depth) { - case 8: d = sizeof(uint8_t); break; - case 16: d = sizeof(uint16_t); break; - case 32: d = sizeof(uint32_t); break; - case 64: d = sizeof(uint64_t); break; - case -1: d = sizeof(float); break; - case -2: d = sizeof(double); break; - default: - return errno = EINVAL, LIBGAMMA_ERRNO_SET; - } - - /* Copy the gamma ramp sizes */ - ramps_sys->ANY = ramps.ANY; - /* Allocate the new ramps */ -#ifdef HAVE_LIBGAMMA_METHOD_LINUX_DRM - /* Valgrind complains about us reading uninitialize memory if we just use malloc */ - ramps_sys->ANY.red = calloc(n, d); -#else - ramps_sys->ANY.red = malloc(n * d); -#endif - ramps_sys->ANY.green = (void *)&((char *)ramps_sys->ANY. red)[ramps.ANY. red_size * d / sizeof(char)]; - ramps_sys->ANY.blue = (void *)&((char *)ramps_sys->ANY.green)[ramps.ANY.green_size * d / sizeof(char)]; - - /* Report the total gamma ramp size */ - *elements = n; - /* Report successfulness */ - return ramps_sys->ANY.red ? 0 : LIBGAMMA_ERRNO_SET; -} - - -/** - * Get the current gamma ramps for a CRTC, re-encoding version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @param depth_user The depth of the gamma ramps that are provided by the user, - * `-1` for `float`, `-2` for `double` - * @param depth_system The depth of the gamma ramps as required by the adjustment method, - * `-1` for `float`, `-2` for `double` - * @param fun Function that is to be used read the ramps, its parameters have - * the same function as those of this function with the same names, - * and the return value too is identical - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_translated_ramp_get_(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps_any_t *restrict ramps, - signed depth_user, signed depth_system, libgamma_get_ramps_any_fun *fun) -{ - size_t n; - int r; - libgamma_gamma_ramps_any_t ramps_sys; - uint64_t *restrict ramps_full; - - /* Allocate ramps with proper data type */ - if ((r = allocated_any_ramp(&ramps_sys, *ramps, depth_system, &n))) - return r; - - /* Fill the ramps */ - if ((r = fun(this, &ramps_sys))) - return free(ramps_sys.ANY.red), r; - - /* Allocate intermediary ramps */ - ramps_full = malloc(n * sizeof(uint64_t)); - if (!ramps_full) { - free(ramps_sys.ANY.red); - return LIBGAMMA_ERRNO_SET; - } - - /* Translate ramps to 64-bit integers */ - translate_to_64(depth_system, n, ramps_full, ramps_sys); - free(ramps_sys.ANY.red); - - /* Translate ramps to the user's format */ - translate_from_64(depth_user, n, *ramps, ramps_full); - free(ramps_full); - return 0; -} - - -/** - * Set the gamma ramps for a CRTC, re-encoding version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @param depth_user The depth of the gamma ramps that are provided by the user, - * `-1` for `float`, `-2` for `double` - * @param depth_system The depth of the gamma ramps as required by the adjustment method, - * `-1` for `float`, `-2` for `double` - * @param fun Function that is to be used write the ramps, its parameters have - * the same function as those of this function with the same names, - * and the return value too is identical - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_translated_ramp_set_(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps_any_t ramps, - signed depth_user, signed depth_system, libgamma_set_ramps_any_fun *fun) -{ - size_t n; - int r; - libgamma_gamma_ramps_any_t ramps_sys; - uint64_t *restrict ramps_full; - - /* Allocate ramps with proper data type */ - if ((r = allocated_any_ramp(&ramps_sys, ramps, depth_system, &n))) - return r; - - /* Allocate intermediary ramps */ - ramps_full = malloc(n * sizeof(uint64_t)); - if (!ramps_full) { - free(ramps_sys.ANY.red); - return LIBGAMMA_ERRNO_SET; - } - - /* Translate ramps to 64-bit integers. */ - translate_to_64(depth_user, n, ramps_full, ramps); - /* Translate ramps to the proper format. */ - translate_from_64(depth_system, n, ramps_sys, ramps_full); - free(ramps_full); - - /* Apply the ramps */ - r = fun(this, ramps_sys); - - free(ramps_sys.ANY.red); - return r; -} - - -#undef __translate -#undef ALL -#undef ANY diff --git a/src/lib/gamma-helper.h b/src/lib/gamma-helper.h deleted file mode 100644 index 38d678d..0000000 --- a/src/lib/gamma-helper.h +++ /dev/null @@ -1,144 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_GAMMA_HELPER_H -#define LIBGAMMA_GAMMA_HELPER_H - - -#include "libgamma-method.h" - - -/** - * Gamma ramp structure union for different depths - */ -typedef union libgamma_gamma_ramps_any { - /** - * 8-bit gamma ramps - */ - libgamma_gamma_ramps8_t bits8; - - /** - * 16-bit gamma ramps - */ - libgamma_gamma_ramps16_t bits16; - - /** - * 32-bit gamma ramps - */ - libgamma_gamma_ramps32_t bits32; - - /** - * 64-bit gamma ramps - */ - libgamma_gamma_ramps64_t bits64; - - /** - * Single precision float gamma ramps - */ - libgamma_gamma_rampsf_t float_single; - - /** - * Double precision float gamma ramps - */ - libgamma_gamma_rampsd_t float_double; - -} libgamma_gamma_ramps_any_t; - - -/** - * A function for reading the gamma ramps from a CRTC - * - * @param this The CRTC state - * @param ramps The store for the gamma ramps - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -typedef int libgamma_get_ramps_any_fun(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps_any_t *restrict); - -/** - * A function for writing the gamma ramps to a CRTC - * - * @param this The CRTC state - * @param ramps The gamma ramps - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -typedef int libgamma_set_ramps_any_fun(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps_any_t); - - - -/** - * Get the current gamma ramps for a CRTC, re-encoding version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @param depth_user The depth of the gamma ramps that are provided by the user, - * `-1` for `float`, `-2` for `double` - * @param depth_system The depth of the gamma ramps as required by the adjustment method, - * `-1` for `float`, `-2` for `double` - * @param fun Function that is to be used read the ramps, its parameters have - * the same function as those of this function with the same names, - * and the return value too is identical - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -#define libgamma_translated_ramp_get(this, ramps, depth_user, depth_system, fun)\ - libgamma_translated_ramp_get_((this), (ramps), (depth_user), (depth_system), (libgamma_get_ramps_any_fun *)(fun)) - - -/** - * Set the gamma ramps for a CRTC, re-encoding version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @param depth_user The depth of the gamma ramps that are provided by the user, - * `-1` for `float`, `-2` for `double` - * @param depth_system The depth of the gamma ramps as required by the adjustment method, - * `-1` for `float`, `-2` for `double` - * @param fun Function that is to be used write the ramps, its parameters have - * the same function as those of this function with the same names, - * and the return value too is identical - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -#define libgamma_translated_ramp_set(this, ramps, depth_user, depth_system, fun)\ - libgamma_translated_ramp_set_((this), (ramps), (depth_user), (depth_system), (libgamma_set_ramps_any_fun *)fun) - - -/** - * Get the current gamma ramps for a CRTC, re-encoding version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @param depth_user The depth of the gamma ramps that are provided by the user, - * `-1` for `float`, `-2` for `double` - * @param depth_system The depth of the gamma ramps as required by the adjustment method, - * `-1` for `float`, `-2` for `double` - * @param fun Function that is to be used read the ramps, its parameters have - * the same function as those of this function with the same names, - * and the return value too is identical - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_translated_ramp_get_(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps_any_t *restrict, - signed, signed, libgamma_get_ramps_any_fun *); - - -/** - * Set the gamma ramps for a CRTC, re-encoding version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @param depth_user The depth of the gamma ramps that are provided by the user, - * `-1` for `float`, `-2` for `double` - * @param depth_system The depth of the gamma ramps as required by the adjustment method, - * `-1` for `float`, `-2` for `double` - * @param fun Function that is to be used write the ramps, its parameters have - * the same function as those of this function with the same names, - * and the return value too is identical - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_translated_ramp_set_(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps_any_t, - signed, signed, libgamma_set_ramps_any_fun *); - - -#endif diff --git a/src/lib/gamma-linux-drm.c b/src/lib/gamma-linux-drm.c deleted file mode 100644 index c2fc646..0000000 --- a/src/lib/gamma-linux-drm.c +++ /dev/null @@ -1,902 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef HAVE_LIBGAMMA_METHOD_LINUX_DRM -# error Compiling gamma-linux-drm.c without HAVE_LIBGAMMA_METHOD_LINUX_DRM -#endif - -#define _GNU_SOURCE - -#include "gamma-linux-drm.h" - -#include "libgamma-error.h" -#include "edid.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#ifndef O_CLOEXEC -# define O_CLOEXEC 02000000 -#endif -#ifndef NGROUPS_MAX -# define NGROUPS_MAX 65536 -#endif -#ifndef PATH_MAX -# define PATH_MAX 4096 -#endif - - - -/** - * Graphics card data for the Direct Rendering Manager adjustment method - */ -typedef struct libgamma_drm_card_data { - /** - * File descriptor for the connection to the graphics card - */ - int fd; - - /** - * The graphics card's mode resources - */ - drmModeRes *res; - - /** - * Resources for open connectors - */ - drmModeConnector **connectors; - - /** - * Resources for open encoders - */ - drmModeEncoder **encoders; - -} libgamma_drm_card_data_t; - - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void libgamma_linux_drm_method_capabilities(libgamma_method_capabilities_t *restrict this) -{ - /* Support for all information except gamma ramp support */ - this->crtc_information = LIBGAMMA_CRTC_INFO_MACRO_EDID - | LIBGAMMA_CRTC_INFO_MACRO_VIEWPORT - | LIBGAMMA_CRTC_INFO_MACRO_RAMP - | LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER - | LIBGAMMA_CRTC_INFO_ACTIVE - | LIBGAMMA_CRTC_INFO_MACRO_CONNECTOR; - /* DRM supports multiple partitions and CRTC:s but not sites */ - this->default_site_known = 1; - this->multiple_sites = 0; - this->multiple_partitions = 1; - this->multiple_crtcs = 1; - /* Partitions are graphics cards in DRM */ - this->partitions_are_graphics_cards = 1; - /* Linux does not have system restore capabilities */ - this->site_restore = 0; - this->partition_restore = 0; - this->crtc_restore = 0; - /* Gamma ramp sizes are identical but not fixed */ - this->identical_gamma_sizes = 1; - this->fixed_gamma_size = 0; - /* Gamma ramp depths are fixed */ - this->fixed_gamma_depth = 1; - /* DRM is a real non-faked adjustment method */ - this->real = 1; - this->fake = 0; - /* Gamma ramp adjustments are persistent */ - this->auto_restore = 0; -} - - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_linux_drm_site_initialise(libgamma_site_state_t *restrict this, char *restrict site) -{ - char pathname[PATH_MAX]; - struct stat _attr; - - if (site) - return LIBGAMMA_NO_SUCH_SITE; - - /* Count the number of available graphics cards by - `stat`:ing their existence in an API filesystem */ - this->partitions_available = 0; - for (;;) { - /* Construct pathname of graphics card device */ - snprintf(pathname, sizeof(pathname) / sizeof(char), - DRM_DEV_NAME, DRM_DIR_NAME, (int)(this->partitions_available)); - /* `stat` the graphics card's existence */ - if (stat(pathname, &_attr)) - break; - /* Move on to next graphics card */ - if (this->partitions_available++ > INT_MAX) - return LIBGAMMA_IMPOSSIBLE_AMOUNT; - } - return 0; -} - - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void -libgamma_linux_drm_site_destroy(libgamma_site_state_t *restrict this) -{ - (void) this; -} - - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_linux_drm_site_restore(libgamma_site_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - -/** - * Figure out why `open` failed for a graphics card - * - * @param pathname The pathname of the error card - * @return The error code to report - */ -static int -figure_out_card_open_error(const char *pathname) -{ - gid_t supplemental_groups[NGROUPS_MAX]; - struct group *group; - struct stat attr; - int i, n; - - - /* Check which the device exists */ - if (errno == ENXIO || errno == ENODEV) - return LIBGAMMA_NO_SUCH_PARTITION; - - - /* If we did not get access permission, figure out why */ - - if (errno != EACCES) { - /* If we could not figure out what went - * wrong, just return the error we got */ - return LIBGAMMA_ERRNO_SET; - } - -#define __test(R, W) ((attr.st_mode & ((R) | (W))) == ((R) | (W))) - - /* Get permission requirement for the file */ - if (stat(pathname, &attr)) - return errno == EACCES ? LIBGAMMA_NO_SUCH_PARTITION : LIBGAMMA_ERRNO_SET; - - /* Test owner's, group's and others' permissions */ - if ((attr.st_uid == geteuid() && __test(S_IRUSR, S_IWUSR)) || - (attr.st_gid == getegid() && __test(S_IRGRP, S_IWGRP)) || __test(S_IROTH, S_IWOTH)) - return LIBGAMMA_DEVICE_ACCESS_FAILED; - - /* The group should be "video", but perhaps - it is "root" to restrict users */ - if (!attr.st_gid /* root group */ || __test(S_IRGRP, S_IWGRP)) - return LIBGAMMA_DEVICE_RESTRICTED; - - - /* Get the user's supplemental group membership list */ - n = getgroups(NGROUPS_MAX, supplemental_groups); - if (n < 0) - return LIBGAMMA_ERRNO_SET; - - /* Test whether any of the supplemental - group should be satisfactory */ - for (i = 0; i < n; i++) - if (supplemental_groups[i] == attr.st_gid) - break; - - /* If one of the supplemental groups should be satisfactory, - then we do not know anything more than that access failed */ - if (i != n) - return LIBGAMMA_DEVICE_ACCESS_FAILED; - - /* Otherwise, try to get the name of the group that is - required and report the missing group membership */ - { - static __thread char buf[1024]; /* My output of `sysconf(_SC_GETGR_R_SIZE_MAX)`. */ - struct group _grp; - - errno = getgrgid_r(attr.st_gid, &_grp, buf, sizeof(buf) / sizeof(char), &group); - if (errno == ERANGE) - { - /* The lenght of the group's name is absurdly long, degrade to thread-unsafe. */ - errno = 0; - group = getgrgid(attr.st_gid); - } - else if (errno) - return LIBGAMMA_ERRNO_SET; - } - - libgamma_group_gid = attr.st_gid; - libgamma_group_name = group ? group->gr_name : NULL; - return LIBGAMMA_DEVICE_REQUIRE_GROUP; -#undef __test -} - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_linux_drm_partition_initialise(libgamma_partition_state_t *restrict this, - libgamma_site_state_t *restrict site, size_t partition) -{ - int rc = 0; - libgamma_drm_card_data_t *restrict data; - char pathname[PATH_MAX]; - - (void) site; - - /* Check for partition index overflow */ - if (partition > INT_MAX) - return LIBGAMMA_NO_SUCH_PARTITION; - - /* Allocate and initialise graphics card data */ - this->data = NULL; - data = malloc(sizeof(libgamma_drm_card_data_t)); - if (!data) - return LIBGAMMA_ERRNO_SET; - data->fd = -1; - data->res = NULL; - data->encoders = NULL; - data->connectors = NULL; - - /* Get the pathname for the graphics card */ - snprintf(pathname, sizeof(pathname) / sizeof(char), DRM_DEV_NAME, DRM_DIR_NAME, (int)partition); - - /* Acquire access to the graphics card */ - data->fd = open(pathname, O_RDWR | O_CLOEXEC); - if (data->fd < 0) { - rc = figure_out_card_open_error(pathname); - goto fail_data; - } - - /* Acquire mode resources */ - data->res = drmModeGetResources(data->fd); - if (!data->res) { - rc = LIBGAMMA_ACQUIRING_MODE_RESOURCES_FAILED; - goto fail_fd; - } - - /* Get the number of CRTC:s that are available in the partition */ - if (data->res->count_crtcs < 0) { - rc = LIBGAMMA_NEGATIVE_CRTC_COUNT; - goto fail_res; - } - this->crtcs_available = (size_t)data->res->count_crtcs; - this->data = data; - return 0; - -fail_res: - drmModeFreeResources(data->res); -fail_fd: - close(data->fd); -fail_data: - free(data); - return rc; -} - - -/** - * Release all connectors and encoders - * - * @param this The graphics card data - */ -static void -release_connectors_and_encoders(libgamma_drm_card_data_t *restrict this) -{ - size_t i, n; - - /* Release individual encoders */ - if (this->encoders) - for (i = 0, n = (size_t)(this->res->count_connectors); i < n; i++) - if (this->encoders[i]) - drmModeFreeEncoder(this->encoders[i]); - /* Release encoder array */ - free(this->encoders); - this->encoders = NULL; - - /* Release individual connectors */ - if (this->connectors) - for (i = 0, n = (size_t)(this->res->count_connectors); i < n; i++) - if (this->connectors[i]) - drmModeFreeConnector(this->connectors[i]); - /* Release connector array */ - free(this->connectors); - this->connectors = NULL; -} - - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void -libgamma_linux_drm_partition_destroy(libgamma_partition_state_t *restrict this) -{ - libgamma_drm_card_data_t *restrict data = this->data; - release_connectors_and_encoders(data); - if (data->res) - drmModeFreeResources(data->res); - if (data->fd >= 0) - close(data->fd); - free(data); -} - - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings. - * - * @param this The partition state. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. - */ -int -libgamma_linux_drm_partition_restore(libgamma_partition_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_linux_drm_crtc_initialise(libgamma_crtc_state_t *restrict this, - libgamma_partition_state_t *restrict partition, size_t crtc) -{ - libgamma_drm_card_data_t *restrict card = partition->data; - - if (crtc >= partition->crtcs_available) - return LIBGAMMA_NO_SUCH_CRTC; - this->data = (void*)(size_t)card->res->crtcs[crtc]; - return 0; -} - - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void -libgamma_linux_drm_crtc_destroy(libgamma_crtc_state_t *restrict this) -{ - (void) this; -} - - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC. - * - * @param this The CRTC state. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library. - */ -int -libgamma_linux_drm_crtc_restore(libgamma_crtc_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - -/** - * Find the connector that a CRTC belongs to - * - * @param this The CRTC state - * @param error Output of the error value to store of error report - * fields for data that requires the connector -'* @return The CRTC's conncetor, `NULL` on error - */ -static drmModeConnector * -find_connector(libgamma_crtc_state_t *restrict this, int *restrict error) -{ - uint32_t crtc_id = (uint32_t)(size_t)this->data; - libgamma_drm_card_data_t *restrict card = this->partition->data; - size_t i, n = (size_t)card->res->count_connectors; - /* Open connectors and encoders if not already opened */ - if (!card->connectors) { - /* Allocate connector and encoder arrays; we use `calloc` - so all non-loaded elements are `NULL` after an error */ - card->connectors = calloc(n, sizeof(drmModeConnector *)); - if (!card->connectors) - goto fail; - card->encoders = calloc(n, sizeof(drmModeEncoder *)); - if (!card->encoders) - goto fail; - /* Fill connector and encoder arrays */ - for (i = 0; i < n; i++) { - /* Get connector */ - card->connectors[i] = drmModeGetConnector(card->fd, card->res->connectors[i]); - if (!card->connectors[i]) - goto fail; - /* Get encoder if the connector is enabled. If it is disabled it - will not have an encoder, which is indicated by the encoder - ID being 0. In such case, leave the encoder to be `NULL`. */ - if (card->connectors[i]->encoder_id) { - card->encoders[i] = drmModeGetEncoder(card->fd, card->connectors[i]->encoder_id); - if (!card->encoders[i]) - goto fail; - } - } - } - /* No error has occurred yet */ - *error = 0; - /* Find connector */ - for (i = 0; i < n; i++) - if (card->encoders[i] && card->connectors[i] && card->encoders[i]->crtc_id == crtc_id) - return card->connectors[i]; - /* We did not find the connector */ - *error = LIBGAMMA_CONNECTOR_UNKNOWN; - return NULL; - -fail: - /* Report the error that got us here, release - resouces and exit with `NULL` for failure */ - *error = errno; - release_connectors_and_encoders(card); - return NULL; -} - - -/** - * Get the size of the gamma ramps for a CRTC - * - * @param out Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @return The value stored in `out->gamma_size_error` - */ -static int -get_gamma_ramp_size(libgamma_crtc_information_t *restrict out, const libgamma_crtc_state_t *restrict crtc) -{ - libgamma_drm_card_data_t *restrict card = crtc->partition->data; - uint32_t crtc_id = card->res->crtcs[crtc->crtc]; - drmModeCrtc *restrict crtc_info; - /* Get CRTC information */ - errno = 0; - crtc_info = drmModeGetCrtc(card->fd, crtc_id); - out->gamma_size_error = crtc_info ? 0 : errno; - /* Get gamma ramp size */ - if (!out->gamma_size_error) { - /* Store gamma ramp size */ - out->red_gamma_size = out->green_gamma_size = out->blue_gamma_size = (size_t)crtc_info->gamma_size; - /* Sanity check gamma ramp size */ - out->gamma_size_error = crtc_info->gamma_size < 2 ? LIBGAMMA_SINGLETON_GAMMA_RAMP : 0; - /* Release CRTC information */ - drmModeFreeCrtc(crtc_info); - } - return out->gamma_size_error; -} - - -/** - * Get the a monitor's subpixel order - * - * @param out Instance of a data structure to fill with the information about the CRTC - * @param connector The connector - */ -static void -get_subpixel_order(libgamma_crtc_information_t *restrict out, const drmModeConnector *restrict connector) -{ -#define __select(value)\ - case DRM_MODE_SUBPIXEL_##value:\ - out->subpixel_order = LIBGAMMA_SUBPIXEL_ORDER_##value;\ - break - - switch (connector->subpixel) { - __select (UNKNOWN); - __select (HORIZONTAL_RGB); - __select (HORIZONTAL_BGR); - __select (VERTICAL_RGB); - __select (VERTICAL_BGR); - __select (NONE); - default: - out->subpixel_order_error = LIBGAMMA_SUBPIXEL_ORDER_NOT_RECOGNISED; - break; - } - -#undef __select -} - - -/** - * Get a connector's type - * - * @param out Instance of a data structure to fill with the information about the CRTC - * @param connector The connector - * @param connector_name_base Output for the basename of the connector - */ -static void -get_connector_type(libgamma_crtc_information_t *restrict out, const drmModeConnector *restrict connector, - const char **restrict connector_name_base) -{ -#define __select(type, name)\ - case DRM_MODE_CONNECTOR_##type:\ - out->connector_type = LIBGAMMA_CONNECTOR_TYPE_##type;\ - *connector_name_base = name;\ - break - - /* These may not have been included by , - but they should be available. Becuase we define them - outself, it is best to test them last. */ -#ifndef DRM_MODE_CONNECTOR_VIRTUAL -# define DRM_MODE_CONNECTOR_VIRTUAL 15 -#endif -#ifndef DRM_MODE_CONNECTOR_DSI -# define DRM_MODE_CONNECTOR_DSI 16 -#endif - - /* Translate connector type from DRM to libgamma - and store connector basename */ - switch (connector->connector_type) { - __select (Unknown, "Unknown" ); - __select (VGA, "VGA" ); - __select (DVII, "DVI-I" ); - __select (DVID, "DVI-D" ); - __select (DVIA, "DVI-A" ); - __select (Composite, "Composite"); - __select (SVIDEO, "SVIDEO" ); - __select (LVDS, "LVDS" ); - __select (Component, "Component"); - __select (9PinDIN, "DIN" ); - __select (DisplayPort, "DP" ); - __select (HDMIA, "HDMI-A" ); - __select (HDMIB, "HDMI-B" ); - __select (TV, "TV" ); - __select (eDP, "eDP" ); - __select (VIRTUAL, "VIRTUAL" ); - __select (DSI, "DSI" ); - default: - out->connector_type_error = LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED; - out->connector_name_error = LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED; - break; - } - -#undef __select -} - - -/** - * Read information from the CRTC's conncetor - * - * @param crtc The state of the CRTC whose information should be read - * @param out Instance of a data structure to fill with the information about the CRTC - * @param connector The CRTC's connector - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Non-zero if at least on error occured - */ -static int -read_connector_data(libgamma_crtc_state_t *restrict crtc, libgamma_crtc_information_t *restrict out, - const drmModeConnector *restrict connector, int32_t fields) -{ - const char *connector_name_base = NULL; - libgamma_drm_card_data_t *restrict card; - uint32_t type; - size_t i, n, c; - - /* Get some information that does not require too much work */ - if (fields & (LIBGAMMA_CRTC_INFO_MACRO_ACTIVE | LIBGAMMA_CRTC_INFO_MACRO_CONNECTOR)) { - /* Get whether or not a monitor is plugged in */ - out->active = connector->connection == DRM_MODE_CONNECTED; - out->active_error = connector->connection == DRM_MODE_UNKNOWNCONNECTION ? LIBGAMMA_STATE_UNKNOWN : 0; - if (!out->active) { - if (fields & (LIBGAMMA_CRTC_INFO_MACRO_VIEWPORT | LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER)) - out->width_mm_error = out->height_mm_error = out->subpixel_order_error = LIBGAMMA_NOT_CONNECTED; - goto not_connected; - } - - /* Get viewport dimension */ - out->width_mm = connector->mmWidth; - out->height_mm = connector->mmHeight; - - /* Get subpixel order */ - get_subpixel_order(out, connector); - - not_connected: - - /* Get connector type */ - get_connector_type(out, connector, &connector_name_base); - } - - /* Get the connector's name */ - if ((fields & LIBGAMMA_CRTC_INFO_CONNECTOR_NAME) && !out->connector_name_error) { - card = crtc->partition->data; - type = connector->connector_type; - n = (size_t)card->res->count_connectors; - - /* Allocate memory for the name of the connector */ - out->connector_name = malloc((strlen(connector_name_base) + 12) * sizeof(char)); - if (!out->connector_name) - return (out->connector_name_error = errno); - - /* Get the number of connectors with the same type on the same graphics card */ - for (i = c = 0; i < n && card->connectors[i] != connector; i++) - if (card->connectors[i]->connector_type == type) - c++; - - /* Construct and store connect name that is unique to the graphics card */ - sprintf(out->connector_name, "%s-%" PRIu32, connector_name_base, (uint32_t)(c + 1)); - } - - /* Did something go wrong? */ - return out->subpixel_order_error | out->active_error | out->connector_name_error; -} - - -/** - * Get the extended display identification data for a monitor - * - * @param crtc The CRTC state - * @param out Instance of a data structure to fill with the information about the CRTC - * @param connector The CRTC's connector - * @reutnr Non-zero on error - */ -static int -get_edid(libgamma_crtc_state_t *restrict crtc, libgamma_crtc_information_t *restrict out, drmModeConnector *connector) -{ - libgamma_drm_card_data_t *restrict card = crtc->partition->data; - int prop_n = connector->count_props; - int prop_i; - drmModePropertyRes *restrict prop; - drmModePropertyBlobRes *restrict blob; - - /* Test all properies on the connector */ - for (prop_i = 0; prop_i < prop_n; prop_i++) { - /* Get output property */ - prop = drmModeGetProperty(card->fd, connector->props[prop_i]); - if (!prop) - continue; - /* Is this property the EDID? */ - if (!strcmp(prop->name, "EDID")) { - /* Get the property value */ - blob = drmModeGetPropertyBlob(card->fd, (uint32_t)connector->prop_values[prop_i]); - if (!blob) { - drmModeFreeProperty(prop); - return (out->edid_error = LIBGAMMA_PROPERTY_VALUE_QUERY_FAILED); - } - if (blob->data) { - /* Get and store the length of the EDID */ - out->edid_length = blob->length; - /* Allocate memory for a copy of the EDID that is under our memory control */ - out->edid = malloc(out->edid_length * sizeof(unsigned char)); - if (!out->edid) { - out->edid_error = errno; - } else { - /* Copy the EDID so we can free resources that got us here */ - memcpy(out->edid, blob->data, (size_t)out->edid_length * sizeof(char)); - } - /* Free the propriety value and the propery */ - drmModeFreePropertyBlob(blob); - drmModeFreeProperty(prop); - /* Were we successful? */ - return !out->edid; - } - /* Free the propriety value */ - drmModeFreePropertyBlob(blob); - } - /* Free the propriety */ - drmModeFreeProperty(prop); - } - /* If we get here, we did not find a EDID */ - return (out->edid_error = LIBGAMMA_EDID_NOT_FOUND); -} - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error; on error refer to the error reports in `this` - */ -int -libgamma_linux_drm_get_crtc_information(libgamma_crtc_information_t *restrict this, - libgamma_crtc_state_t *restrict crtc, int32_t fields) -{ -#define _E(FIELD) ((fields & FIELD) ? LIBGAMMA_CRTC_INFO_NOT_SUPPORTED : 0) - - int e = 0, require_connector, free_edid, error; - drmModeConnector *restrict connector; - - /* Wipe all error indicators */ - memset(this, 0, sizeof(libgamma_crtc_information_t)); - - /* We need to free the EDID after us if it is not explicitly requested */ - free_edid = !(fields & LIBGAMMA_CRTC_INFO_EDID); - - /* Figure out whether we require the connector to get all information we want */ - require_connector = fields & (LIBGAMMA_CRTC_INFO_MACRO_ACTIVE | LIBGAMMA_CRTC_INFO_MACRO_CONNECTOR); - - /* If we are not interested in the connector or monitor, jump */ - if (!require_connector) - goto cont; - /* Find connector. */ - connector = find_connector(crtc, &error); - if (!connector) { - /* Store reported error in affected fields */ - e |= this->width_mm_error = this->height_mm_error - = this->connector_type_error = this->subpixel_order_error - = this->active_error = this->connector_name_error - = this->edid_error = this->gamma_error - = this->width_mm_edid_error = this->height_mm_edid_error = error; - goto cont; - } - - /* Read connector data and monitor data, excluding EDID */ - e |= read_connector_data(crtc, this, connector, fields); - - /* If we do not want any EDID information, jump */ - if (!(fields & LIBGAMMA_CRTC_INFO_MACRO_EDID)) - goto cont; - /* If there is not monitor that report error in EDID related fields */ - if (this->active_error || !this->active) { - e |= this->edid_error = this->gamma_error - = this->width_mm_edid_error = this->height_mm_edid_error - = LIBGAMMA_NOT_CONNECTED; - goto cont; - } - /* Get EDID */ - e |= get_edid(crtc, this, connector); - if (!this->edid) { - this->gamma_error = this->width_mm_edid_error = this->height_mm_edid_error = this->edid_error; - goto cont; - } - /* Parse EDID */ - if (fields & (LIBGAMMA_CRTC_INFO_MACRO_EDID ^ LIBGAMMA_CRTC_INFO_EDID)) - e |= libgamma_parse_edid(this, fields); - -cont: - /* Get gamma ramp size */ - e |= (fields & LIBGAMMA_CRTC_INFO_GAMMA_SIZE) ? get_gamma_ramp_size(this, crtc) : 0; - /* Store gamma ramp depth */ - this->gamma_depth = 16; - /* DRM does not support quering gamma ramp support */ - e |= this->gamma_support_error = _E(LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT); - - /* Free the EDID after us */ - if (free_edid) { - free(this->edid); - this->edid = NULL; - } - - return e ? -1 : 0; - -#undef _E -} - - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_linux_drm_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t *restrict ramps) -{ - libgamma_drm_card_data_t *restrict card = this->partition->data; - int r; -#ifdef DEBUG - /* Gamma ramp sizes are identical but not fixed */ - if (ramps->red_size != ramps->green_size || ramps->red_size != ramps->blue_size) - return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; -#endif - /* Read current gamma ramps */ - r = drmModeCrtcGetGamma(card->fd, (uint32_t)(size_t)this->data, (uint32_t)ramps->red_size, - ramps->red, ramps->green, ramps->blue); - return r ? LIBGAMMA_GAMMA_RAMP_READ_FAILED : 0; -} - - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_linux_drm_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t ramps) -{ - libgamma_drm_card_data_t *restrict card = this->partition->data; - int r; -#ifdef DEBUG - /* Gamma ramp sizes are identical but not fixed */ - if (ramps.red_size != ramps.green_size || ramps.red_size != ramps.blue_size) - return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; -#endif - - /* Apply gamma ramps */ - r = drmModeCrtcSetGamma(card->fd, (uint32_t)(size_t)this->data, - (uint32_t)ramps.red_size, ramps.red, ramps.green, ramps.blue); - /* Check for errors */ - if (r) { - switch (errno) { - case EACCES: - case EAGAIN: - case EIO: - /* Permission denied errors must be ignored, because we do not - * have permission to do this while a display server is active. - * We are also checking for some other error codes just in case. */ - case EBUSY: - case EINPROGRESS: - /* It is hard to find documentation for DRM (in fact all of this is - * just based on the functions names and some testing,) perhaps we - * could get this if we are updating to fast. */ - break; - case EBADF: - case ENODEV: - case ENXIO: - /* XXX: I have not actually tested removing my graphics card or, - * monitor but I imagine either of these is what would happen. */ - return LIBGAMMA_GRAPHICS_CARD_REMOVED; - - default: - return LIBGAMMA_ERRNO_SET; - } - } - return 0; -} diff --git a/src/lib/gamma-linux-drm.h b/src/lib/gamma-linux-drm.h deleted file mode 100644 index 41a654d..0000000 --- a/src/lib/gamma-linux-drm.h +++ /dev/null @@ -1,139 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_GAMMA_LINUX_DRM_H -#define LIBGAMMA_GAMMA_LINUX_DRM_H - -#ifndef HAVE_LIBGAMMA_METHOD_LINUX_DRM -# error Including gamma-linux-drm.h without HAVE_LIBGAMMA_METHOD_LINUX_DRM -#endif - - -#include "libgamma-method.h" - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void libgamma_linux_drm_method_capabilities(libgamma_method_capabilities_t *restrict); - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_linux_drm_site_initialise(libgamma_site_state_t *restrict, char *restrict); - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void libgamma_linux_drm_site_destroy(libgamma_site_state_t *restrict) __attribute__((const)); - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_linux_drm_site_restore(libgamma_site_state_t *restrict); - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_linux_drm_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void libgamma_linux_drm_partition_destroy(libgamma_partition_state_t *restrict); - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_linux_drm_partition_restore(libgamma_partition_state_t *restrict); - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_linux_drm_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void libgamma_linux_drm_crtc_destroy(libgamma_crtc_state_t *restrict) __attribute__((const)); - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_linux_drm_crtc_restore(libgamma_crtc_state_t *restrict); - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error; on error refer to the error reports in `this` - */ -int libgamma_linux_drm_get_crtc_information(libgamma_crtc_information_t *restrict, libgamma_crtc_state_t *restrict, int32_t); - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_linux_drm_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_linux_drm_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t); - - -#endif diff --git a/src/lib/gamma-quartz-cg.c b/src/lib/gamma-quartz-cg.c deleted file mode 100644 index b0e3a29..0000000 --- a/src/lib/gamma-quartz-cg.c +++ /dev/null @@ -1,359 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef HAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS -# error Compiling gamma-quartz-cg.c without HAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS -#endif - -#include "gamma-quartz-cg.h" - -#include "libgamma-error.h" - -#ifdef FAKE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS -# include "fake-quartz-cg.h" -#else -# include -# include -# define close_fake_quartz_cg() /* For compatibility with "fake-quartz-cg.h" */ -#endif - -#include -#include - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void -libgamma_quartz_cg_method_capabilities(libgamma_method_capabilities_t* restrict this) -{ - /* Gamma ramps size and depth can be queried */ - this->crtc_information = LIBGAMMA_CRTC_INFO_GAMMA_SIZE | LIBGAMMA_CRTC_INFO_GAMMA_DEPTH; - /* Quartz/CoreGraphics does not support sites or partitions */ - this->default_site_known = 1; - this->multiple_sites = 0; - this->multiple_partitions = 0; - /* Quartz/CoreGraphics does support CRTC:s */ - this->multiple_crtcs = 1; - /* Partitions are not supported... */ - this->partitions_are_graphics_cards = 0; - /* CoreGraphics have support for system restore */ - this->site_restore = 1; - this->partition_restore = 1; - /* But not for individual CRTC:s */ - this->crtc_restore = 0; - /* Gamma ramp sizes are identifical but not fixed */ - this->identical_gamma_sizes = 1; - this->fixed_gamma_size = 0; - /* Gamma ramp depths are fixed */ - this->fixed_gamma_depth = 1; - /* Quartz/CoreGraphics is a real adjustment method that can be faked */ -#ifdef FAKE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS - /* It is considered real but fake if it is translated to X RandR */ - this->fake = 1; -# ifdef HAVE_LIBGAMMA_METHOD_X_RANDR - this->real = 1; -# else - this->real = 0; -# endif -#else - /* It is real and not fake if we are running on Mac OS X */ - this->fake = 0; - this->real = 1; -#endif - /* Gamma ramp adjustments are non-persistent */ - this->auto_restore = 1; -} - - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_quartz_cg_site_initialise(libgamma_site_state_t *restrict this, char *restrict site) -{ - this->partitions_available = 1; - return site ? 0 : LIBGAMMA_NO_SUCH_SITE; -} - - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void -libgamma_quartz_cg_site_destroy(libgamma_site_state_t *restrict this) -{ - (void) this; - close_fake_quartz_cg(); -} - - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_quartz_cg_site_restore(libgamma_site_state_t *restrict this) -{ - (void) this; - CGDisplayRestoreColorSyncSettings(); - return 0; -} - - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_quartz_cg_partition_initialise(libgamma_partition_state_t *restrict this, - libgamma_site_state_t *restrict site, size_t partition) -{ - CGDirectDisplayID *crtcs, *crtcs_old; - uint32_t cap = 4, n; - - (void) site; - - this->data = NULL; - - if (partition) - return LIBGAMMA_NO_SUCH_PARTITION; - - /* Allocate array of CRTC ID:s */ - crtcs = malloc((size_t)cap * sizeof(CGDirectDisplayID)); - if (!crtcs) - return LIBGAMMA_ERRNO_SET; - - /* It is not possible to ask CoreGraphics how many CRTC:s are - * available. We have to ask it to give us a ID:s of a number - * of CRTC:s and ask for more if we got as many as we asked for. */ - for (;;) { - /* Ask for CRTC ID:s */ - if (CGGetOnlineDisplayList(cap, crtcs, &n) != kCGErrorSuccess) - return free(crtcs), LIBGAMMA_LIST_CRTCS_FAILED; - /* If we did not get as many as we asked for then we have all */ - if (n < cap) - break; - /* Increase the number CRTC ID:s to ask for */ - if (cap > SIZE_MAX / 2) /* We could also test ~0, but it is still too many */ - return free(crtcs), LIBGAMMA_IMPOSSIBLE_AMOUNT; - cap <<= 1; - /* Grow the array of CRTC ID:s so that it can fit all we are asking for */ - crtcs = realloc(crtcs_old = crtcs, (size_t)cap * sizeof(CGDirectDisplayID)); - if (!crtcs) { - free(crtcs_old); - return LIBGAMMA_ERRNO_SET; - } - } - - /* Store CRTC ID:s and CRTC count */ - this->data = crtcs; - this->crtcs_available = (size_t)n; - return 0; -} - - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void -libgamma_quartz_cg_partition_destroy(libgamma_partition_state_t *restrict this) -{ - free(this->data); -} - - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_quartz_cg_partition_restore(libgamma_partition_state_t *restrict this) -{ - return libgamma_quartz_cg_site_restore(this->site); -} - - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_quartz_cg_crtc_initialise(libgamma_crtc_state_t *restrict this, - libgamma_partition_state_t *restrict partition, size_t crtc) -{ - (void) this; - return crtc < partition->crtcs_available ? 0 : LIBGAMMA_NO_SUCH_CRTC; -} - - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void -libgamma_quartz_cg_crtc_destroy(libgamma_crtc_state_t *restrict this) -{ - (void) this; -} - - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_quartz_cg_crtc_restore(libgamma_crtc_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read. - * @return Zero on success, -1 on error. On error refer to the error reports in `this` - */ -int -libgamma_quartz_cg_get_crtc_information(libgamma_crtc_information_t *restrict this, - libgamma_crtc_state_t *restrict crtc, int32_t fields) -{ -#define SUPPORTED_FIELDS (LIBGAMMA_CRTC_INFO_GAMMA_SIZE | LIBGAMMA_CRTC_INFO_GAMMA_DEPTH) -#define _E(FIELD) ((fields & FIELD) ? LIBGAMMA_CRTC_INFO_NOT_SUPPORTED : 0) - - CGDirectDisplayID *restrict crtcs; - size_t gamma_size; - - /* Quartz/CoreGraphics does not support EDID or monitor dimensions */ - this->edid_error = _E(LIBGAMMA_CRTC_INFO_EDID); - this->width_mm_error = _E(LIBGAMMA_CRTC_INFO_WIDTH_MM); - this->height_mm_error = _E(LIBGAMMA_CRTC_INFO_HEIGHT_MM); - this->width_mm_edid_error = _E(LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID); - this->height_mm_edid_error = _E(LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID); - /* Quartz/CoreGraphics does support gamma ramp size query - The gamma ramps are identical but not fixed, and the query can fail */ - this->gamma_size_error = 0; - if ((fields & LIBGAMMA_CRTC_INFO_GAMMA_SIZE)) { - crtcs = crtc->partition->data; - gamma_size = CGDisplayGammaTableCapacity(crtcs[crtc->crtc]); - this->red_gamma_size = this->green_gamma_size = this->blue_gamma_size = (size_t)gamma_size; - this->gamma_size_error = gamma_size < 2 ? LIBGAMMA_SINGLETON_GAMMA_RAMP : 0; - } - /* Quartz/CoreGraphics uses `float` ramps */ - this->gamma_depth = -1; - this->gamma_depth_error = 0; - /* Quartz/CoreGraphics does not support gamma ramp support queries */ - this->gamma_support_error = _E(LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT); - /* Quartz/CoreGraphics does not support EDID or connector information */ - this->subpixel_order_error = _E(LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER); - this->active_error = _E(LIBGAMMA_CRTC_INFO_ACTIVE); - this->connector_name_error = _E(LIBGAMMA_CRTC_INFO_CONNECTOR_NAME); - this->connector_type_error = _E(LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE); - this->gamma_error = _E(LIBGAMMA_CRTC_INFO_GAMMA); - - /* We failed if gamma ramp size query failed or if an unsupport field was queried */ - return (this->gamma_size_error || (fields & ~SUPPORTED_FIELDS)) ? -1 : 0; - -#undef _E -#undef SUPPORTED_FIELDS -} - - -/** - * Get the current gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_quartz_cg_crtc_get_gamma_rampsf(libgamma_crtc_state_t *restrict this, libgamma_gamma_rampsf_t *restrict ramps) -{ - CGDirectDisplayID *restrict crtcs = this->partition->data; - CGDirectDisplayID crtc_id = crtcs[this->crtc]; - uint32_t gamma_size_out; - CGError r; -#ifdef DEBUG - /* Gamma ramps sizes are identical but not fixed */ - if (ramps->red_size != ramps->green_size || ramps->red_size != ramps->blue_size) - return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; -#endif - /* Read current gamma ramps */ - r = CGGetDisplayTransferByTable(crtc_id, (uint32_t)ramps->red_size, ramps->red, ramps->green, ramps->blue, &gamma_size_out); - if (r != kCGErrorSuccess) - return LIBGAMMA_GAMMA_RAMP_READ_FAILED; - /* I hope that it will not actually ever change, - but it does return the the gamma ramp size despite - that it can be queried without querying for more */ - if (gamma_size_out != ramps->red_size) - return LIBGAMMA_GAMMA_RAMP_SIZE_CHANGED; - return 0; -} - - -/** - * Set the gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_quartz_cg_crtc_set_gamma_rampsf(libgamma_crtc_state_t *restrict this, libgamma_gamma_rampsf_t ramps) -{ - CGDirectDisplayID *restrict crtcs = this->partition->data; - CGDirectDisplayID crtc_id = crtcs[this->crtc]; - CGError r; -#ifdef DEBUG - /* Gamma ramps sizes are identical but not fixed */ - if (ramps.red_size != ramps.green_size || ramps.red_size != ramps.blue_size) - return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; -#endif - /* Apply gamma ramps */ - r = CGSetDisplayTransferByTable(crtc_id, (uint32_t)ramps.red_size, ramps.red, ramps.green, ramps.blue); - return r == kCGErrorSuccess ? 0 : LIBGAMMA_GAMMA_RAMP_WRITE_FAILED; -} diff --git a/src/lib/gamma-quartz-cg.h b/src/lib/gamma-quartz-cg.h deleted file mode 100644 index 211e055..0000000 --- a/src/lib/gamma-quartz-cg.h +++ /dev/null @@ -1,140 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_GAMMA_QUARTZ_CG_H -#define LIBGAMMA_GAMMA_QUARTZ_CG_H - -#ifndef HAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS -# error Including gamma-quartz-cg.h without HAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS -#endif - - -#include "libgamma-method.h" - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void libgamma_quartz_cg_method_capabilities(libgamma_method_capabilities_t *restrict); - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_quartz_cg_site_initialise(libgamma_site_state_t *restrict, char *restrict); - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void libgamma_quartz_cg_site_destroy(libgamma_site_state_t *restrict); - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_quartz_cg_site_restore(libgamma_site_state_t *restrict); - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_quartz_cg_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void libgamma_quartz_cg_partition_destroy(libgamma_partition_state_t *restrict); - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_quartz_cg_partition_restore(libgamma_partition_state_t *restrict); - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -__attribute__((pure)) -int libgamma_quartz_cg_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void libgamma_quartz_cg_crtc_destroy(libgamma_crtc_state_t *restrict) __attribute__((const)); - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_quartz_cg_crtc_restore(libgamma_crtc_state_t *restrict); - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error. On error refer to the error reports in `this` - */ -int libgamma_quartz_cg_get_crtc_information(libgamma_crtc_information_t *restrict, libgamma_crtc_state_t *restrict, int32_t); - -/** - * Get the current gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_quartz_cg_crtc_get_gamma_rampsf(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsf_t *restrict); - -/** - * Set the gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_quartz_cg_crtc_set_gamma_rampsf(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsf_t); - - -#endif diff --git a/src/lib/gamma-w32-gdi.c b/src/lib/gamma-w32-gdi.c deleted file mode 100644 index e93b53d..0000000 --- a/src/lib/gamma-w32-gdi.c +++ /dev/null @@ -1,360 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef HAVE_LIBGAMMA_METHOD_W32_GDI -# error Compiling gamma-w32-gdi.c without HAVE_LIBGAMMA_METHOD_W32_GDI -#endif - -#include "gamma-w32-gdi.h" - -#include "libgamma-error.h" - -#ifndef WINVER -# define WINVER 0x0500 -#endif -#ifdef FAKE_LIBGAMMA_METHOD_W32_GDI -# include "fake-w32-gdi.h" -#else -# include -# include -#endif - -#include - - -/** - * The gamma ramp size that devices will always have in Windows GDI - * - * @see http://msdn.microsoft.com/en-us/library/windows/desktop/dd372194(v=vs.85).aspx - */ -#define GAMMA_RAMP_SIZE 256 - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void -libgamma_w32_gdi_method_capabilities(libgamma_method_capabilities_t *restrict this) -{ - /* Gamma ramps size, depth and support can be queried */ - this->crtc_information = LIBGAMMA_CRTC_INFO_GAMMA_SIZE | LIBGAMMA_CRTC_INFO_GAMMA_DEPTH; - /* Windows GDI does not support sites or partitions */ - this->default_site_known = 1; - this->multiple_sites = 0; - this->multiple_partitions = 0; - /* Windows GDI does support CRTC:s */ - this->multiple_crtcs = 1; - /* Partitions are not supported... */ - this->partitions_are_graphics_cards = 0; - /* Windows GDI does not have system restore capabilities */ - this->site_restore = 0; - this->partition_restore = 0; - this->crtc_restore = 0; - /* Ramps sizes are fixed and identical and ramp depth is too */ - this->identical_gamma_sizes = 1; - this->fixed_gamma_size = 1; - this->fixed_gamma_depth = 1; - /* Windows GDI is a real adjustment method that can be faked */ -#ifdef FAKE_LIBGAMMA_METHOD_W32_GDI - /* It is considered real but fake if it is translated to X RandR */ - this->fake = 1; -# ifdef HAVE_LIBGAMMA_METHOD_X_RANDR - this->real = 1; -# else - this->real = 0; -# endif -#else - /* It is real and not fake if we are running on Windows */ - this->fake = 0; - this->real = 1; -#endif - /* Gamma ramp adjustments are persistent */ - this->auto_restore = 0; -} - - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_w32_gdi_site_initialise(libgamma_site_state_t *restrict this, char* restrict site) -{ - this->partitions_available = 1; - return !site ? 0 : LIBGAMMA_NO_SUCH_SITE; -} - - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void -libgamma_w32_gdi_site_destroy(libgamma_site_state_t *restrict this) -{ - (void) this; -} - - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_w32_gdi_site_restore(libgamma_site_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_w32_gdi_partition_initialise(libgamma_partition_state_t *restrict this, - libgamma_site_state_t *restrict site, size_t partition) -{ - DWORD n = 0; - DISPLAY_DEVICE display; - - (void) site; - - if (partition) - return LIBGAMMA_NO_SUCH_PARTITION; - - /* Count CRTC:s by iteration over all possible identifiers - until we reach on that does not exist */ - display.cb = sizeof(DISPLAY_DEVICE); - while (EnumDisplayDevices(NULL, n, &display, 0)) - if (n++ == UINT32_MAX) - return LIBGAMMA_IMPOSSIBLE_AMOUNT; - this->crtcs_available = (size_t)n; - return 0; -} - - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void -libgamma_w32_gdi_partition_destroy(libgamma_partition_state_t *restrict this) -{ - (void) this; -} - - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_w32_gdi_partition_restore(libgamma_partition_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_w32_gdi_crtc_initialise(libgamma_crtc_state_t *restrict this, - libgamma_partition_state_t *restrict partition, size_t crtc) -{ - DISPLAY_DEVICE display; - HDC context; - - (void) partition; - - this->data = NULL; - - /* Windows's API mandates this... */ - display.cb = sizeof(DISPLAY_DEVICE); - /* Get identifier for selected CRTC */ - if (!EnumDisplayDevices(NULL, (DWORD)crtc, &display, 0)) - return LIBGAMMA_NO_SUCH_CRTC; - /* Check that the connector is enabled, - * newer versions of Windows will always pass. - * (According to w32's documentation, but that - * that is a load of crap) */ - if (!(display.StateFlags & DISPLAY_DEVICE_ACTIVE)) - return LIBGAMMA_CONNECTOR_DISABLED; - /* Acquire CRTC connection. */ - context = CreateDC(TEXT("DISPLAY"), display.DeviceName, NULL, NULL); - if (!context) - return LIBGAMMA_OPEN_CRTC_FAILED; - this->data = context; - return 0; -} - - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void -libgamma_w32_gdi_crtc_destroy(libgamma_crtc_state_t *restrict this) -{ - if (this->data) - ReleaseDC(NULL, this->data); -} - - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_w32_gdi_crtc_restore(libgamma_crtc_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error. On error refer to the error reports in `this` - */ -int -libgamma_w32_gdi_get_crtc_information(libgamma_crtc_information_t *restrict this, - libgamma_crtc_state_t *restrict crtc, int32_t fields) -{ -#define KNOWN_FIELDS (LIBGAMMA_CRTC_INFO_GAMMA_SIZE | LIBGAMMA_CRTC_INFO_GAMMA_DEPTH) -#define _E(FIELD) ((fields & FIELD) ? LIBGAMMA_CRTC_INFO_NOT_SUPPORTED : 0) - - (void) crtc; - - /* Windows GDI does not support EDID or monitor dimensions */ - this->edid_error = _E(LIBGAMMA_CRTC_INFO_EDID); - this->width_mm_error = _E(LIBGAMMA_CRTC_INFO_WIDTH_MM); - this->height_mm_error = _E(LIBGAMMA_CRTC_INFO_HEIGHT_MM); - this->width_mm_edid_error = _E(LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID); - this->height_mm_edid_error = _E(LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID); - /* Windows GDI have fixed gamma ramp sizes */ - this->red_gamma_size = GAMMA_RAMP_SIZE; - this->green_gamma_size = GAMMA_RAMP_SIZE; - this->blue_gamma_size = GAMMA_RAMP_SIZE; - this->gamma_size_error = 0; - /* Windows GDI have fixed gamma ramp depth */ - this->gamma_depth = 16; - this->gamma_depth_error = 0; - /* It is possible to query Windows GDI whether the device - have gamma ramp support. It cannot fail. However, I think - the result is incorrect if multiple monitors are active, - so we cannot include this. */ - /* - if ((fields & LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT)) - this->gamma_support = GetDeviceCaps(crtc->data, COLORMGMTCAPS) == CM_GAMMA_RAMP; - this->gamma_support_error = 0; - */ - this->gamma_support_error = _E(LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT); - /* Windows GDI does not support EDID or connector information */ - this->subpixel_order_error = _E(LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER); - this->active_error = _E(LIBGAMMA_CRTC_INFO_ACTIVE); - this->connector_name_error = _E(LIBGAMMA_CRTC_INFO_CONNECTOR_NAME); - this->connector_type_error = _E(LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE); - this->gamma_error = _E(LIBGAMMA_CRTC_INFO_GAMMA); - - /* There was a failure if and only if unsupport field was requested. */ - return (fields & ~KNOWN_FIELDS) ? -1 : 0; - -#undef _E -#undef KNOWN_FIELDS -} - - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_w32_gdi_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t *restrict ramps) -{ -#ifdef DEBUG - /* Windows GDI have fixed gamma ramp sizes */ - if (ramps-> red_size != GAMMA_RAMP_SIZE || - ramps->green_size != GAMMA_RAMP_SIZE || - ramps-> blue_size != GAMMA_RAMP_SIZE) - return LIBGAMMA_WRONG_GAMMA_RAMP_SIZE; -#endif - /* Read current gamma ramps */ - if (!GetDeviceGammaRamp(this->data, ramps->red)) - return LIBGAMMA_GAMMA_RAMP_READ_FAILED; - return 0; -} - - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_w32_gdi_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t ramps) -{ -#ifdef DEBUG - /* Windows GDI have fixed gamma ramp sizes */ - if (ramps. red_size != GAMMA_RAMP_SIZE || - ramps.green_size != GAMMA_RAMP_SIZE || - ramps. blue_size != GAMMA_RAMP_SIZE) - return LIBGAMMA_WRONG_GAMMA_RAMP_SIZE; -#endif - /* Apply gamma ramps */ - if (!SetDeviceGammaRamp(this->data, ramps.red)) - return LIBGAMMA_GAMMA_RAMP_WRITE_FAILED; - return 0; -} diff --git a/src/lib/gamma-w32-gdi.h b/src/lib/gamma-w32-gdi.h deleted file mode 100644 index d139f16..0000000 --- a/src/lib/gamma-w32-gdi.h +++ /dev/null @@ -1,139 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_GAMMA_W32_GDI_H -#define LIBGAMMA_GAMMA_W32_GDI_H - -#ifndef HAVE_LIBGAMMA_METHOD_W32_GDI -# error Including gamma-w32-gdi.h without HAVE_LIBGAMMA_METHOD_W32_GDI -#endif - - -#include "libgamma-method.h" - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void libgamma_w32_gdi_method_capabilities(libgamma_method_capabilities_t *restrict); - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_w32_gdi_site_initialise(libgamma_site_state_t *restrict, char *restrict); - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void libgamma_w32_gdi_site_destroy(libgamma_site_state_t *restrict) __attribute__((const)); - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_w32_gdi_site_restore(libgamma_site_state_t *restrict); - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_w32_gdi_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void libgamma_w32_gdi_partition_destroy(libgamma_partition_state_t *restrict) __attribute__((const)); - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_w32_gdi_partition_restore(libgamma_partition_state_t *restrict); - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_w32_gdi_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void libgamma_w32_gdi_crtc_destroy(libgamma_crtc_state_t *restrict); - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_w32_gdi_crtc_restore(libgamma_crtc_state_t *restrict); - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error. On error refer to the error reports in `this` - */ -int libgamma_w32_gdi_get_crtc_information(libgamma_crtc_information_t *restrict, libgamma_crtc_state_t *restrict, int32_t); - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_w32_gdi_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_w32_gdi_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t); - - -#endif diff --git a/src/lib/gamma-x-randr.c b/src/lib/gamma-x-randr.c deleted file mode 100644 index fce65d1..0000000 --- a/src/lib/gamma-x-randr.c +++ /dev/null @@ -1,985 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef HAVE_LIBGAMMA_METHOD_X_RANDR -# error Compiling gamma-x-randr.c without HAVE_LIBGAMMA_METHOD_X_RANDR -#endif - -#include "gamma-x-randr.h" - -#include "libgamma-error.h" -#include "edid.h" - -#include -#include -#include -#include -#ifdef DEBUG -# include -#endif - -#include -#include - - -/** - * The major version of RandR the library expects - */ -#define RANDR_VERSION_MAJOR 1 - -/** - * The minor version of RandR the library expects - */ -#define RANDR_VERSION_MINOR 3 - - - -/** - * Data structure for partition data - */ -typedef struct libgamma_x_randr_partition_data { - /** - * Mapping from CRTC indices to CRTC identifiers - */ - xcb_randr_crtc_t *crtcs; - - /** - * Mapping from output indices to output identifiers - */ - xcb_randr_output_t *outputs; - - /** - * The number of outputs available - */ - size_t outputs_count; - - /** - * Mapping from CRTC indices to output indices. - * CRTC's without an output (should be impossible) - * have the value `SIZE_MAX` which is impossible - * for an existing mapping - */ - size_t *crtc_to_output; - - /** - * Screen configuration timestamp - */ - xcb_timestamp_t config_timestamp; - -} libgamma_x_randr_partition_data_t; - - - -/** - * Translate an xcb error into a libgamma error - * - * @param error_code The xcb error - * @param default_error The libgamma error to use if the xcb error is not recognised - * @return The libgamma error - */ -static int -translate_error_(int error_code, int default_error) -{ - switch (error_code) { - case XCB_CONN_ERROR: return errno = ECONNABORTED, LIBGAMMA_ERRNO_SET; - case XCB_CONN_CLOSED_EXT_NOTSUPPORTED: return errno = ENOPROTOOPT, LIBGAMMA_ERRNO_SET; - case XCB_CONN_CLOSED_MEM_INSUFFICIENT: return errno = ENOMEM, LIBGAMMA_ERRNO_SET; - case XCB_CONN_CLOSED_REQ_LEN_EXCEED: return errno = EMSGSIZE, LIBGAMMA_ERRNO_SET; - case XCB_CONN_CLOSED_PARSE_ERR: return LIBGAMMA_NO_SUCH_SITE; - case XCB_CONN_CLOSED_INVALID_SCREEN: return LIBGAMMA_NO_SUCH_PARTITION; - case XCB_CONN_CLOSED_FDPASSING_FAILED: return errno = EIO, LIBGAMMA_ERRNO_SET; - default: - return default_error; - } -} - - -/** - * Translate an xcb error into a libgamma error - * - * @param error_code The xcb error - * @param default_error The libgamma error to use if the xcb error is not recognised - * @param return_errno Whether an `errno` value may be returned - * @return The libgamma error - */ -static int -translate_error(int error_code, int default_error, int return_errno) -{ - int r = translate_error_(error_code, default_error); - return (return_errno && r > 0) ? errno : r; -} - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void -libgamma_x_randr_method_capabilities(libgamma_method_capabilities_t *restrict this) -{ - char *display = getenv("DISPLAY"); - /* Support for all information except active status and gamma ramp support. - Active status can be queried but it is not guaranteed produces an up to date result. */ - this->crtc_information = LIBGAMMA_CRTC_INFO_MACRO_EDID - | LIBGAMMA_CRTC_INFO_MACRO_VIEWPORT - | LIBGAMMA_CRTC_INFO_MACRO_RAMP - | LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER - | LIBGAMMA_CRTC_INFO_MACRO_CONNECTOR; - /* X RandR supports multiple sites, partitions and CRTC:s */ - this->default_site_known = display && *display; - this->multiple_sites = 1; - this->multiple_partitions = 1; - this->multiple_crtcs = 1; - /* Partitions are screens and not graphics cards in X */ - this->partitions_are_graphics_cards = 0; - /* X does not have system restore capabilities */ - this->site_restore = 0; - this->partition_restore = 0; - this->crtc_restore = 0; - /* Gamma ramp sizes are identical but not fixed */ - this->identical_gamma_sizes = 1; - this->fixed_gamma_size = 0; - /* Gamma ramp depths are fixed */ - this->fixed_gamma_depth = 1; - /* X RandR is a real non-faked adjustment method */ - this->real = 1; - this->fake = 0; - /* Gamma ramp adjustments are persistent */ - this->auto_restore = 0; -} - - -/* xcb violates the rule to never return struct:s */ -#ifdef __GNUC__ -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Waggregate-return" -#endif - - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_randr_site_initialise(libgamma_site_state_t *restrict this, char *restrict site) -{ - xcb_generic_error_t *error = NULL; - xcb_connection_t *restrict connection; - xcb_randr_query_version_cookie_t cookie; - xcb_randr_query_version_reply_t *restrict reply; - const xcb_setup_t *restrict setup; - xcb_screen_iterator_t iter; - - /* Connect to the display server */ - this->data = connection = xcb_connect(site, NULL); - if (!connection || xcb_connection_has_error(connection)) - return LIBGAMMA_OPEN_SITE_FAILED; - - /* Query the version of the X RandR extension protocol */ - cookie = xcb_randr_query_version(connection, RANDR_VERSION_MAJOR, RANDR_VERSION_MINOR); - reply = xcb_randr_query_version_reply(connection, cookie, &error); - - /* Check for version query failure */ - if (error || !reply) { - /* Release resources */ - free(reply); - /* If `xcb_connect` failed, both `error` and `reply` will be `NULL`. - XXX: Can both be `NULL` for any other reason? */ - if (!error && !reply) - return LIBGAMMA_OPEN_SITE_FAILED; - xcb_disconnect(connection); - /* Translate and report error. */ - if (error != NULL) - return translate_error(error->error_code, LIBGAMMA_PROTOCOL_VERSION_QUERY_FAILED, 0); - return LIBGAMMA_PROTOCOL_VERSION_QUERY_FAILED; - } - - /* Check protocol compatibility, - we require 1.3 but 2.x may not be backwards compatible */ - if (reply->major_version != RANDR_VERSION_MAJOR || reply->minor_version < RANDR_VERSION_MINOR) { -#ifdef DEBUG - /* Print used protocol */ - fprintf(stderr, "libgamma: RandR protocol version: %u.%u", reply->major_version, reply->minor_version); -#endif - /* Release resources */ - free(reply); - xcb_disconnect(connection); - /* Report error */ - return LIBGAMMA_PROTOCOL_VERSION_NOT_SUPPORTED; - } - - /* We do not longer need to know the version of the protocol */ - free(reply); - - /* Get available screens */ - setup = xcb_get_setup(connection); - if (!setup) { - xcb_disconnect(connection); - return LIBGAMMA_LIST_PARTITIONS_FAILED; - } - iter = xcb_setup_roots_iterator(setup); - /* Get the number of available screens */ - this->partitions_available = (size_t)iter.rem; - - /* Sanity check the number of available screens. */ - return iter.rem >= ? 0 : LIBGAMMA_NEGATIVE_PARTITION_COUNT; -} - - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void -libgamma_x_randr_site_destroy(libgamma_site_state_t *restrict this) -{ - xcb_disconnect((xcb_connection_t *)this->data); -} - - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_randr_site_restore(libgamma_site_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - -/** - * Duplicate a memory area - * - * @param ptr The memory area - * @param bytes The size, in bytes, of the memory area - * @return A duplication of the memory, `NULL` if zero-length or on error - */ -static inline void * -memdup(void *restrict ptr, size_t bytes) -{ - char *restrict rc; - if (!bytes) - return NULL; - rc = malloc(bytes); - if (!rc) - return NULL; - memcpy(rc, ptr, bytes); - return rc; -} - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to. - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_randr_partition_initialise(libgamma_partition_state_t *restrict this, - libgamma_site_state_t *restrict site, size_t partition) -{ - int fail_rc = LIBGAMMA_ERRNO_SET; - xcb_connection_t *restrict connection = site->data; - xcb_screen_t *restrict screen = NULL; - xcb_generic_error_t *error = NULL; - const xcb_setup_t *restrict setup; - xcb_screen_iterator_t iter; - xcb_randr_get_screen_resources_current_cookie_t cookie; - xcb_randr_get_screen_resources_current_reply_t *restrict reply; - xcb_randr_crtc_t *restrict crtcs; - xcb_randr_output_t *restrict outputs; - libgamma_x_randr_partition_data_t *restrict data; - xcb_randr_get_output_info_cookie_t out_cookie; - xcb_randr_get_output_info_reply_t *out_reply; - size_t i; - uint16_t j; - - /* Get screen list */ - setup = xcb_get_setup(connection); - if (!setup) - return LIBGAMMA_LIST_PARTITIONS_FAILED; - iter = xcb_setup_roots_iterator(setup); - - /* Get the screen */ - for (i = 0; iter.rem > 0; i++, xcb_screen_next(&iter)) - if (i == partition) { - screen = iter.data; - break; - } - /* Report failure if we did not find the screen */ - if (!iter.rem) - return LIBGAMMA_NO_SUCH_PARTITION; - - /* Check that the screen is not `NULL`. - * (Do not think this can happen, but why not.) */ - if (!screen) - return LIBGAMMA_NULL_PARTITION; - - /* Get the current resources of the screen */ - cookie = xcb_randr_get_screen_resources_current(connection, screen->root); - reply = xcb_randr_get_screen_resources_current_reply(connection, cookie, &error); - if (error) - return translate_error(error->error_code, LIBGAMMA_LIST_CRTCS_FAILED, 0); - - /* Get the number of available CRTC:s */ - this->crtcs_available = reply->num_crtcs; - /* Get the CRTC and output lists */ - crtcs = xcb_randr_get_screen_resources_current_crtcs(reply); - outputs = xcb_randr_get_screen_resources_current_outputs(reply); - if (!crtcs || !outputs) { - free(reply); - return LIBGAMMA_REPLY_VALUE_EXTRACTION_FAILED; - } - - /* Allocate adjustment method dependent data memory area. - We use `calloc` because we want `data`'s pointers to be `NULL` if not allocated at `fail`. */ - data = calloc(1, sizeof(libgamma_x_randr_partition_data_t)); - if (!data) - goto fail; - - /* Copy the CRTC:s, just so we do not have to keep the reply in memory */ - data->crtcs = memdup(crtcs, (size_t)(reply->num_crtcs) * sizeof(xcb_randr_crtc_t)); - if (!data->crtcs && reply->num_crtcs > 0) - goto fail; - - /* Copy the outputs as well */ - data->outputs = memdup(outputs, (size_t)reply->num_outputs * sizeof(xcb_randr_output_t)); - if (!data->outputs && reply->num_outputs > 0) - goto fail; - - /* Get the number of available outputs */ - data->outputs_count = (size_t)reply->num_outputs; - - /* Create mapping table from CRTC indices to output indicies. (injection) */ - data->crtc_to_output = malloc((size_t)reply->num_crtcs * sizeof(size_t)); - if (!data->crtc) - goto fail; - /* All CRTC:s should be mapped, but incase they are not, all unmapped CRTC:s should have - an invalid target, namely `SIZE_MAX`, which is 1 more than the theoretical limit */ - for (i = 0; i < (size_t)reply->num_crtcs; i++) - data->crtc_to_output[i] = SIZE_MAX; - /* Fill the table */ - for (i = 0; i < (size_t)reply->num_outputs; i++) { - /* Query output (target) information */ - out_cookie = xcb_randr_get_output_info(connection, outputs[i], reply->config_timestamp); - out_reply = xcb_randr_get_output_info_reply(connection, out_cookie, &error); - if (error) { - fail_rc = translate_error(error->error_code, LIBGAMMA_OUTPUT_INFORMATION_QUERY_FAILED, 0); - goto fail; - } - - /* Find CRTC (source) */ - for (j = 0; j < reply->num_crtcs; j++) { - if (crtcs[j] == out_reply->crtc) { - data->crtc_to_output[j] = i; - break; - } - } - - /* Release output information */ - free(out_reply); - } - - /* Store the configuration timestamp */ - data->config_timestamp = reply->config_timestamp; - /* Store the adjustment method dependent data */ - this->data = data; - /* Release resources and return successfully */ - free(reply); - return 0; - -fail: - /* Release resources and return with an error */ - if (data) { - free(data->crtcs); - free(data->outputs); - free(data->crtc_to_output); - free(data); - } - free(reply); - return fail_rc; -} - - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void -libgamma_x_randr_partition_destroy(libgamma_partition_state_t *restrict this) -{ - libgamma_x_randr_partition_data_t *restrict data = this->data; - free(data->crtcs); - free(data->outputs); - free(data->crtc_to_output); - free(data); -} - - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_randr_partition_restore(libgamma_partition_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to. - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_randr_crtc_initialise(libgamma_crtc_state_t *restrict this, - libgamma_partition_state_t *restrict partition, size_t crtc) -{ - libgamma_x_randr_partition_data_t *restrict screen_data = partition->data; - xcb_randr_crtc_t *restrict crtc_ids = screen_data->crtcs; - this->data = crtc_ids + crtc; - return crtc < partition->crtcs_available ? 0 : LIBGAMMA_NO_SUCH_CRTC; -} - - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void -libgamma_x_randr_crtc_destroy(libgamma_crtc_state_t *restrict this) -{ - (void) this; -} - - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_randr_crtc_restore(libgamma_crtc_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - - -/** - * Get the gamma ramp size of a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @return Non-zero on error - */ -static int -get_gamma_ramp_size(libgamma_crtc_information_t *restrict out, libgamma_crtc_state_t *restrict crtc) -{ - xcb_connection_t *restrict connection = crtc->partition->site->data; - xcb_randr_crtc_t *restrict crtc_id = crtc->data; - xcb_randr_get_crtc_gamma_size_cookie_t cookie; - xcb_randr_get_crtc_gamma_size_reply_t *restrict reply; - xcb_generic_error_t *error; - - /* Query gamma ramp size */ - out->gamma_size_error = 0; - cookie = xcb_randr_get_crtc_gamma_size(connection, *crtc_id); - reply = xcb_randr_get_crtc_gamma_size_reply(connection, cookie, &error); - if (error) { - out->gamma_size_error = translate_error(error->error_code, LIBGAMMA_GAMMA_RAMPS_SIZE_QUERY_FAILED, 1); - return out->gamma_size_error; - } - /* Sanity check gamma ramp size */ - if (reply->size < 2) - out->gamma_size_error = LIBGAMMA_SINGLETON_GAMMA_RAMP; - /* Store gamma ramp size */ - out->red_gamma_size = out->green_gamma_size = out->blue_gamma_size = reply->size; - /* Release resources and return successfulnes */ - free(reply); - return out->gamma_size_error; -} - - -/** - * Read information from the CRTC's output - * - * @param out Instance of a data structure to fill with the information about the CRTC - * @param output The CRTC's output information - * @return Non-zero if at least on error occured - */ -static int -read_output_data(libgamma_crtc_information_t *restrict out, xcb_randr_get_output_info_reply_t *restrict output) -{ -#define __select(value)\ - case XCB_RENDER_SUB_PIXEL_##value: out->subpixel_order = LIBGAMMA_SUBPIXEL_ORDER_##value; break - - switch (output->connection) { - case XCB_RANDR_CONNECTION_CONNECTED: - /* We have a monitor connected, report that and store information that is provided to us */ - out->active = 1; - out->width_mm = output->mm_width; - out->height_mm = output->mm_height; - switch (output->subpixel_order) { - __select (UNKNOWN); - __select (HORIZONTAL_RGB); - __select (HORIZONTAL_BGR); - __select (VERTICAL_RGB); - __select (VERTICAL_BGR); - __select (NONE); - default: - out->subpixel_order_error = LIBGAMMA_SUBPIXEL_ORDER_NOT_RECOGNISED; - break; - } - return 0; - - case XCB_RANDR_CONNECTION_UNKNOWN: - /* If we do know whether a monitor is connected report that and assume it is not */ - out->active_error = LIBGAMMA_STATE_UNKNOWN; - /* fall through */ - default: - /* If no monitor is connected, report that on fails that require it */ - out->width_mm_error = LIBGAMMA_NOT_CONNECTED; - out->height_mm_error = LIBGAMMA_NOT_CONNECTED; - out->subpixel_order_error = LIBGAMMA_NOT_CONNECTED; - /* And store that we are not connected */ - out->active = 0; - /* This fuction only failed if we could not figure out whether a monitor is connected */ - return output->connection != XCB_RANDR_CONNECTION_UNKNOWN ? 0 : -1; - } - -#undef __select -} - - -/** - * Determine the connector type from the connector name - * - * @param this The CRTC information to use and extend - * @param Non-zero on error - */ -static int -get_connector_type(libgamma_crtc_information_t *restrict this) -{ - /* Since we require the name of the output of get the type of the connected, - copy any reported error on the output's name to the connector's type, - and report failure if there was an error */ - if ((this->connector_type_error = this->connector_name_error)) - return -1; - -#define __select(name, type)\ - do {\ - if (strstr(this->connector_name, name "-") == this->connector_name) {\ - this->connector_type = LIBGAMMA_CONNECTOR_TYPE_##type\ - return 0;\ - }\ - } while (0) - - /* Check begin on the name of the output to find out what type the connector is of */ - __select ("None", Unknown); - __select ("VGA", VGA); - __select ("DVI-I", DVII); - __select ("DVI-D", DVID); - __select ("DVI-A", DVIA); - __select ("DVI", DVI); - __select ("Composite", Composite); - __select ("S-Video", SVIDEO); - __select ("Component", Component); - __select ("LFP", LFP); - __select ("Proprietary", Unknown); - __select ("HDMI", HDMI); - __select ("DisplayPort", DisplayPort); - -#undef __select - - /* If there was no matching output name pattern report that and exit with an error */ - this->connector_name_error = LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED; - return -1; -} - - -/** - * Get the output name of a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param output The CRTC's output's information - * @return Non-zero on error - */ -static int -get_output_name(libgamma_crtc_information_t *restrict out, xcb_randr_get_output_info_reply_t *restrict output) -{ - char *restrict store; - uint8_t *restrict name; - uint16_t length; - size_t i; - - /* Get the name of the output and the length of that name */ - name = xcb_randr_get_output_info_name(output); - length = output->name_len; /* There is no NUL-termination */ - if (!name) - return out->connector_name_error = LIBGAMMA_REPLY_VALUE_EXTRACTION_FAILED; - - /* Allocate a memory area for a NUL-terminated copy of the name */ - store = out->connector_name = malloc(((size_t)length + 1) * sizeof(char)); - if (!store) { - out->connector_name_error = errno; - return -1; - } - - /* char is guaranteed to be (u)int_least8_t, but it is only guaranteed to be (u)int8_t - * on POSIX, so to be truly portable we will not assume that char is (u)int8_t */ - for (i = 0; i < (size_t)length; i++) - store[i] = (char)name[i]; - store[length] = '\0'; - - return 0; -} - - -/** - * Get the Extended Display Information Data of the monitor connected to the connector of a CRTC - * - * @param out Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param output The CRTC's output - * @return Non-zero on error - */ -static int -get_edid(libgamma_crtc_information_t *restrict out, libgamma_crtc_state_t *restrict crtc, xcb_randr_output_t output) -{ - xcb_connection_t *restrict connection = crtc->partition->site->data; - xcb_randr_list_output_properties_cookie_t prop_cookie; - xcb_randr_list_output_properties_reply_t *restrict prop_reply; - xcb_atom_t *atoms; - xcb_atom_t *atoms_end; - xcb_generic_error_t *error; - xcb_get_atom_name_cookie_t atom_name_cookie; - xcb_get_atom_name_reply_t *restrict atom_name_reply; - char *restrict atom_name; - int atom_name_len; - xcb_randr_get_output_property_cookie_t atom_cookie; - xcb_randr_get_output_property_reply_t *restrict atom_reply; - unsigned char* restrict atom_data; - int length; - - /* Acquire a list of all properties of the output */ - prop_cookie = xcb_randr_list_output_properties(connection, output); - prop_reply = xcb_randr_list_output_properties_reply(connection, prop_cookie, &error); - if (error) - return out->edid_error = translate_error(error->error_code, LIBGAMMA_LIST_PROPERTIES_FAILED, 1); - - /* Extract the properties form the data structure that holds them, */ - atoms = xcb_randr_list_output_properties_atoms(prop_reply); - /* and get the last one so that we can iterate over them nicely */ - atoms_end = atoms + xcb_randr_list_output_properties_atoms_length(prop_reply); - - if (!atoms) { - free(prop_reply); - return out->edid_error = LIBGAMMA_REPLY_VALUE_EXTRACTION_FAILED; - } - - /* For each property */ - for (; atoms != atoms_end; atoms++) { - /* Acquire the atom name */ - atom_name_cookie = xcb_get_atom_name(connection, *atoms); - atom_name_reply = xcb_get_atom_name_reply(connection, atom_name_cookie, &error); - if (error) - continue; - - /* Extract the atom name from the data structure that holds it */ - atom_name = xcb_get_atom_name_name(atom_name_reply); - /* As well as the length of the name; it is not NUL-termianted */ - atom_name_len = xcb_get_atom_name_name_length(atom_name_reply); - - if (/* Check for errors */ - !atom_name || /* atom_name_len < 1 || */ - /* Check that the length is the expected length for the EDID property */ - atom_name_len != 4 || - /* Check that the property is the EDID property */ - atom_name[0] != 'E' || atom_name[1] != 'D' || atom_name[2] != 'I' || atom_name[3] != 'D') { - free(atom_name_reply); - continue; - } - - /* Acquire the property's value, we know that it is either 128 or 256 byte long */ - atom_cookie = xcb_randr_get_output_property(connection, output, *atoms, XCB_GET_PROPERTY_TYPE_ANY, 0, 256, 0, 0); - atom_reply = xcb_randr_get_output_property_reply(connection, atom_cookie, &error); - /* (*) EDID version 1.0 through 1.4 define it as 128 bytes long, - * but version 2.0 define it as 256 bytes long. However, - * version 2.0 is rare(?) and has been deprecated and replaced - * by version 1.3 (I guess that is with a new version epoch, - * but I do not know.) */ - if (error) { - free(atom_name_reply); - free(prop_reply); - return out->edid_error = LIBGAMMA_PROPERTY_VALUE_QUERY_FAILED; - } - - /* Extract the property's value */ - atom_data = xcb_randr_get_output_property_data(atom_reply); - /* and its actual length */ - length = xcb_randr_get_output_property_data_length(atom_reply); - if (!atom_data || length < 1) { - free(atom_reply); - free(atom_name_reply); - free(prop_reply); - return out->edid_error = LIBGAMMA_REPLY_VALUE_EXTRACTION_FAILED; - } - - /* Store the EDID */ - out->edid_length = (size_t)length; - out->edid = malloc((size_t)length * sizeof(unsigned char)); - if (!out->edid) - out->edid_error = errno; - else - memcpy(out->edid, atom_data, (size_t)length * sizeof(unsigned char)); - - /* Release resouces */ - free(atom_reply); - free(atom_name_reply); - free(prop_reply); - - return out->edid_error; - } - - return out->edid_error = LIBGAMMA_EDID_NOT_FOUND; -} - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error; on error refer to the error reports in `this` - */ -int -libgamma_x_randr_get_crtc_information(libgamma_crtc_information_t *restrict this, - libgamma_crtc_state_t *restrict crtc, int32_t fields) -{ -#define _E(FIELD) ((fields & FIELD) ? LIBGAMMA_CRTC_INFO_NOT_SUPPORTED : 0) - - int e = 0; - xcb_randr_get_output_info_reply_t *restrict output_info = NULL; - xcb_randr_output_t output; - int free_edid, free_name; - xcb_connection_t *restrict connection; - libgamma_x_randr_partition_data_t *restrict screen_data; - size_t output_index; - xcb_randr_get_output_info_cookie_t cookie; - xcb_generic_error_t *error; - - /* Wipe all error indicators */ - memset(this, 0, sizeof(libgamma_crtc_information_t)); - - /* We need to free the EDID after us if it is not explicitly requested */ - free_edid = !(fields & LIBGAMMA_CRTC_INFO_EDID); - - /* We need to free the output's name after us if it is not explicitly requested */ - free_name = !(fields & LIBGAMMA_CRTC_INFO_CONNECTOR_NAME); - - /* Jump if the output information is not required */ - if (!(fields & (LIBGAMMA_CRTC_INFO_MACRO_ACTIVE | LIBGAMMA_CRTC_INFO_MACRO_CONNECTOR))) - goto cont; - - /* Get connector and connector information */ - connection = crtc->partition->site->data; - screen_data = crtc->partition->data; - output_index = screen_data->crtc_to_output[crtc->crtc]; - /* `SIZE_MAX` is used for CRTC:s that misses mapping to its output (should not happen), - * because `SIZE_MAX - 1` is the highest theoretical possible value */ - if (output_index == SIZE_MAX) { - e |= this->edid_error = this->gamma_error = this->width_mm_edid_error - = this->height_mm_edid_error = this->connector_type_error - = this->connector_name_error = this->subpixel_order_error - = this->width_mm_error = this->height_mm_error - = this->active_error = LIBGAMMA_CONNECTOR_UNKNOWN; - goto cont; - } - /* Get the output */ - output = screen_data->outputs[output_index]; - /* Query output information */ - cookie = xcb_randr_get_output_info(connection, output, screen_data->config_timestamp); - output_info = xcb_randr_get_output_info_reply(connection, cookie, &error); - if (error) { - e |= this->edid_error = this->gamma_error = this->width_mm_edid_error - = this->height_mm_edid_error = this->connector_type_error - = this->connector_name_error = this->subpixel_order_error - = this->width_mm_error = this->height_mm_error - = this->active_error = LIBGAMMA_OUTPUT_INFORMATION_QUERY_FAILED; - goto cont; - } - - /* Get connector name */ - e |= get_output_name(this, output_info); - /* Get connector type */ - if (fields & LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE) - e |= get_connector_type(this); - /* Get additional output data, excluding EDID */ - e |= read_output_data(this, output_info); - if (fields & LIBGAMMA_CRTC_INFO_MACRO_VIEWPORT) - e |= this->width_mm_error | this->height_mm_error; - e |= (fields & LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER) ? this->subpixel_order_error : 0; - - /* If we do not want any EDID information, jump */ - if (!(fields & LIBGAMMA_CRTC_INFO_MACRO_EDID)) - goto cont; - /* If there is not monitor that report error in EDID related fields */ - if (!this->active) { - e |= this->edid_error = this->gamma_error = this->width_mm_edid_error - = this->height_mm_edid_error = LIBGAMMA_NOT_CONNECTED; - goto cont; - } - /* Get EDID */ - e |= get_edid(this, crtc, output); - if (!this->edid) { - this->gamma_error = this->width_mm_edid_error = this->height_mm_edid_error = this->edid_error; - goto cont; - } - /* Parse EDID */ - if ((fields & (LIBGAMMA_CRTC_INFO_MACRO_EDID ^ LIBGAMMA_CRTC_INFO_EDID))) - e |= libgamma_parse_edid(this, fields); - -cont: - /* Get gamma ramp size */ - e |= (fields & LIBGAMMA_CRTC_INFO_GAMMA_SIZE) ? get_gamma_ramp_size(this, crtc) : 0; - /* Store gamma ramp depth. */ - this->gamma_depth = 16; - /* X RandR does not support quering gamma ramp support. */ - e |= this->gamma_support_error = _E(LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT); - - /* Free the EDID after us */ - if (free_edid) { - free(this->edid); - this->edid = NULL; - } - /* Free the output name after us */ - if (free_name) { - free(this->connector_name); - this->connector_name = NULL; - } - - free(output_info); - return e ? -1 : 0; - -#undef _E -} - - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_randr_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t *restrict ramps) -{ - xcb_connection_t *restrict connection = this->partition->site->data; - xcb_randr_get_crtc_gamma_cookie_t cookie; - xcb_randr_get_crtc_gamma_reply_t *restrict reply; - xcb_generic_error_t *error; - uint16_t *restrict red; - uint16_t *restrict green; - uint16_t *restrict blue; - -#ifdef DEBUG - /* Gamma ramp sizes are identical but not fixed */ - if (ramps->red_size != ramps->green_size || ramps->red_size != ramps->blue_size) - return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; -#endif - - /* Read current gamma ramps */ - cookie = xcb_randr_get_crtc_gamma(connection, *(xcb_randr_crtc_t *)this->data); - reply = xcb_randr_get_crtc_gamma_reply(connection, cookie, &error); - - /* Check for errors */ - if (error) - return translate_error(error->error_code, LIBGAMMA_GAMMA_RAMP_READ_FAILED, 0); - - /* Get current gamma ramps from response */ - red = xcb_randr_get_crtc_gamma_red(reply); - green = xcb_randr_get_crtc_gamma_green(reply); - blue = xcb_randr_get_crtc_gamma_blue(reply); - /* Copy over the gamma ramps to our memory */ - memcpy(ramps->red, red, ramps->red_size * sizeof(uint16_t)); - memcpy(ramps->green, green, ramps->green_size * sizeof(uint16_t)); - memcpy(ramps->blue, blue, ramps->blue_size * sizeof(uint16_t)); - - free(reply); - return 0; -} - - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_randr_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t ramps) -{ - xcb_connection_t *restrict connection = this->partition->site->data; - xcb_void_cookie_t cookie; - xcb_generic_error_t *restrict error; -#ifdef DEBUG - /* Gamma ramp sizes are identical but not fixed */ - if (ramps.red_size != ramps.green_size || ramps.red_size != ramps.blue_size) - return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; -#endif - - /* Apply gamma ramps */ - cookie = xcb_randr_set_crtc_gamma_checked(connection, *(xcb_randr_crtc_t*)this->data, - (uint16_t)ramps.red_size, ramps.red, ramps.green, ramps.blue); - /* Check for errors */ - error = xcb_request_check(connection, cookie); - if (error) - return translate_error(error->error_code, LIBGAMMA_GAMMA_RAMP_WRITE_FAILED, 0); - return 0; -} - - -#ifdef __GNUC__ -# pragma GCC diagnostic pop -#endif diff --git a/src/lib/gamma-x-randr.h b/src/lib/gamma-x-randr.h deleted file mode 100644 index 59c64bc..0000000 --- a/src/lib/gamma-x-randr.h +++ /dev/null @@ -1,139 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_GAMMA_X_RANDR_H -#define LIBGAMMA_GAMMA_X_RANDR_H - -#ifndef HAVE_LIBGAMMA_METHOD_X_RANDR -# error Including gamma-x-randr.h without HAVE_LIBGAMMA_METHOD_X_RANDR -#endif - - -#include "libgamma-method.h" - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void libgamma_x_randr_method_capabilities(libgamma_method_capabilities_t *restrict); - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_randr_site_initialise(libgamma_site_state_t *restrict, char *restrict); - -/** - * Release all resources held by a site state. - * - * @param this The site state. - */ -void libgamma_x_randr_site_destroy(libgamma_site_state_t *restrict); - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_randr_site_restore(libgamma_site_state_t *restrict); - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_randr_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void libgamma_x_randr_partition_destroy(libgamma_partition_state_t *restrict); - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_randr_partition_restore(libgamma_partition_state_t *restrict); - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_randr_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void libgamma_x_randr_crtc_destroy(libgamma_crtc_state_t *restrict) __attribute__((const)); - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_randr_crtc_restore(libgamma_crtc_state_t *restrict); - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error; on error refer to the error reports in `this` - */ -int libgamma_x_randr_get_crtc_information(libgamma_crtc_information_t *restrict, libgamma_crtc_state_t *restrict, int32_t); - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_randr_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_randr_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t); - - -#endif diff --git a/src/lib/gamma-x-vidmode.c b/src/lib/gamma-x-vidmode.c deleted file mode 100644 index bae9d31..0000000 --- a/src/lib/gamma-x-vidmode.c +++ /dev/null @@ -1,312 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef HAVE_LIBGAMMA_METHOD_X_VIDMODE -# error Compiling gamma-x-vidmode.c without HAVE_LIBGAMMA_METHOD_X_VIDMODE -#endif - -#include "gamma-x-vidmode.h" - -#include "libgamma-error.h" - -#include -#include - -#include -#include - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void -libgamma_x_vidmode_method_capabilities(libgamma_method_capabilities_t *restrict this) -{ - char *restrict display = getenv("DISPLAY"); - /* Gamma ramps size and depth can be queried */ - this->crtc_information = LIBGAMMA_CRTC_INFO_GAMMA_SIZE | LIBGAMMA_CRTC_INFO_GAMMA_DEPTH; - /* X VidMode supports multiple sites and partitions but not CRTC:s */ - this->default_site_known = (display && *display) ? 1 : 0; - this->multiple_sites = 1; - this->multiple_partitions = 1; - this->multiple_crtcs = 0; - /* Partitions are screens and not graphics cards in X */ - this->partitions_are_graphics_cards = 0; - /* X does not have system restore capabilities */ - this->site_restore = 0; - this->partition_restore = 0; - this->crtc_restore = 0; - /* Gamma ramp sizes are identical but not fixed */ - this->identical_gamma_sizes = 1; - this->fixed_gamma_size = 0; - /* Gamma ramp depths are fixed */ - this->fixed_gamma_depth = 1; - /* X VidMode is a real non-faked adjustment method */ - this->real = 1; - this->fake = 0; - /* Gamma ramp adjustments are persistent */ - this->auto_restore = 0; -} - - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_vidmode_site_initialise(libgamma_site_state_t *restrict this, char *restrict site) -{ - /* Connect to the display */ - Display *restrict connection; - int _major, _minor, screens; - this->data = connection = XOpenDisplay(site); - if (!this->data) - return LIBGAMMA_OPEN_SITE_FAILED; - /* Query X VidMode extension protocol version */ - if (!XF86VidModeQueryVersion(connection, &_major, &_minor)) { - XCloseDisplay(connection); - return LIBGAMMA_PROTOCOL_VERSION_QUERY_FAILED; - } - /* Query the number of available screens */ - screens = ScreenCount(connection); - if (screens < 0) { - XCloseDisplay(connection); - return LIBGAMMA_NEGATIVE_PARTITION_COUNT; - } - this->partitions_available = (size_t)screens; - return 0; -} - - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void -libgamma_x_vidmode_site_destroy(libgamma_site_state_t *restrict this) -{ - XCloseDisplay((Display *)this->data); -} - - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_vidmode_site_restore(libgamma_site_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_vidmode_partition_initialise(libgamma_partition_state_t *restrict this, - libgamma_site_state_t *restrict site, size_t partition) -{ - this->crtcs_available = 1; - return partition < site->partitions_available ? 0 : LIBGAMMA_NO_SUCH_PARTITION; -} - - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void -libgamma_x_vidmode_partition_destroy(libgamma_partition_state_t *restrict this) -{ - (void) this; -} - - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_vidmode_partition_restore(libgamma_partition_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_vidmode_crtc_initialise(libgamma_crtc_state_t *restrict this, - libgamma_partition_state_t *restrict partition, size_t crtc) -{ - (void) this; - (void) partition; - return !crtc ? 0 : LIBGAMMA_NO_SUCH_CRTC; -} - - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void -libgamma_x_vidmode_crtc_destroy(libgamma_crtc_state_t *restrict this) -{ - (void) this; -} - - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_vidmode_crtc_restore(libgamma_crtc_state_t *restrict this) -{ - (void) this; - errno = ENOTSUP; - return LIBGAMMA_ERRNO_SET; -} - - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error; oOn error refer to the error reports in `this` - */ -int -libgamma_x_vidmode_get_crtc_information(libgamma_crtc_information_t *restrict this, - libgamma_crtc_state_t *restrict crtc, int32_t fields) -{ -#define _E(FIELD) ((fields & FIELD) ? LIBGAMMA_CRTC_INFO_NOT_SUPPORTED : 0) - - Display *restrict connection; - int stops = 0; - - /* X VidMode does not support EDID or monitor dimensions */ - this->edid_error = _E(LIBGAMMA_CRTC_INFO_EDID); - this->width_mm_error = _E(LIBGAMMA_CRTC_INFO_WIDTH_MM); - this->height_mm_error = _E(LIBGAMMA_CRTC_INFO_HEIGHT_MM); - this->width_mm_edid_error = _E(LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID); - this->height_mm_edid_error = _E(LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID); - this->gamma_size_error = 0; - /* X VidMode does support gamma ramp size query. The gamma - ramps are identical but not fixed, and the query can fail. */ - if ((fields & LIBGAMMA_CRTC_INFO_GAMMA_SIZE)) { - connection = crtc->partition->site->data; - if (!XF86VidModeGetGammaRampSize(connection, (int)crtc->partition->partition, &stops)) - this->gamma_size_error = LIBGAMMA_GAMMA_RAMPS_SIZE_QUERY_FAILED; - else if (stops < 2) - this->gamma_size_error = LIBGAMMA_SINGLETON_GAMMA_RAMP; - this->red_gamma_size = this->green_gamma_size = this->blue_gamma_size = (size_t)stops; - } - /* X VidMode uses 16-bit integer ramps */ - this->gamma_depth = 16; - this->gamma_depth_error = 0; - /* X VidMode does not support gamma ramp support queries */ - this->gamma_support_error = _E(LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT); - /* X VidMode does not support EDID or connector information */ - this->subpixel_order_error = _E(LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER); - this->active_error = _E(LIBGAMMA_CRTC_INFO_ACTIVE); - this->connector_name_error = _E(LIBGAMMA_CRTC_INFO_CONNECTOR_NAME); - this->connector_type_error = _E(LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE); - this->gamma_error = _E(LIBGAMMA_CRTC_INFO_GAMMA); - - /* We failed if gamma ramp size query failed or if an unsupport field was queried. */ - return (this->gamma_size_error || (fields & ~(LIBGAMMA_CRTC_INFO_GAMMA_DEPTH | LIBGAMMA_CRTC_INFO_GAMMA_SIZE))) ? -1 : 0; - -#undef _E -} - - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_vidmode_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t *restrict ramps) -{ -#ifdef DEBUG - /* Gamma ramp sizes are identical but not fixed */ - if (ramps->red_size != ramps->green_size || ramps->red_size != ramps->blue_size) - return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; -#endif - /* Read current gamma ramps */ - if (!XF86VidModeGetGammaRamp((Display *)this->partition->site->data, (int)this->partition->partition, - (int)ramps->red_size, ramps->red, ramps->green, ramps->blue)) - return LIBGAMMA_GAMMA_RAMP_READ_FAILED; - return 0; -} - - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_x_vidmode_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t ramps) -{ -#ifdef DEBUG - /* Gamma ramp sizes are identical but not fixed */ - if (ramps.red_size != ramps.green_size || ramps.red_size != ramps.blue_size) - return LIBGAMMA_MIXED_GAMMA_RAMP_SIZE; -#endif - /* Apply gamma ramps */ - if (!XF86VidModeSetGammaRamp((Display *)this->partition->site->data, (int)this->partition->partition, - (int)ramps.red_size, ramps.red, ramps.green, ramps.blue)) - return LIBGAMMA_GAMMA_RAMP_WRITE_FAILED; - return 0; -} diff --git a/src/lib/gamma-x-vidmode.h b/src/lib/gamma-x-vidmode.h deleted file mode 100644 index 618d5c4..0000000 --- a/src/lib/gamma-x-vidmode.h +++ /dev/null @@ -1,140 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_GAMMA_X_VIDMODE_H -#define LIBGAMMA_GAMMA_X_VIDMODE_H - -#ifndef HAVE_LIBGAMMA_METHOD_X_VIDMODE -# error Including gamma-x-vidmode.h without HAVE_LIBGAMMA_METHOD_X_VIDMODE -#endif - - -#include "libgamma-method.h" - - -/** - * Return the capabilities of the adjustment method - * - * @param this The data structure to fill with the method's capabilities - */ -void libgamma_x_vidmode_method_capabilities(libgamma_method_capabilities_t *restrict); - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be free:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_vidmode_site_initialise(libgamma_site_state_t *restrict, char *restrict); - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void libgamma_x_vidmode_site_destroy(libgamma_site_state_t *restrict); - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_vidmode_site_restore(libgamma_site_state_t *restrict); - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The the index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_vidmode_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void libgamma_x_vidmode_partition_destroy(libgamma_partition_state_t *restrict) __attribute__((const)); - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_vidmode_partition_restore(libgamma_partition_state_t *restrict); - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The the index of the CRTC within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -__attribute__((const)) -int libgamma_x_vidmode_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void libgamma_x_vidmode_crtc_destroy(libgamma_crtc_state_t *restrict) __attribute__((const)); - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_vidmode_crtc_restore(libgamma_crtc_state_t *restrict); - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error; on error refer to the error reports in `this` - */ -int libgamma_x_vidmode_get_crtc_information(libgamma_crtc_information_t *restrict, libgamma_crtc_state_t *restrict, int32_t); - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_vidmode_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_x_vidmode_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t); - - -#endif diff --git a/src/lib/libgamma-error.c.gpp b/src/lib/libgamma-error.c.gpp deleted file mode 100644 index d43f336..0000000 --- a/src/lib/libgamma-error.c.gpp +++ /dev/null @@ -1,178 +0,0 @@ -/* -*- c -*- */ -/* See LICENSE file for copyright and license details. */ -#include "libgamma-error.h" - - -#include -#include -#include -#include -#include - - - -$>set -u -$>cd src/extract -$>export PATH=".:${PATH}" - - - -/** - * Group that the user needs to be a member of if - * `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned - */ -#ifndef __WIN32__ -__thread gid_t libgamma_group_gid = 0; -gid_t -libgamma_group_gid_get(void) -{ - return libgamma_group_gid; -} -void -libgamma_group_gid_set(gid_t value) -{ - libgamma_group_gid = value; -} -#else -short libgamma_group_gid = 0; -#endif - -/** - * Group that the user needs to be a member of if - * `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned, - * `NULL` if the name of the group `libgamma_group_gid` - * cannot be determined - */ -#ifndef __WIN32__ -__thread const char *libgamma_group_name = NULL; -const char * -libgamma_group_name_get(void) { - return libgamma_group_name; -} -void -libgamma_group_name_set(const char *value) -{ - libgamma_group_name = value; -} -#else -const char *libgamma_group_name = NULL; -#endif - - - -/** - * Prints an error to stderr in a `perror` fashion, - * however this function will not translate the `libgamma` - * errors into human-readable strings, it will simply - * print the name of the error. If the value `error_code` - * is the value of `LIBGAMMA_ERRNO_SET`, `perror` will be - * used to print the current error stored in `errno`. - * If `error_code` is non-negative (an `errno` value`), that - * value will be stored in `errno` and `perror` will be - * used to print it. Additionally, if the `error_code` is - * the value of `LIBGAMMA_DEVICE_REQUIRE_GROUP` the - * required group will be printed with its numerical value - * and, if known, its name. - * - * @param name The text to add at the beginning - * @param value The error code, may be an `errno` value - */ -void -libgamma_perror(const char *name, int error_code) -{ - const char *error; - long int gid; - - if (error_code >= 0) { - /* Print the stored errno value */ - errno = error_code; - perror(name); - } else if (error_code == LIBGAMMA_ERRNO_SET) { - /* Print errno */ - perror(name); - } else if (error_code == LIBGAMMA_DEVICE_REQUIRE_GROUP) { - /* Print the error name and the required group membership */ - error = libgamma_name_of_error(error_code); - gid = (long int)libgamma_group_gid; - if (!libgamma_group_name) { - /* Group name unknown */ - if (name && *name) - fprintf(stderr, "%s: %s: %ld\n", name, error, gid); - else - fprintf(stderr, "%s: %ld\n", error, gid); - } else { - /* Group name known, ID is second class */ - if (name && *name) - fprintf(stderr, "%s: %s: %s (%ld)\n", name, error, libgamma_group_name, gid); - else - fprintf(stderr, "%s: %s (%ld)\n", error, libgamma_group_name, gid); - } - } else if (error_code < LIBGAMMA_ERROR_MIN) { - /* If the error code does not exist, print "(?)" */ - if (name && *name) - fprintf(stderr, "%s: (?)\n", name); - else - fprintf(stderr, "(?)\n"); - } else { - /* Print the name of the error */ - if (name && *name) - fprintf(stderr, "%s: %s\n", name, libgamma_name_of_error(error_code)); - else - fprintf(stderr, "%s\n", libgamma_name_of_error(error_code)); - } -} - - -/** - * Returns the name of the definition associated with a `libgamma` error code - * - * @param value The error code - * @return The name of the definition associated with the error code, - * `NULL` if the error code does not exist; the return string - * should not be `free`:d - */ -const char * -libgamma_name_of_error(int value) -{ - /* Map from error codes to error names. - The output of $(libgamma-error-extract --list) - is sorted by error code in decreasing order */ - static const char* error_names[] = { -$>for error in $(libgamma-error-extract --list); do - "${error}", -$>done - }; - - /* Return `NULL` if the error code is invalid */ - if (value < LIBGAMMA_ERROR_MIN || value >= 0) - return NULL; - - /* Convert error code from {-1, -2, -3, ...} to {0, 1, 2, ...} - * and look up the error's name and return it */ - return error_names[-value - 1]; -} - - -/** - * Return the value of a `libgamma` error definition refered to by name - * - * @param name The name of the definition associated with the error code - * @return The error code, zero if the name does is `NULL` - * or does not refer to a `libgamma` error - */ -int -libgamma_value_of_error(const char *name) -{ - /* Return 0 (not a valid error code) if the error name is `NULL` */ - if (!name) - return 0; - - /* Test error names against `name` and return the value of the match error */ -$>for error in $(libgamma-error-extract --list); do - if (!strcmp(name, "${error}")) - return ${error}; -$>done - - /* Return 0 (not a valid error code) if the error name is unknown */ - return 0; -} diff --git a/src/lib/libgamma-error.h b/src/lib/libgamma-error.h deleted file mode 100644 index a026e79..0000000 --- a/src/lib/libgamma-error.h +++ /dev/null @@ -1,379 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_ERROR_H -#define LIBGAMMA_ERROR_H - -#if !defined(LIBGAMMA_CONFIG_H) && !defined(DEBUG) -# error libgamma-error.h should not be included directly, include libgamma.h instead -#endif - - -#include - -#ifndef __GNUC__ -# define __attribute__(x) -#endif - - -/** - * Group that the user needs to be a member of if - * `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned - */ -#ifndef __WIN32__ -extern __thread gid_t libgamma_group_gid; -gid_t libgamma_group_gid_get(void) __attribute__((pure)); -void libgamma_group_gid_set(gid_t value); -#else -extern short libgamma_group_gid; -#endif - -/** - * Group that the user needs to be a member of if - * `LIBGAMMA_DEVICE_REQUIRE_GROUP` is returned, - * `NULL` if the name of the group `libgamma_group_gid` - * cannot be determined - */ -#ifndef __WIN32__ -extern __thread const char *libgamma_group_name; -const char *libgamma_group_name_get(void) __attribute__((pure)); -void libgamma_group_name_set(const char *value); -#else -extern const char *libgamma_group_name; -#endif - - -/** - * `errno` has be set with a standard error number - * to indicate the what has gone wrong - */ -#define LIBGAMMA_ERRNO_SET (-1) - -/** - * The selected adjustment method does not exist - * or has been excluded at compile-time - */ -#define LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD (-2) - -/** - * The selected site does not exist - */ -#define LIBGAMMA_NO_SUCH_SITE (-3) - -/** - * The selected partition does not exist - */ -#define LIBGAMMA_NO_SUCH_PARTITION (-4) - -/** - * The selected CRTC does not exist - */ -#define LIBGAMMA_NO_SUCH_CRTC (-5) - -/** - * Counter overflowed when counting the number - * of available items - */ -#define LIBGAMMA_IMPOSSIBLE_AMOUNT (-6) - -/** - * The selected connector is disabled, it does - * not have a CRTC - */ -#define LIBGAMMA_CONNECTOR_DISABLED (-7) - -/** - * The selected CRTC could not be opened, - * reason unknown - */ -#define LIBGAMMA_OPEN_CRTC_FAILED (-8) - -/** - * The CRTC information field is not supported - * by the adjustment method - */ -#define LIBGAMMA_CRTC_INFO_NOT_SUPPORTED (-9) - -/** - * Failed to read the current gamma ramps for - * the selected CRTC, reason unknown - */ -#define LIBGAMMA_GAMMA_RAMP_READ_FAILED (-10) - -/** - * Failed to write the current gamma ramps for - * the selected CRTC, reason unknown - */ -#define LIBGAMMA_GAMMA_RAMP_WRITE_FAILED (-11) - -/** - * The specified ramp sizes does not match the - * ramps sizes returned by the adjustment methods - * in response to the query/command - */ -#define LIBGAMMA_GAMMA_RAMP_SIZE_CHANGED (-12) - -/** - * The specified ramp sizes are not identical - * which is required by the adjustment method - * - * (Only returned in debug mode) - */ -#define LIBGAMMA_MIXED_GAMMA_RAMP_SIZE (-13) - -/** - * The specified ramp sizes are not supported - * by the adjustment method - * - * (Only returned in debug mode) - */ -#define LIBGAMMA_WRONG_GAMMA_RAMP_SIZE (-14) - -/** - * The adjustment method reported that the gamma - * ramps size is 1, or perhaps even zero or negative - */ -#define LIBGAMMA_SINGLETON_GAMMA_RAMP (-15) - -/** - * The adjustment method failed to list - * available CRTC:s, reason unknown - */ -#define LIBGAMMA_LIST_CRTCS_FAILED (-16) - -/** - * Failed to acquire mode resources from the - * adjustment method - */ -#define LIBGAMMA_ACQUIRING_MODE_RESOURCES_FAILED (-17) - -/** - * The adjustment method reported that a negative - * number of partitions exists in the site - */ -#define LIBGAMMA_NEGATIVE_PARTITION_COUNT (-18) - -/** - * The adjustment method reported that a negative - * number of CRTC:s exists in the partition - */ -#define LIBGAMMA_NEGATIVE_CRTC_COUNT (-19) - -/** - * Device cannot be access becauses of - * insufficient permissions - */ -#define LIBGAMMA_DEVICE_RESTRICTED (-20) - -/** - * Device cannot be access, reason unknown - */ -#define LIBGAMMA_DEVICE_ACCESS_FAILED (-21) - -/** - * Device cannot be access, membership of the - * `libgamma_group_gid` (named by `libgamma_group_name` - * (can be `NULL`, if so `errno` may have been set - * to tell why)) is required - */ -#define LIBGAMMA_DEVICE_REQUIRE_GROUP (-22) - -/** - * The graphics card appear to have been removed - */ -#define LIBGAMMA_GRAPHICS_CARD_REMOVED (-23) - -/** - * The state of the requested information is unknown - */ -#define LIBGAMMA_STATE_UNKNOWN (-24) - -/** - * Failed to determine which connector the - * CRTC belongs to - */ -#define LIBGAMMA_CONNECTOR_UNKNOWN (-25) - -/** - * The detected connector type is not listed - * in this library and has to be updated - */ -#define LIBGAMMA_CONNECTOR_TYPE_NOT_RECOGNISED (-26) - -/** - * The detected subpixel order is not listed - * in this library and has to be updated - */ -#define LIBGAMMA_SUBPIXEL_ORDER_NOT_RECOGNISED (-27) - -/** - * The length of the EDID does not match that - * of any supported EDID structure revision - */ -#define LIBGAMMA_EDID_LENGTH_UNSUPPORTED (-28) - -/** - * The magic number in the EDID does not match - * that of any supported EDID structure revision - */ -#define LIBGAMMA_EDID_WRONG_MAGIC_NUMBER (-29) - -/** - * The EDID structure revision used by the - * monitor is not supported - */ -#define LIBGAMMA_EDID_REVISION_UNSUPPORTED (-30) - -/** - * The gamma characteristics field in the EDID - * is left unspecified - * - * (This could be considered a non-error) - */ -#define LIBGAMMA_GAMMA_NOT_SPECIFIED (-31) - -/** - * The checksum in the EDID is incorrect, all - * request information has been provided - * by you cannot count on it - */ -#define LIBGAMMA_EDID_CHECKSUM_ERROR (-32) - -/** - * Both of the errors `LIBGAMMA_GAMMA_NOT_SPECIFIED` - * and `LIBGAMMA_EDID_CHECKSUM_ERROR` have occurred - */ -#define LIBGAMMA_GAMMA_NOT_SPECIFIED_AND_EDID_CHECKSUM_ERROR (-33) - -/** - * Failed to query the gamma ramps size from the - * adjustment method, reason unknown - */ -#define LIBGAMMA_GAMMA_RAMPS_SIZE_QUERY_FAILED (-34) - -/** - * The selected partition could not be opened, - * reason unknown - */ -#define LIBGAMMA_OPEN_PARTITION_FAILED (-35) - -/** - * The selected site could not be opened, - * reason unknown - */ -#define LIBGAMMA_OPEN_SITE_FAILED (-36) - -/** - * Failed to query the adjustment method for - * its protocol version, reason unknown - */ -#define LIBGAMMA_PROTOCOL_VERSION_QUERY_FAILED (-37) - -/** - * The adjustment method's version of its - * protocol is not supported - */ -#define LIBGAMMA_PROTOCOL_VERSION_NOT_SUPPORTED (-38) - -/** - * The adjustment method failed to list - * available partitions, reason unknown - */ -#define LIBGAMMA_LIST_PARTITIONS_FAILED (-39) - -/** - * Partition exists by index, but the partition - * at that index does not exist - */ -#define LIBGAMMA_NULL_PARTITION (-40) - -/** - * There is not monitor connected to the - * connector of the selected CRTC - */ -#define LIBGAMMA_NOT_CONNECTED (-41) - -/** - * Data extraction from a reply from the - * adjustment method failed, reason unknown - */ -#define LIBGAMMA_REPLY_VALUE_EXTRACTION_FAILED (-42) - -/** - * No EDID property was found on the output - */ -#define LIBGAMMA_EDID_NOT_FOUND (-43) - -/** - * Failed to list properties on the output, - * reason unknown - */ -#define LIBGAMMA_LIST_PROPERTIES_FAILED (-44) - -/** - * Failed to query a property's value from - * the output, reason unknown - */ -#define LIBGAMMA_PROPERTY_VALUE_QUERY_FAILED (-45) - -/** - * A request for information on an output - * failed, reason unknown - */ -#define LIBGAMMA_OUTPUT_INFORMATION_QUERY_FAILED (-46) - - - -/** - * The number of the libgamma error with the - * lowest number. If this is lower than the - * number your program thinks it should be sould - * update your program for new errors. - */ -#define LIBGAMMA_ERROR_MIN (-46) - - - -/** - * Prints an error to stderr in a `perror` fashion, - * however this function will not translate the `libgamma` - * errors into human-readable strings, it will simply - * print the name of the error. If the value `error_code` - * is the value of `LIBGAMMA_ERRNO_SET`, `perror` will be - * used to print the current error stored in `errno`. - * If `error_code` is non-negative (an `errno` value`), that - * value will be stored in `errno` and `perror` will be - * used to print it. Additionally, if the `error_code` is - * the value of `LIBGAMMA_DEVICE_REQUIRE_GROUP` the - * required group will be printed with its numerical value - * and, if known, its name. - * - * @param name The text to add at the beginning - * @param error_code The error code, may be an `errno` value - */ -void libgamma_perror(const char *, int); - -/** - * Returns the name of the definition associated with a `libgamma` error code - * - * @param value The error code - * @return The name of the definition associated with the error code, - * `NULL` if the error code does not exist. The return string - * should not be `free`:d. - */ -const char *libgamma_name_of_error(int) __attribute__((const)); - -/** - * Return the value of a `libgamma` error definition refered to by name - * - * @param name The name of the definition associated with the error code - * @return The error code, zero if the name is `NULL` - * or does not refer to a `libgamma` error - */ -int libgamma_value_of_error(const char *) __attribute__((const)); - - - -#ifndef __GNUC__ -# undef __attribute__ -#endif - -#endif diff --git a/src/lib/libgamma-facade.c.gpp b/src/lib/libgamma-facade.c.gpp deleted file mode 100644 index 9f90ed6..0000000 --- a/src/lib/libgamma-facade.c.gpp +++ /dev/null @@ -1,1097 +0,0 @@ -/* -*- c -*- */ -/* See LICENSE file for copyright and license details. */ -#include "libgamma-facade.h" - -#include "libgamma-error.h" -#include "libgamma-method.h" -#include "gamma-helper.h" - - -/* Initialise the general preprocessor */ -$>cd src/extract -$>export PATH=".:${PATH}" - -/* Some general preprocessor we will use frequently */ -$< -get-methods () -{ - ./libgamma-method-extract --list --method | cut -d _ -f 1,2 --complement -} -lowercase () -{ - echo "$*" | sed -e y/QWERTYUIOPASDFGHJKLZXCVBNM/qwertyuiopasdfghjklzxcvbnm/ | sed -e s:core_graphics:cg:g -} -$> - -/* Include all adjustment methods that are enabled at compile-time. */ -$>for method in $(get-methods); do -#ifdef HAVE_LIBGAMMA_METHOD_${method} -# include "gamma-$(lowercase $method | sed -e s:_:-:g).h" -# ifndef HAVE_LIBGAMMA_METHODS -# define HAVE_LIBGAMMA_METHODS -# endif -#endif -$>done - -#include -#include -#include -#include -#include -#include - - -/* Some things to reduce warnings when we do - * not have any adjustment methods enabled */ -#ifndef HAVE_LIBGAMMA_METHODS -# define HAVE_NO_LIBGAMMA_METHODS -# ifdef __GNUC__ -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wsuggest-attribute=const" -# endif -#endif - - - -#ifdef HAVE_LIBGAMMA_METHODS -# ifdef HAVE_LIBGAMMA_METHOD_LINUX_DRM -/** - * Test whether a file descriptor refers to a VT - * - * @param fd The file descriptor - * @return Whether the file descriptor refers to a VT - */ -static int -libgamma_is_vt_proper(int fd) -{ - char buf[32], digit0; - - /* Get TTY */ - if (ttyname_r(fd, buf, sizeof(buf) / sizeof(char))) - return 0; - - /* Validate TTY path */ - if (!strcmp(buf, "/dev/console")) - return 1; - if (strstr(buf, "/dev/tty") != buf) - return 0; - - /* Validate TTY name */ - digit0 = buf[strlen("/dev/tty")]; - return '1' <= digit0 && digit0 <= '9'; -} -# endif - - -/** - * Test the availability of an adjustment method - * - * @param method The adjustment method - * @param operation Allowed values: - * 0: Pass if the environment suggests it will work but is not fake - * 1: Pass if the environment suggests it will work - * 2: Pass if real and not fake - * 3: Pass if real - * 4: Always pass - * Other values invoke undefined behaviour - * @return Whether the test passed - */ -static int -libgamma_list_method_test(int method, int operation) -{ - libgamma_method_capabilities_t caps; - libgamma_method_capabilities(&caps, method); - - switch (operation) { - case 0: - /* Methods that the environment suggests will work, excluding fake */ - if (caps.fake) - return 0; - /* fall through */ - - case 1: - /* Methods that the environment suggests will work, including fake */ - if (!caps.real) - return 0; -#ifdef HAVE_LIBGAMMA_METHOD_LINUX_DRM - if (method == LIBGAMMA_METHOD_LINUX_DRM) { - return libgamma_is_vt_proper(STDIN_FILENO) || - libgamma_is_vt_proper(STDOUT_FILENO) || - libgamma_is_vt_proper(STDERR_FILENO); - } -#endif -#ifdef HAVE_LIBGAMMA_METHOD_DUMMY - if (method == LIBGAMMA_METHOD_DUMMY) - return 0; -#endif - return caps.default_site_known; - - case 2: - /* All real non-fake methods */ - return caps.real && !caps.fake; - - case 3: - /* All real methods */ - return caps.real; - - default: - /* All methods */ - return 1; - } -} -#endif - - -/** - * List available adjustment methods by their order of preference based on the environment - * - * @param methods Output array of methods, should be able to hold `LIBGAMMA_METHOD_COUNT` elements - * @param buf_size The number of elements that fits in `methods`, it should be `LIBGAMMA_METHOD_COUNT`, - * This is used to avoid writing outside the output buffer if this library adds new - * adjustment methods without the users of the library recompiling - * @param operation Allowed values: - * 0: Methods that the environment suggests will work, excluding fake - * 1: Methods that the environment suggests will work, including fake - * 2: All real non-fake methods - * 3: All real methods - * 4: All methods - * Other values invoke undefined behaviour - * @return The number of element that have been stored in `methods`, or should - * have been stored if the buffer was large enough. - */ -size_t -libgamma_list_methods(int *restrict methods, size_t buf_size, int operation) -{ -#ifdef HAVE_NO_LIBGAMMA_METHODS - (void) methods; - (void) buf_size; - (void) operation; - return 0; -#else - size_t n = 0; - -$>for method in $(get-methods); do -#ifdef HAVE_LIBGAMMA_METHOD_${method} - if (libgamma_list_method_test(LIBGAMMA_METHOD_${method}, operation) && n++ < buf_size) - methods[n - 1] = LIBGAMMA_METHOD_${method}; -#endif -$>done - - return n; -#endif -} - - -/** - * Check whether an adjustment method is available, non-existing (invalid) methods will be - * identified as not available under the rationale that the library may be out of date - * - * @param method The adjustment method - * @return Whether the adjustment method is available - */ -int -libgamma_is_method_available(int method) -{ -#ifdef HAVE_NO_LIBGAMMA_METHODS - (void) method; - return 0; -#else - switch (method) { -$>for method in $(get-methods); do -#ifdef HAVE_LIBGAMMA_METHOD_${method} - case LIBGAMMA_METHOD_${method}: -#endif -$>done - return 1; - - default: - return 0; - } -#endif -} - - -/** - * Call the adjustment method's implementation of the called function - * - * @param 1 The adjustment method, you may use `.` instead of `->` when resolving it - * @param 2 `return` if the function returns a value, `break` otherwise - * @param 3 The base name of the function to call, that is, the name of the function - * this is expended into without the libgamma namespace prefix - * @param * The function's parameters - */ -${ - /* Read out macro's parameters */ -$}" - ctrl=$2 - fun=$3 - shift 3 - params="$*" -$>params="${params// /, }" - - switch (${method}) { -$>for adjmethod in $(get-methods); do -#ifdef HAVE_LIBGAMMA_METHOD_${adjmethod} - case LIBGAMMA_METHOD_${adjmethod}: - /* Call the adjustment method's implementation, either - * return or break after it depending on macro parameter's */ -$> if [ $ctrl = return ]; then - return - libgamma_$(lowercase $adjmethod)_${fun}(${params}); -$> elif [ ! $ctrl = return ]; then - break; -$> fi -#endif -$>done - - default: - /* If the adjustment method does not exists, either return - * that error, or do nothing because the function this is - * expanded into does return errors */ -$>if [ $ctrl = return ]; then - return LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD; -$>else - /* Method does not exists/excluded at compile-time. - * We will assume that this is not done... */ - break; -$>fi - } -$>} - - -/** - * Return the capabilities of an adjustment method - * - * @param this The data structure to fill with the method's capabilities - * @param method The adjustment method (display server and protocol) - */ -void -libgamma_method_capabilities(libgamma_method_capabilities_t *restrict this, int method) -{ - memset(this, 0, sizeof(libgamma_method_capabilities_t)); -$>switch method break method_capabilities this -} - - -/** - * Return the default site for an adjustment method - * - * @param method The adjustment method (display server and protocol) - * @return The default site, `NULL` if it cannot be determined or - * if multiple sites are not supported by the adjustment - * method; this value should not be `free`:d - */ -char * -libgamma_method_default_site(int method) -{ - const char *restrict var = libgamma_method_default_site_variable(method); - char *restrict env; - - /* Return `NULL` there is not variable to read */ - if (!var) - return NULL; - - /* Read the variable */ - env = getenv(var); - /* Return `NULL` if it does not exist (or is empty) */ - if (!env || !*env) - return NULL; - - /* Return the variable's value */ - return env; -} - - -/** - * Return the default variable that determines - * the default site for an adjustment method - * - * @param method The adjustment method (display server and protocol) - * @return The environ variables that is used to determine the - * default site. `NULL` if there is none, that is, if - * the method does not support multiple sites. - * This value should not be `free`:d. - */ -const char * -libgamma_method_default_site_variable(int method) -{ - switch (method) { -#ifdef HAVE_LIBGAMMA_METHOD_X_RANDR - case LIBGAMMA_METHOD_X_RANDR: - return "DISPLAY"; -#endif -#ifdef HAVE_LIBGAMMA_METHOD_X_VIDMODE - case LIBGAMMA_METHOD_X_VIDMODE: - return "DISPLAY"; -#endif - default: - return NULL; - } -} - - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param method The adjustment method (display server and protocol) - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocated on the stack. Note however that it will - * not be `free`:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_site_initialise(libgamma_site_state_t *restrict this, int method, char *restrict site) -{ - this->method = method; - this->site = site; -$>switch method return site_initialise this site -} - - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void -libgamma_site_destroy(libgamma_site_state_t *restrict this) -{ -$>switch this.method break site_destroy this - free(this->site); -} - - -/** - * Release all resources held by a site state - * and free the site state pointer - * - * @param this The site state - */ -void -libgamma_site_free(libgamma_site_state_t *restrict this) -{ - libgamma_site_destroy(this); - free(this); -} - - -/** - * Restore the gamma ramps all CRTC:s with a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_site_restore(libgamma_site_state_t *restrict this) -{ -$>switch this.method return site_restore this -} - - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_partition_initialise(libgamma_partition_state_t *restrict this, libgamma_site_state_t *restrict site, size_t partition) -{ - this->site = site; - this->partition = partition; -$>switch site.method return partition_initialise this site partition -} - - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void -libgamma_partition_destroy(libgamma_partition_state_t *restrict this) -{ -$>switch this.site.method break partition_destroy this -} - - -/** - * Release all resources held by a partition state - * and free the partition state pointer - * - * @param this The partition state - */ -void -libgamma_partition_free(libgamma_partition_state_t *restrict this) -{ - libgamma_partition_destroy(this); - free(this); -} - - -/** - * Restore the gamma ramps all CRTC:s with a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_partition_restore(libgamma_partition_state_t *restrict this) -{ -$>switch this.site.method return partition_restore this -} - - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The index of the CRTC within the partition - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_crtc_initialise(libgamma_crtc_state_t *restrict this, libgamma_partition_state_t *restrict partition, size_t crtc) -{ - this->partition = partition; - this->crtc = crtc; -$>switch partition.site.method return crtc_initialise this partition crtc -} - - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void -libgamma_crtc_destroy(libgamma_crtc_state_t *restrict this) -{ -$>switch this.partition.site.method break crtc_destroy this -} - - -/** - * Release all resources held by a CRTC state - * and free the CRTC state pointer - * - * @param this The CRTC state - */ -void -libgamma_crtc_free(libgamma_crtc_state_t *restrict this) -{ - libgamma_crtc_destroy(this); - free(this); -} - - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_crtc_restore(libgamma_crtc_state_t *restrict this) -{ -$>switch this.partition.site.method return crtc_restore this -} - - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error; on error refer to the error reports in `this` - */ -int -libgamma_get_crtc_information(libgamma_crtc_information_t *restrict this, libgamma_crtc_state_t *restrict crtc, int32_t fields) -{ -#ifdef HAVE_NO_LIBGAMMA_METHODS - (void) fields; -#endif - this->edid = NULL; - this->connector_name = NULL; -$>switch crtc.partition.site.method return get_crtc_information this crtc fields -} - - -/** - * Release all resources in an information data structure for a CRTC - * - * @param this The CRTC information - */ -void -libgamma_crtc_information_destroy(libgamma_crtc_information_t *restrict this) -{ - free(this->edid); - free(this->connector_name); -} - - -/** - * Release all resources in an information data structure for a CRTC - * and free the data structure pointer - * - * @param this The CRTC information - */ -void -libgamma_crtc_information_free(libgamma_crtc_information_t *restrict this) -{ - libgamma_crtc_information_destroy(this); - free(this); -} - - -/** - * Convert a raw representation of an EDID to a hexadecimal representation - * - * @param 1 Casing name - * @param 2 The hexadecimal alphabet - * @param edid The EDID in raw representation - * @param length The length of `edid` - * @return The EDID in hexadecimal representation - * `NULL` on allocation error, `errno` will be set accordingly - */ -$>behex_edid () -$>{ -char * -libgamma_behex_edid_${1}(const unsigned char *restrict edid, size_t length) -{ - char *restrict out; - size_t i; - - /* Allocate memory area for the output string */ - out = malloc((length * 2 + 1) * sizeof(char)); - if (!out) - return NULL; - - /* Translate from raw octets to hexadecimal */ - for (i = 0; i < length; i++) { - out[i * 2 + 0] = "${2}"[(edid[i] >> 4) & 15]; - out[i * 2 + 1] = "${2}"[(edid[i] >> 0) & 15]; - } - /* NUL-terminate the output string */ - out[length * 2] = '\0'; - - return out; -} -$>} - - -/** - * Convert a raw representation of an EDID to a lowercase hexadecimal representation - * - * @param edid The EDID in raw representation - * @param length The length of `edid` - * @return The EDID in lowercase hexadecimal representation, - * `NULL` on allocation error, `errno` will be set accordingly - */ -$>behex_edid lowercase 0123456789abcdef - - -/** - * Convert a raw representation of an EDID to an uppercase hexadecimal representation - * - * @param edid The EDID in raw representation - * @param length The length of `edid` - * @return The EDID in uppercase hexadecimal representation, - * NULL` on allocation error, `errno` will be set accordingly - */ -$>behex_edid uppercase 0123456789ABCDEF - - -/** - * Convert an hexadecimal representation of an EDID to a raw representation - * - * @param edid The EDID in hexadecimal representation - * @return The EDID in raw representation, it will be half the length - * of `edid` (the input value); `NULL` on allocation error or - * if the EDID is malformated, `errno` will be set accordingly - */ -unsigned char * -libgamma_unhex_edid(const char *restrict edid) -{ -#define not_range(lower, V, upper) (V < lower || upper < V) -#define is_not_hex(V) (not_range('0', V, '9') && not_range('a', V, 'f') && not_range('A', V, 'F')) - - unsigned char *restrict out; - size_t i, n = strlen(edid); - char a, b; - - /* Check that the length of the strings is even, - * otherwise it cannot represent bytes */ - if (n & 1) { - errno = EINVAL; - return NULL; - } - - /* Allocate memory area for output octet array */ - n /= 2 * sizeof(unsigned char); - out = malloc(n); - if (!out) - return NULL; - - /* Convert to raw octet array */ - for (i = 0; i < n; i++) { - /* Get the next character pair that, it represents an octet. */ - a = edid[i * 2 + 0]; - b = edid[i * 2 + 1]; - - /* Verify that the input is in hexadecimal */ - if (is_not_hex(a) || is_not_hex(b)) { - free(out); - errno = EINVAL; - return NULL; - } - - /* Convert each chararacter to raw format */ - a = (char)((a & 15) + (a > '9' ? 9 : 0)); - b = (char)((b & 15) + (b > '9' ? 9 : 0)); - - /* Combine the two characters into one octet */ - out[i] = (unsigned char)((a << 4) | b); - } - - return out; - -#undef is_hex -#undef not_range -} - - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t *restrict ramps) -{ - libgamma_gamma_ramps_any_t ramps_; - -#ifdef HAVE_NO_LIBGAMMA_METHODS - (void) ramps; -#endif - - switch (this->partition->site->method) { - /* Methods other than Quartz/CoreGraphics uses 16-bit integers */ -$>for method in $(get-methods | grep -v QUARTZ_CORE_GRAPHICS); do -#ifdef HAVE_LIBGAMMA_METHOD_${method} - case LIBGAMMA_METHOD_${method}: - return libgamma_$(lowercase $method)_crtc_get_gamma_ramps16(this, ramps); -#endif - - /* The Quartz/CoreGraphics method uses single precision float */ -$>done -#ifdef HAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS - case LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS: - ramps_.bits16 = *ramps; - return libgamma_translated_ramp_get(this, &ramps_, 16, -1, libgamma_crtc_get_gamma_rampsf); -#endif - - /* The selected method does not exist */ - default: - return LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD; - } -} - - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int -libgamma_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict this, libgamma_gamma_ramps16_t ramps) -{ - libgamma_gamma_ramps_any_t ramps_; - -#ifdef HAVE_NO_LIBGAMMA_METHODS - (void) ramps; -#endif - - switch (this->partition->site->method) { - /* Methods other than Quartz/CoreGraphics uses 16-bit integers */ -$>for method in $(get-methods | grep -v QUARTZ_CORE_GRAPHICS); do -#ifdef HAVE_LIBGAMMA_METHOD_${method} - case LIBGAMMA_METHOD_${method}: - return libgamma_$(lowercase $method)_crtc_set_gamma_ramps16(this, ramps); -#endif -$>done - - /* The Quartz/CoreGraphics method uses single precision float */ -#ifdef HAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS - case LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS: - ramps_.bits16 = ramps; - return libgamma_translated_ramp_set(this, ramps_, 16, -1, libgamma_crtc_set_gamma_rampsf); -#endif - - /* The selected method does not exist. */ - default: - return LIBGAMMA_NO_SUCH_ADJUSTMENT_METHOD; - } -} - - - -/** - * Set or get the gamma ramps for a CRTC, non-16-bit gamma-depth version - * - * @param 1 Either `get` or `set`, for the action that the name of value implies - * @param 2 The `ramp*` pattern for the ramp structure and function to call - * @param 3 Either of `bit8`, `bit16`, `bit32`, `bit64`, `float_single`, `float_double`; - * rather self-explanatory - * @param 4 The number of bits in the gamma depth, -1 for single precision float, - * (`float`) and -2 for double percition float (`double`) - * @param this The CRTC state - * @param ramps The gamma ramps to apply, or - * the gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps () -$>{ -$ -int -libgamma_crtc_${action}_gamma_${ramps}(libgamma_crtc_state_t *restrict this, libgamma_gamma_${ramps}_t${p:+* restrict} ramps) -{ - libgamma_gamma_ramps_any_t ramps_; - switch (this->partition->site->method) { - /* The dummy method supports all ramp depths */ -#ifdef HAVE_LIBGAMMA_METHOD_DUMMY - case LIBGAMMA_METHOD_DUMMY: - return libgamma_dummy_crtc_${action}_gamma_${ramps}(this, ramps); -#endif - - /* The Quartz/CoreGraphics method uses single precision float */ -#ifdef HAVE_LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS - case LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS: -$>if [ $bits = -1 ]; then - /* Single precision float is used */ - return libgamma_quartz_cg_crtc_${action}_gamma_${ramps}(this, ramps); -$>else - /* Something else is used and we convert to Single precision float */ - ramps_.${type} = ${p}ramps; - return libgamma_translated_ramp_${action}(this, ${p:+&}ramps_, ${bits}, -1, libgamma_crtc_${action}_gamma_rampsf); -$>fi -#endif - - /* Other methods use 16-bit integers. */ - default: - ramps_.${type} = ${p}ramps; - return libgamma_translated_ramp_${action}(this, ${p:+&}ramps_, ${bits}, 16, libgamma_crtc_${action}_gamma_ramps16); - } -} -$>} - - - -/** - * Get the current gamma ramps for a CRTC, 8-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps get ramps8 bits8 8 - - -/** - * Set the gamma ramps for a CRTC, 32-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps set ramps8 bits8 8 - - - -/** - * Get the current gamma ramps for a CRTC, 32-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps get ramps32 bits32 32 - - -/** - * Set the gamma ramps for a CRTC, 32-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps set ramps32 bits32 32 - - - -/** - * Get the current gamma ramps for a CRTC, 64-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps get ramps64 bits64 64 - - -/** - * Set the gamma ramps for a CRTC, 64-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps set ramps64 bits64 64 - - -/** - * Get the current gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps get rampsf float_single -1 - - -/** - * Set the gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps set rampsf float_single -1 - - - -/** - * Get the current gamma ramps for a CRTC, `double` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps get rampsd float_double -2 - - -/** - * Set the gamma ramps for a CRTC, `double` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_get_gamma_ramps set rampsd float_double -2 - - - -/** - * Set the gamma ramps for a CRTC - * - * Note that this will probably involve the library allocating temporary data - * - * @param 1 The data type for the ramp stop elements - * @param 2 The `ramp*` pattern for the ramp structure and function to call - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_gamma_ramps_f () -$>{ -int -libgamma_crtc_set_gamma_${2}_f(libgamma_crtc_state_t *restrict this, libgamma_gamma_${2}_fun *red_function, - libgamma_gamma_${2}_fun *green_function, libgamma_gamma_${2}_fun *blue_function) -{ - libgamma_crtc_information_t info; - libgamma_gamma_${2}_t ramps; - size_t i, n; - int e; - - /* Get the size of the gamma ramps */ - if (libgamma_get_crtc_information(&info, this, LIBGAMMA_CRTC_INFO_GAMMA_SIZE)) { - e = info.gamma_size_error; - if (e < 0) - return e; - errno = e; - return LIBGAMMA_ERRNO_SET; - } - - /* Copy the size of the gamma ramps and calculte the grand size */ - n = ramps. red_size = info. red_gamma_size; - n += ramps.green_size = info.green_gamma_size; - n += ramps. blue_size = info. blue_gamma_size; - - /* Allocate gamma ramps */ - ramps. red = malloc(n * sizeof(${1})); - ramps.green = &ramps. red[ramps. red_size]; - ramps. blue = &ramps.green[ramps.green_size]; - if (!ramps.red) - return LIBGAMMA_ERRNO_SET; - - /* Generate the gamma ramp for the red chennel */ - for (i = 0, n = ramps.red_size; i < n; i++) - ramps.red[i] = red_function((float)i / (float)(n - 1)); - - /* Generate the gamma ramp for the green chennel */ - for (i = 0, n = ramps.green_size; i < n; i++) - ramps.green[i] = green_function((float)i / (float)(n - 1)); - - /* Generate the gamma ramp for the blue chennel */ - for (i = 0, n = ramps.blue_size; i < n; i++) - ramps.blue[i] = blue_function((float)i / (float)(n - 1)); - - /* Apply the gamma ramps */ - e = libgamma_crtc_set_gamma_${2}(this, ramps); - free(ramps.red); - return e; -} -$>} - - -/** - * Set the gamma ramps for a CRTC, 8-bit gamma-depth function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_gamma_ramps_f uint8_t ramps8 - - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_gamma_ramps_f uint16_t ramps16 - - -/** - * Set the gamma ramps for a CRTC, 32-bit gamma-depth function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_gamma_ramps_f uint32_t ramps32 - - -/** - * Set the gamma ramps for a CRTC, 64-bit gamma-depth function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_gamma_ramps_f uint64_t ramps64 - - -/** - * Set the gamma ramps for a CRTC, `float` function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_gamma_ramps_f float rampsf - - -/** - * Set the gamma ramps for a CRTC, `double` function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -$>crtc_set_gamma_ramps_f double rampsd - - - -#ifdef HAVE_NO_LIBGAMMA_METHODS -# ifdef __GNUC__ -# pragma GCC diagnostic pop -# endif -#endif diff --git a/src/lib/libgamma-facade.h b/src/lib/libgamma-facade.h deleted file mode 100644 index 596094c..0000000 --- a/src/lib/libgamma-facade.h +++ /dev/null @@ -1,537 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_FACADE_H -#define LIBGAMMA_FACADE_H - -#if !defined(LIBGAMMA_CONFIG_H) && !defined(DEBUG) -# error libgamma-facade.h should not be included directly, include libgamma.h instead -#endif - - -#include "libgamma-method.h" - -#include -#include - -#ifndef __GNUC__ -# define __attribute__(x) -#endif - - - -/** - * Mapping function from [0, 1] float encoding value to [0, 2⁸ − 1] integer output value - * - * @param encoding [0, 1] float encoding value - * @return [0, 2⁸ − 1] integer output value - */ -typedef uint8_t libgamma_gamma_ramps8_fun(float); - -/** - * Mapping function from [0, 1] float encoding value to [0, 2¹⁶ − 1] integer output value - * - * @param encoding [0, 1] float encoding value - * @return [0, 2¹⁶ − 1] integer output value - */ -typedef uint16_t libgamma_gamma_ramps16_fun(float); - -/** - * Mapping function from [0, 1] float encoding value to [0, 2³² − 1] integer output value - * - * @param encoding [0, 1] float encoding value - * @return [0, 2³² − 1] integer output value - */ -typedef uint32_t libgamma_gamma_ramps32_fun(float); - -/** - * Mapping function from [0, 1] float encoding value to [0, 2⁶⁴ − 1] integer output value - * - * @param encoding [0, 1] float encoding value - * @return [0, 2⁶⁴ − 1] integer output value - */ -typedef uint64_t libgamma_gamma_ramps64_fun(float); - -/** - * Mapping function from [0, 1] float encoding value to [0, 1] float output value - * - * @param encoding [0, 1] float encoding value - * @return [0, 1] float output value - */ -typedef float libgamma_gamma_rampsf_fun(float); - -/** - * Mapping function from [0, 1] double precision float encoding - * value to [0, 1] double precision float output value - * - * @param encoding [0, 1] float encoding value - * @return [0, 1] float output value - */ -typedef double libgamma_gamma_rampsd_fun(double); - - - -/** - * List available adjustment methods by their order of preference based on the environment - * - * @param methods Output array of methods, should be able to hold `LIBGAMMA_METHOD_COUNT` elements - * @param buf_size The number of elements that fits in `methods`, it should be `LIBGAMMA_METHOD_COUNT`, - * This is used to avoid writing outside the output buffer if this library adds new - * adjustment methods without the users of the library recompiling - * @param operation Allowed values: - * 0: Methods that the environment suggests will work, excluding fake - * 1: Methods that the environment suggests will work, including fake - * 2: All real non-fake methods - * 3: All real methods - * 4: All methods - * Other values invoke undefined behaviour - * @return The number of element that have been stored in `methods`, or should - * have been stored if the buffer was large enough - */ -size_t libgamma_list_methods(int *restrict, size_t, int); - -/** - * Check whether an adjustment method is available, non-existing (invalid) methods will be - * identified as not available under the rationale that the library may be out of date - * - * @param method The adjustment method - * @return Whether the adjustment method is available - */ -int libgamma_is_method_available(int) __attribute__((const)); - -/** - * Return the capabilities of an adjustment method - * - * @param this The data structure to fill with the method's capabilities - * @param method The adjustment method (display server and protocol) - */ -void libgamma_method_capabilities(libgamma_method_capabilities_t *restrict, int); - -/** - * Return the default site for an adjustment method - * - * @param method The adjustment method (display server and protocol) - * @return The default site, `NULL` if it cannot be determined or - * if multiple sites are not supported by the adjustment - * method; this value should not be `free`:d - */ -char *libgamma_method_default_site(int); - -/** - * Return the default variable that determines - * the default site for an adjustment method - * - * @param method The adjustment method (display server and protocol) - * @return The environ variables that is used to determine the - * default site, `NULL` if there is none, that is, if - * the method does not support multiple sites; this - * value should not be `free`:d - */ -const char *libgamma_method_default_site_variable(int) __attribute__((const)); - - -/** - * Initialise an allocated site state - * - * @param this The site state to initialise - * @param method The adjustment method (display server and protocol) - * @param site The site identifier, unless it is `NULL` it must a - * `free`:able. Once the state is destroyed the library - * will attempt to free it. There you should not free - * it yourself, and it must not be a string constant - * or allocate on the stack. Note however that it will - * not be `free`:d if this function fails. - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_site_initialise(libgamma_site_state_t *restrict, int, char *restrict); - -/** - * Release all resources held by a site state - * - * @param this The site state - */ -void libgamma_site_destroy(libgamma_site_state_t *restrict); - -/** - * Release all resources held by a site state - * and free the site state pointer - * - * @param this The site state - */ -void libgamma_site_free(libgamma_site_state_t *restrict); - -/** - * Restore the gamma ramps all CRTC:s within a site to the system settings - * - * @param this The site state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_site_restore(libgamma_site_state_t *restrict); - - -/** - * Initialise an allocated partition state - * - * @param this The partition state to initialise - * @param site The site state for the site that the partition belongs to - * @param partition The index of the partition within the site - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_partition_initialise(libgamma_partition_state_t *restrict, libgamma_site_state_t *restrict, size_t); - -/** - * Release all resources held by a partition state - * - * @param this The partition state - */ -void libgamma_partition_destroy(libgamma_partition_state_t *restrict); - -/** - * Release all resources held by a partition state - * and free the partition state pointer - * - * @param this The partition state - */ -void libgamma_partition_free(libgamma_partition_state_t *restrict); - -/** - * Restore the gamma ramps all CRTC:s within a partition to the system settings - * - * @param this The partition state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_partition_restore(libgamma_partition_state_t *restrict); - - -/** - * Initialise an allocated CRTC state - * - * @param this The CRTC state to initialise - * @param partition The partition state for the partition that the CRTC belongs to - * @param crtc The index of the CRTC within the partition - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_initialise(libgamma_crtc_state_t *restrict, libgamma_partition_state_t *restrict, size_t); - -/** - * Release all resources held by a CRTC state - * - * @param this The CRTC state - */ -void libgamma_crtc_destroy(libgamma_crtc_state_t *restrict); - -/** - * Release all resources held by a CRTC state - * and free the CRTC state pointer - * - * @param this The CRTC state - */ -void libgamma_crtc_free(libgamma_crtc_state_t *restrict); - -/** - * Restore the gamma ramps for a CRTC to the system settings for that CRTC - * - * @param this The CRTC state - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_restore(libgamma_crtc_state_t *restrict); - - -/** - * Read information about a CRTC - * - * @param this Instance of a data structure to fill with the information about the CRTC - * @param crtc The state of the CRTC whose information should be read - * @param fields OR:ed identifiers for the information about the CRTC that should be read - * @return Zero on success, -1 on error; on error refer to the error reports in `this` - */ -int libgamma_get_crtc_information(libgamma_crtc_information_t *restrict, libgamma_crtc_state_t *restrict, int32_t); - -/** - * Release all resources in an information data structure for a CRTC - * - * @param this The CRTC information - */ -void libgamma_crtc_information_destroy(libgamma_crtc_information_t *restrict); - -/** - * Release all resources in an information data structure for a CRTC - * and free the data structure pointer - * - * @param this The CRTC information - */ -void libgamma_crtc_information_free(libgamma_crtc_information_t *restrict); - -/** - * Convert a raw representation of an EDID to a lowercase hexadecimal representation - * - * @param edid:const unsigned char* The EDID in raw representation - * @param length:size_t The length of `edid` - * @return :char* The EDID in lowercase hexadecimal representation, - * `NULL` on allocation error, `errno` will be set accordingly - */ -#define libgamma_behex_edid(edid, length) libgamma_behex_edid_lowercase((edid), (length)) - -/** - * Convert a raw representation of an EDID to a lowercase hexadecimal representation - * - * @param edid The EDID in raw representation - * @param length The length of `edid` - * @return The EDID in lowercase hexadecimal representation - * `NULL` on allocation error, `errno` will be set accordingly - */ -char *libgamma_behex_edid_lowercase(const unsigned char *restrict, size_t); - -/** - * Convert a raw representation of an EDID to an uppercase hexadecimal representation - * - * @param edid The EDID in raw representation - * @param length The length of `edid` - * @return The EDID in uppercase hexadecimal representation, - * NULL` on allocation error, `errno` will be set accordingly - */ -char *libgamma_behex_edid_uppercase(const unsigned char *restrict, size_t); - -/** - * Convert an hexadecimal representation of an EDID to a raw representation - * - * @param edid The EDID in hexadecimal representation - * @return The EDID in raw representation, it will be half the length - * of `edid` (the input value); `NULL` on allocation error or - * if the EDID is malformated, `errno` will be set accordingly - */ -unsigned char *libgamma_unhex_edid(const char *restrict); - - -/** - * Get the current gamma ramps for a CRTC, 8-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_get_gamma_ramps8(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps8_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 8-bit gamma-depth version. - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_set_gamma_ramps8(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps8_t); - - -/** - * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_get_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_set_gamma_ramps16(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_t) __attribute__((hot)); - - -/** - * Get the current gamma ramps for a CRTC, 32-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_get_gamma_ramps32(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps32_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 32-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_set_gamma_ramps32(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps32_t); - - -/** - * Get the current gamma ramps for a CRTC, 64-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_get_gamma_ramps64(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps64_t *restrict); - -/** - * Set the gamma ramps for a CRTC, 64-bit gamma-depth version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_set_gamma_ramps64(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps64_t); - - -/** - * Set the gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_set_gamma_rampsf(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsf_t); - -/** - * Get the current gamma ramps for a CRTC, `float` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_get_gamma_rampsf(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsf_t *restrict); - - -/** - * Get the current gamma ramps for a CRTC, `double` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to fill with the current values - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_get_gamma_rampsd(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsd_t *restrict); - -/** - * Set the gamma ramps for a CRTC, `double` version - * - * @param this The CRTC state - * @param ramps The gamma ramps to apply - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -int libgamma_crtc_set_gamma_rampsd(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsd_t); - - -/** - * Set the gamma ramps for a CRTC, 8-bit gamma-depth function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -__attribute__((cold)) -int libgamma_crtc_set_gamma_ramps8_f(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps8_fun *, - libgamma_gamma_ramps8_fun *, libgamma_gamma_ramps8_fun *); - -/** - * Set the gamma ramps for a CRTC, 16-bit gamma-depth function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -__attribute__((cold)) -int libgamma_crtc_set_gamma_ramps16_f(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps16_fun *, - libgamma_gamma_ramps16_fun *, libgamma_gamma_ramps16_fun *); - -/** - * Set the gamma ramps for a CRTC, 32-bit gamma-depth function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -__attribute__((cold)) -int libgamma_crtc_set_gamma_ramps32_f(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps32_fun *, - libgamma_gamma_ramps32_fun *, libgamma_gamma_ramps32_fun *); - -/** - * Set the gamma ramps for a CRTC, 64-bit gamma-depth function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -__attribute__((cold)); -int libgamma_crtc_set_gamma_ramps64_f(libgamma_crtc_state_t *restrict, libgamma_gamma_ramps64_fun *, - libgamma_gamma_ramps64_fun *, libgamma_gamma_ramps64_fun *); - -/** - * Set the gamma ramps for a CRTC, `float` function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -__attribute__((cold)) -int libgamma_crtc_set_gamma_rampsf_f(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsf_fun *, - libgamma_gamma_rampsf_fun *, libgamma_gamma_rampsf_fun *); - -/** - * Set the gamma ramps for a CRTC, `double` function version - * - * Note that this will probably involve the library allocating temporary data - * - * @param this The CRTC state - * @param red_function The function that generates the gamma ramp for the red channel - * @param green_function The function that generates the gamma ramp for the green channel - * @param blue_function The function that generates the gamma ramp for the blue channel - * @return Zero on success, otherwise (negative) the value of an - * error identifier provided by this library - */ -__attribute__((cold)) -int libgamma_crtc_set_gamma_rampsd_f(libgamma_crtc_state_t *restrict, libgamma_gamma_rampsd_fun *, - libgamma_gamma_rampsd_fun *, libgamma_gamma_rampsd_fun *); - - -#ifndef __GNUC__ -# undef __attribute__ -#endif - -#endif diff --git a/src/lib/libgamma-method.c b/src/lib/libgamma-method.c deleted file mode 100644 index cf9a23b..0000000 --- a/src/lib/libgamma-method.c +++ /dev/null @@ -1,324 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include "libgamma-method.h" - - -#include -#include -#include - - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int -libgamma_gamma_ramps8_initialise(libgamma_gamma_ramps8_t *restrict this) -{ - size_t n = this->red_size + this->green_size + this->blue_size; - this->red = malloc(n * sizeof(uint8_t)); - this->green = &this-> red[this-> red_size]; - this->blue = &this->green[this->green_size]; - return this->red ? 0 : -1; -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps8_initialise` or otherwise - * initialises in the proper manner - * - * @param this The gamma ramps - */ -void -libgamma_gamma_ramps8_destroy(libgamma_gamma_ramps8_t *restrict this) -{ - free(this->red); -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps8_initialise` or otherwise - * initialises in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void -libgamma_gamma_ramps8_free(libgamma_gamma_ramps8_t *restrict this) -{ - free(this->red); - free(this); -} - - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int -libgamma_gamma_ramps16_initialise(libgamma_gamma_ramps16_t *restrict this) -{ - size_t n = this->red_size + this->green_size + this->blue_size; -#ifdef HAVE_LIBGAMMA_METHOD_LINUX_DRM - /* Valgrind complains about us reading uninitialize memory if we just use malloc */ - this->red = calloc(n, sizeof(uint16_t)); -#else - this->red = malloc(n * sizeof(uint16_t)); -#endif - this->green = &this-> red[this-> red_size]; - this->blue = &this->green[this->green_size]; - return this->red ? 0 : -1; -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps_initialise` or otherwise - * initialises in the proper manner - * - * @param this The gamma ramps - */ -void -libgamma_gamma_ramps16_destroy(libgamma_gamma_ramps16_t *restrict this) -{ - free(this->red); -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps_initialise` or otherwise - * initialises in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void -libgamma_gamma_ramps16_free(libgamma_gamma_ramps16_t *restrict this) -{ - free(this->red); - free(this); -} - - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int -libgamma_gamma_ramps32_initialise(libgamma_gamma_ramps32_t *restrict this) -{ - size_t n = this->red_size + this->green_size + this->blue_size; - this->red = malloc(n * sizeof(uint32_t)); - this->green = &this-> red[this-> red_size]; - this->blue = &this->green[this->green_size]; - return this->red ? 0 : -1; -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps32_initialise` or otherwise - * initialises in the proper manner - * - * @param this The gamma ramps - */ -void -libgamma_gamma_ramps32_destroy(libgamma_gamma_ramps32_t *restrict this) -{ - free(this->red); -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps32_initialise` or otherwise - * initialises in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void -libgamma_gamma_ramps32_free(libgamma_gamma_ramps32_t *restrict this) -{ - free(this->red); - free(this); -} - - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int -libgamma_gamma_ramps64_initialise(libgamma_gamma_ramps64_t *restrict this) -{ - size_t n = this->red_size + this->green_size + this->blue_size; - this->red = malloc(n * sizeof(uint64_t)); - this->green = &this-> red[this-> red_size]; - this->blue = &this->green[this->green_size]; - return this->red ? 0 : -1; -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps64_initialise` or otherwise - * initialises in the proper manner - * - * @param this The gamma ramps - */ -void -libgamma_gamma_ramps64_destroy(libgamma_gamma_ramps64_t *restrict this) -{ - free(this->red); -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps64_initialise` or otherwise - * initialises in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void -libgamma_gamma_ramps64_free(libgamma_gamma_ramps64_t *restrict this) -{ - free(this->red); - free(this); -} - - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int -libgamma_gamma_rampsf_initialise(libgamma_gamma_rampsf_t *restrict this) -{ - size_t n = this->red_size + this->green_size + this->blue_size; - this->red = malloc(n * sizeof(float)); - this->green = &this-> red[this-> red_size]; - this->blue = &this->green[this->green_size]; - return this->red ? 0 : -1; -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_rampsf_initialise` or otherwise - * initialises in the proper manner - * - * @param this The gamma ramps - */ -void -libgamma_gamma_rampsf_destroy(libgamma_gamma_rampsf_t *restrict this) -{ - free(this->red); -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_rampsf_initialise` or otherwise - * initialises in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void -libgamma_gamma_rampsf_free(libgamma_gamma_rampsf_t *restrict this) -{ - free(this->red); - free(this); -} - - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int -libgamma_gamma_rampsd_initialise(libgamma_gamma_rampsd_t *restrict this) -{ - size_t n = this->red_size + this->green_size + this->blue_size; - this->red = malloc(n * sizeof(double)); - this->green = &this-> red[this-> red_size]; - this->blue = &this->green[this->green_size]; - return this->red ? 0 : -1; -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_rampsd_initialise` or otherwise - * initialises in the proper manner - * - * @param this The gamma ramps - */ -void -libgamma_gamma_rampsd_destroy(libgamma_gamma_rampsd_t *restrict this) -{ - free(this->red); -} - - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_rampsd_initialise` or otherwise - * initialises in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void -libgamma_gamma_rampsd_free(libgamma_gamma_rampsd_t *restrict this) -{ - free(this->red); - free(this); -} diff --git a/src/lib/libgamma-method.h b/src/lib/libgamma-method.h deleted file mode 100644 index e2a35c6..0000000 --- a/src/lib/libgamma-method.h +++ /dev/null @@ -1,1323 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_METHOD_H -#define LIBGAMMA_METHOD_H - -#if !defined(LIBGAMMA_CONFIG_H) && !defined(DEBUG) -# error libgamma-method.h should not be included directly, include libgamma.h instead -#endif - - -#ifndef __GNUC__ -# define __attribute__(x) -#endif - -#include -#include - - - -/** - * The identifier for the dummy adjustment method - * - * This method can be configured and is useful for - * testing your program's ability to handle errors - */ -#define LIBGAMMA_METHOD_DUMMY 0 - -/** - * The identifier for the adjustment method with - * uses the RandR protocol under the X display server - */ -#define LIBGAMMA_METHOD_X_RANDR 1 - -/** - * The identifier for the adjustment method with - * uses the VidMode protocol under the X display server - * - * This is an older alternative to RandR that can - * work on some drivers that are not supported by RandR, - * however it can only control the primary CRTC per - * screen (partition) - */ -#define LIBGAMMA_METHOD_X_VIDMODE 2 - -/** - * The identifier for the Direct Rendering Manager - * adjustment method that is available in Linux - * (built in to the Linux kernel with a userland - * library for access) and is a part of the - * Direct Rendering Infrastructure. - * - * This adjustment method will work when you are - * in non-graphical mode; however a display server - * cannot be started while this is running, but it - * can be started while a display server is running - */ -#define LIBGAMMA_METHOD_LINUX_DRM 3 - -/** - * The identifier for the Graphics Device Interface - * adjustment method that is available in Windows - * - * This method is not well tested; it can be compiled - * to be available under X.org using a translation layer - */ -#define LIBGAMMA_METHOD_W32_GDI 4 - -/** - * The identifier for the CoreGraphics adjustment - * method that is available in Mac OS X that can - * adjust gamma ramps under the Quartz display server - * - * This method is not well tested; it can be compiled - * to be available under X.org using a translation layer - */ -#define LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS 5 - - -/** - * The index of the last gamma method, neither it - * nor any index before it may actually be supported - * as it could have been disabled at compile-time - */ -#define LIBGAMMA_METHOD_MAX 5 - -/** - * The number adjustment methods provided by this library. - * Note however that this includes adjstment methods that - * have been removed at compile-time - */ -#define LIBGAMMA_METHOD_COUNT (LIBGAMMA_METHOD_MAX + 1) - - - -/** - * Capabilities of adjustment methods - */ -typedef struct libgamma_method_capabilities { - /** - * OR of the CRTC information fields in `libgamma_crtc_information_t` - * that may (but can fail) be read successfully - */ - int32_t crtc_information; - - /** - * Whether the default site is known, if true the site is integrated - * to the system or can be determined using environment variables - */ - unsigned default_site_known : 1; - - /** - * Whether the adjustment method supports multiple sites rather - * than just the default site - */ - unsigned multiple_sites : 1; - - /** - * Whether the adjustment method supports multiple partitions - * per site - */ - unsigned multiple_partitions : 1; - - /** - * Whether the adjustment method supports multiple CRTC:s - * per partition per site - */ - unsigned multiple_crtcs : 1; - - /** - * Whether the partition to graphics card is a bijection - */ - unsigned partitions_are_graphics_cards : 1; - - /** - * Whether the adjustment method supports `libgamma_site_restore` - */ - unsigned site_restore : 1; - - /** - * Whether the adjustment method supports `libgamma_partition_restore` - */ - unsigned partition_restore : 1; - - /** - * Whether the adjustment method supports `libgamma_crtc_restore` - */ - unsigned crtc_restore : 1; - - /** - * Whether the `red_gamma_size`, `green_gamma_size` and `blue_gamma_size` - * fields in `libgamma_crtc_information_t` will always have the same - * values as each other for the adjustment method - */ - unsigned identical_gamma_sizes : 1; - - /** - * Whether the `red_gamma_size`, `green_gamma_size` and `blue_gamma_size` - * fields in `libgamma_crtc_information_t` will always be filled with the - * same value for the adjustment method - */ - unsigned fixed_gamma_size : 1; - - /** - * Whether the `gamma_depth` field in `libgamma_crtc_information_t` - * will always be filled with the same value for the adjustment method - */ - unsigned fixed_gamma_depth : 1; - - /** - * Whether the adjustment method will actually perform adjustments - */ - unsigned real : 1; - - /** - * Whether the adjustment method is implement using a translation layer - */ - unsigned fake : 1; - - /** - * Whether adjustments are undone when the process disconnects from - * the display server - */ - unsigned auto_restore : 1; - -} libgamma_method_capabilities_t; - - - -/** - * Site state - * - * On operating systems that integrate a graphical environment - * there is usually just one site. However, one systems with - * pluggable graphics, like Unix-like systems such as GNU/Linux - * and the BSD:s, there can usually be any (feasible) number of - * sites. In X.org parlance they are called displays. - */ -typedef struct libgamma_site_state { - /** - * Adjustment method implementation specific data. - * You as a user of this library should not touch this. - */ - void *data; - - /** - * This field specifies, for the methods if this library, - * which adjustment method (display server and protocol) - * is used to adjust the gamma ramps - */ - int method; - - /** - * The site identifier. It can either be `NULL` or a string. - * `NULL` indicates the default site. On systems like the - * Unix-like systems, where the graphics are pluggable, this - * is usually resolved by an environment variable, such as - * "DISPLAY" for X.org. - */ - char *site; - - /** - * The number of partitions that is available on this site. - * Probably the majority of display server only one partition - * per site. However, X.org can, and traditional used to have - * on multi-headed environments, multiple partitions per site. - * In X.org partitions are called 'screens'. It is not to be - * confused with monitor. A screen is a collection of monitors, - * and the mapping from monitors to screens is a surjection. - * On hardware-level adjustment methods, such as Direct - * Rendering Manager, a partition is a graphics card. - */ - size_t partitions_available; - -} libgamma_site_state_t; - - -/** - * Partition state - * - * Probably the majority of display server only one partition - * per site. However, X.org can, and traditional used to have - * on multi-headed environments, multiple partitions per site. - * In X.org partitions are called 'screens'. It is not to be - * confused with monitor. A screen is a collection of monitors, - * and the mapping from monitors to screens is a surjection. - * On hardware-level adjustment methods, such as Direct - * Rendering Manager, a partition is a graphics card. - */ -typedef struct libgamma_partition_state { - /** - * Adjustment method implementation specific data - * - * You as a user of this library should not touch this - */ - void *data; - - /** - * The site this partition belongs to - */ - libgamma_site_state_t *site; - - /** - * The index of the partition - */ - size_t partition; - - /** - * The number of CRTC:s that are available - * under this partition - * - * Note that the CRTC:s are not necessarily - * online. - */ - size_t crtcs_available; - -} libgamma_partition_state_t; - - -/** - * Cathode ray tube controller state - * - * The CRTC controls the gamma ramps for the - * monitor that is plugged in to the connector - * that the CRTC belongs to - */ -typedef struct libgamma_crtc_state { - /** - * Adjustment method implementation specific data - * - * You as a user of this library should not touch this - */ - void *data; - - /** - * The partition this CRTC belongs to - */ - libgamma_partition_state_t *partition; - - /** - * The index of the CRTC within its partition - */ - size_t crtc; - -} libgamma_crtc_state_t; - - -/** - * Types for connectors - */ -typedef enum libgamma_connector_type { - /** - * The adjustment method does not know the connector's type - * - * (This could be considered an error) - */ - LIBGAMMA_CONNECTOR_TYPE_Unknown = 0, - - /** - * Video Graphics Array (VGA) - */ - LIBGAMMA_CONNECTOR_TYPE_VGA, - - /** - * Digital Visual Interface, unknown type - */ - LIBGAMMA_CONNECTOR_TYPE_DVI, - - /** - * Digital Visual Interface, integrated (DVI-I) - */ - LIBGAMMA_CONNECTOR_TYPE_DVII, - - /** - * Digital Visual Interface, digital only (DVI-D) - */ - LIBGAMMA_CONNECTOR_TYPE_DVID, - - /** - * Digital Visual Interface, analogue only (DVI-A) - */ - LIBGAMMA_CONNECTOR_TYPE_DVIA, - - /** - * Composite video - */ - LIBGAMMA_CONNECTOR_TYPE_Composite, - - /** - * Separate Video (S-video) - */ - LIBGAMMA_CONNECTOR_TYPE_SVIDEO, - - /** - * Low-voltage differential signaling (LVDS) - */ - LIBGAMMA_CONNECTOR_TYPE_LVDS, - - /** - * Component video, usually separate cables for each channel - */ - LIBGAMMA_CONNECTOR_TYPE_Component, - - /** - * 9 pin DIN (Deutsches Institut für Normung) connector - */ - LIBGAMMA_CONNECTOR_TYPE_9PinDIN, - - /** - * DisplayPort - */ - LIBGAMMA_CONNECTOR_TYPE_DisplayPort, - - /** - * High-Definition Multimedia Interface (HDMI), unknown type - */ - LIBGAMMA_CONNECTOR_TYPE_HDMI, - - /** - * High-Definition Multimedia Interface, type A (HDMI-A) - */ - LIBGAMMA_CONNECTOR_TYPE_HDMIA, - - /** - * High-Definition Multimedia Interface, type B (HDMI-B) - */ - LIBGAMMA_CONNECTOR_TYPE_HDMIB, - - /** - * Television, unknown connector - */ - LIBGAMMA_CONNECTOR_TYPE_TV, - - /** - * Embedded DisplayPort (eDP) - */ - LIBGAMMA_CONNECTOR_TYPE_eDP, - - /** - * A virtual connector - */ - LIBGAMMA_CONNECTOR_TYPE_VIRTUAL, - - /** - * Display Serial Interface (DSI) - */ - LIBGAMMA_CONNECTOR_TYPE_DSI, - - /** - * LFP connector - * - * (What is this?) - */ - LIBGAMMA_CONNECTOR_TYPE_LFP - -} libgamma_connector_type_t; - -/** - * The number of values defined in `libgamma_connector_type_t` - */ -#define LIBGAMMA_CONNECTOR_TYPE_COUNT 20 - -/** - * Orders for subpixels - * - * Currently the possible values are very biased - * to LCD, Plasma and monochrome monitors - */ -typedef enum libgamma_subpixel_order { - /** - * The adjustment method does not know the order of the subpixels - * - * (This could be considered an error) - */ - LIBGAMMA_SUBPIXEL_ORDER_UNKNOWN = 0, - - /** - * There are no subpixels in the monitor - */ - LIBGAMMA_SUBPIXEL_ORDER_NONE, - - /** - * The subpixels are ordered red, green and then blue, from left to right - */ - LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_RGB, - - /** - * The subpixels are ordered blue, green and then red, from left to right - */ - LIBGAMMA_SUBPIXEL_ORDER_HORIZONTAL_BGR, - - /** - * The subpixels are ordered red, green and then blue, from the top down - */ - LIBGAMMA_SUBPIXEL_ORDER_VERTICAL_RGB, - - /** - * The subpixels are ordered blue, green and then red, from the top down - */ - LIBGAMMA_SUBPIXEL_ORDER_VERTICAL_BGR - -} libgamma_subpixel_order_t; - -/** - * The number of values defined in `libgamma_subpixel_order_t` - */ -#define LIBGAMMA_SUBPIXEL_ORDER_COUNT 6 - - -/** - * Answer enum to a decision problem - */ -typedef enum libgamma_decision { - /** - * The answer is negative - */ - LIBGAMMA_NO = 0, - - /** - * The answer is unknown - */ - LIBGAMMA_MAYBE = 1, - - /** - * The answer is positive - */ - LIBGAMMA_YES = 2 - -} libgamma_decision_t; - - -/** - * For a `libgamma_crtc_information_t` fill in the values for - * `edid` and `edid_length` and report errors to `edid_error` - */ -#define LIBGAMMA_CRTC_INFO_EDID (1 << 0) - -/** - * For a `libgamma_crtc_information_t` fill in the value - * for `width_mm` and report errors to `width_mm_error` - */ -#define LIBGAMMA_CRTC_INFO_WIDTH_MM (1 << 1) - -/** - * For a `libgamma_crtc_information_t` fill in the value - * for `height_mm` and report errors to `height_mm_error` - */ -#define LIBGAMMA_CRTC_INFO_HEIGHT_MM (1 << 2) - -/** - * For a `libgamma_crtc_information_t` fill in the value for - * `width_mm_edid` and report errors to `width_mm_edid_error` - */ -#define LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID (1 << 3) - -/** - * For a `libgamma_crtc_information_t` fill in the value for - * `height_mm_edid` and report errors to `height_mm_edid_error` - */ -#define LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID (1 << 4) - -/** - * For a `libgamma_crtc_information_t` fill in the values for - * `red_gamma_size`, `green_gamma_size`, and `blue_gamma_size`, - * and report errors to `gamma_size_error` - */ -#define LIBGAMMA_CRTC_INFO_GAMMA_SIZE (1 << 5) - -/** - * For a `libgamma_crtc_information_t` fill in the value for - * `gamma_depth` and report errors to `gamma_depth_error` - */ -#define LIBGAMMA_CRTC_INFO_GAMMA_DEPTH (1 << 6) - -/** - * For a `libgamma_crtc_information_t` fill in the value for - * `gamma_support` and report errors to `gamma_support_error` - */ -#define LIBGAMMA_CRTC_INFO_GAMMA_SUPPORT (1 << 7) - -/** - * For a `libgamma_crtc_information_t` fill in the value for - * `subpixel_order` and report errors to `subpixel_order_error` - */ -#define LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER (1 << 8) - -/** - * For a `libgamma_crtc_information_t` fill in the - * value for `active` and report errors to `active_error` - */ -#define LIBGAMMA_CRTC_INFO_ACTIVE (1 << 9) - -/** - * For a `libgamma_crtc_information_t` fill in the value for - * `connector_name` and report errors to `connector_name_error` - */ -#define LIBGAMMA_CRTC_INFO_CONNECTOR_NAME (1 << 10) - -/** - * For a `libgamma_crtc_information_t` fill in the value for - * `connector_type` and report errors to `connector_type_error` - */ -#define LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE (1 << 11) - -/** - * For a `libgamma_crtc_information_t` fill in the - * values for `gamma_red`, `gamma_green`, and `gamma_blue` - * and report errors to `gamma_error` - */ -#define LIBGAMMA_CRTC_INFO_GAMMA (1 << 12) - -/** - * The number of `LIBGAMMA_CRTC_INFO_*` values defined - */ -#define LIBGAMMA_CRTC_INFO_COUNT 13 - -/** - * Macro for both `libgamma_crtc_information_t` fields - * that can specify the size of the monitor's viewport - * as specified in the monitor's Extended Display - * Information Data - */ -#define LIBGAMMA_CRTC_INFO_MACRO_EDID_VIEWPORT (LIBGAMMA_CRTC_INFO_WIDTH_MM_EDID | LIBGAMMA_CRTC_INFO_HEIGHT_MM_EDID) - -/** - * Macro for all `libgamma_crtc_information_t` fields - * that can be filled if the adjustment method have - * support for reading the monitors' Extended Display - * Information Data - */ -#define LIBGAMMA_CRTC_INFO_MACRO_EDID (LIBGAMMA_CRTC_INFO_EDID | LIBGAMMA_CRTC_INFO_MACRO_EDID_VIEWPORT | LIBGAMMA_CRTC_INFO_GAMMA) - -/** - * Macro for both `libgamma_crtc_information_t` fields - * that can specify the size of the monitor's viewport - * as provided by the adjustment method without this - * library having to parse the monitor's Extended Display - * Information Data - */ -#define LIBGAMMA_CRTC_INFO_MACRO_VIEWPORT (LIBGAMMA_CRTC_INFO_WIDTH_MM | LIBGAMMA_CRTC_INFO_HEIGHT_MM) - -/** - * Macro for the `libgamma_crtc_information_t` fields - * that specifies the CRTC's gamma ramp sizes and gamma - * ramp depth - */ -#define LIBGAMMA_CRTC_INFO_MACRO_RAMP (LIBGAMMA_CRTC_INFO_GAMMA_SIZE | LIBGAMMA_CRTC_INFO_GAMMA_DEPTH) - -/** - * Macro for the `libgamma_crtc_information_t` fields - * that specifies the CRTC's connector type and the - * partition unique name of the connector - */ -#define LIBGAMMA_CRTC_INFO_MACRO_CONNECTOR (LIBGAMMA_CRTC_INFO_CONNECTOR_NAME | LIBGAMMA_CRTC_INFO_CONNECTOR_TYPE) - -/** - * Macro for the `libgamma_crtc_information_t` fields - * that required there is a monitor attached to the - * connector, and that status itself - */ -#define LIBGAMMA_CRTC_INFO_MACRO_ACTIVE (LIBGAMMA_CRTC_INFO_MACRO_EDID | LIBGAMMA_CRTC_INFO_MACRO_VIEWPORT |\ - LIBGAMMA_CRTC_INFO_SUBPIXEL_ORDER | LIBGAMMA_CRTC_INFO_ACTIVE) - - - -/** - * Cathode ray tube controller information data structure - */ -typedef struct libgamma_crtc_information { - /** - * The Extended Display Identification Data associated with - * the attached monitor. - * - * This is raw byte array that is usually 128 bytes long. - * It is not NUL-terminate, rather its length is stored in - * `edid_length`. - */ - unsigned char *edid; - - /** - * The length of `edid` - */ - size_t edid_length; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int edid_error; - - - /** - * The phyical width, in millimetres, of the viewport of the - * attached monitor, as reported by the adjustment method - * - * This value may be incorrect, which is a known issue with - * the X server where it is the result of the X server - * attempting the estimate the size on its own - * - * Zero means that its is not applicable, which is the case - * for projectors - */ - size_t width_mm; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int width_mm_error; - - - /** - * The phyical height, in millimetres, of the viewport of the - * attached monitor, as reported by the adjustment method - * - * This value may be incorrect, which is a known issue with - * the X server where it is the result of the X server - * attempting the estimate the size on its own - * - * Zero means that its is not applicable, which is the case - * for projectors - */ - size_t height_mm; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int height_mm_error; - - - /** - * The phyical width, in millimetres, of the viewport of the - * attached monitor, as reported by it the monitor's Extended - * Display Information Data - * - * This value can only contain whole centimetres, which means - * that the result is always zero modulus ten. However, this - * could change with revisions of the EDID structure. - * - * Zero means that its is not applicable, which is the case - * for projectors. - */ - size_t width_mm_edid; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int width_mm_edid_error; - - - /** - * The phyical height, in millimetres, of the viewport of the - * attached monitor, as reported by it the monitor's Extended - * Display Information Data - * - * This value can only contain whole centimetres, which means - * that the result is always zero modulus ten. However, this - * could change with revisions of the EDID structure. - * - * Zero means that its is not applicable, which is the case - * for projectors - */ - size_t height_mm_edid; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int height_mm_edid_error; - - - /** - * The size of the encoding axis of the red gamma ramp - */ - size_t red_gamma_size; - - /** - * The size of the encoding axis of the green gamma ramp - */ - size_t green_gamma_size; - - /** - * The size of the encoding axis of the blue gamma ramp - */ - size_t blue_gamma_size; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int gamma_size_error; - - - /** - * The bit-depth of the value axes of gamma ramps, - * -1 for single precision floating point, and -2 for - * double precision floating point - */ - signed gamma_depth; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int gamma_depth_error; - - - /** - * `LIBGAMMA_NO` indicates that the CRTC does not support - * gamma ramp adjustments. `LIBGAMMA_MAYBE` indicates that - * the CRTC may or may not support gamma ramp adjustments, - * meaning that the display server really does not know, but - * the protocol is available. `LIBGAMMA_NO` indicates that - * the CRTC does support gamma ramp adjustments. - */ - libgamma_decision_t gamma_support; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int gamma_support_error; - - - /** - * The layout of the subpixels - * - * You cannot count on this value — especially for CRT:s — - * but it is provided anyway as a means of distinguishing - * monitors - */ - libgamma_subpixel_order_t subpixel_order; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int subpixel_order_error; - - - /** - * Whether there is a monitor connected to the CRTC - */ - int active; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int active_error; - - - /** - * The name of the connector as designated by the display - * server or as give by this library in case the display - * server lacks this feature. - */ - char *connector_name; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library. - */ - int connector_name_error; - - - /** - * The type of the connector that is associated with the CRTC. - */ - libgamma_connector_type_t connector_type; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library. - */ - int connector_type_error; - - - /** - * The gamma characteristics of the monitor as reported - * in its Extended Display Information Data. The value - * holds the value for the red channel. - * - * If you do not have and more accurate measurement of the - * gamma for the monitor this could be used to give a rought - * gamma correction; simply divide the value with 2.2 and use - * the result for the red channel in the gamma correction. - */ - float gamma_red; - - /** - * The gamma characteristics of the monitor as reported - * in its Extended Display Information Data. The value - * holds the value for the green channel. - * - * If you do not have and more accurate measurement of the - * gamma for the monitor this could be used to give a rought - * gamma correction; simply divide the value with 2.2 and use - * the result for the green channel in the gamma correction. - */ - float gamma_green; - - /** - * The gamma characteristics of the monitor as reported - * in its Extended Display Information Data. The value - * holds the value for the blue channel. - * - * If you do not have and more accurate measurement of the - * gamma for the monitor this could be used to give a rought - * gamma correction; simply divide the value with 2.2 and use - * the result for the blue channel in the gamma correction. - */ - float gamma_blue; - - /** - * Zero on success, positive it holds the value `errno` had - * when the reading failed, otherwise (negative) the value - * of an error identifier provided by this library - */ - int gamma_error; - -} libgamma_crtc_information_t; - - - -/** - * Gamma ramp structure for 8-bit gamma ramps - */ -typedef struct libgamma_gamma_ramps8 { - /** - * The size of `red` - */ - size_t red_size; - - /** - * The size of `green` - */ - size_t green_size; - - /** - * The size of `blue` - */ - size_t blue_size; - - /** - * The gamma ramp for the red channel - */ - uint8_t *red; - - /** - * The gamma ramp for the green channel - */ - uint8_t *green; - - /** - * The gamma ramp for the blue channel - */ - uint8_t *blue; - -} libgamma_gamma_ramps8_t; - - -/** - * Gamma ramp structure for 16-bit gamma ramps - */ -typedef struct libgamma_gamma_ramps16 -{ - /** - * The size of `red` - */ - size_t red_size; - - /** - * The size of `green` - */ - size_t green_size; - - /** - * The size of `blue` - */ - size_t blue_size; - - /** - * The gamma ramp for the red channel - */ - uint16_t *red; - - /** - * The gamma ramp for the green channel - */ - uint16_t *green; - - /** - * The gamma ramp for the blue channel - */ - uint16_t *blue; - -} libgamma_gamma_ramps16_t; - - -/** - * Gamma ramp structure for 32-bit gamma ramps - */ -typedef struct libgamma_gamma_ramps32 -{ - /** - * The size of `red` - */ - size_t red_size; - - /** - * The size of `green` - */ - size_t green_size; - - /** - * The size of `blue` - */ - size_t blue_size; - - /** - * The gamma ramp for the red channel - */ - uint32_t *red; - - /** - * The gamma ramp for the green channel - */ - uint32_t *green; - - /** - * The gamma ramp for the blue channel - */ - uint32_t *blue; - -} libgamma_gamma_ramps32_t; - - -/** - * Gamma ramp structure for 64-bit gamma ramps - */ -typedef struct libgamma_gamma_ramps64 -{ - /** - * The size of `red` - */ - size_t red_size; - - /** - * The size of `green` - */ - size_t green_size; - - /** - * The size of `blue` - */ - size_t blue_size; - - /** - * The gamma ramp for the red channel - */ - uint64_t *red; - - /** - * The gamma ramp for the green channel - */ - uint64_t *green; - - /** - * The gamma ramp for the blue channel - */ - uint64_t *blue; - -} libgamma_gamma_ramps64_t; - - -/** - * Gamma ramp structure for `float` gamma ramps - */ -typedef struct libgamma_gamma_rampsf -{ - /** - * The size of `red` - */ - size_t red_size; - - /** - * The size of `green` - */ - size_t green_size; - - /** - * The size of `blue` - */ - size_t blue_size; - - /** - * The gamma ramp for the red channel - */ - float *red; - - /** - * The gamma ramp for the green channel - */ - float *green; - - /** - * The gamma ramp for the blue channel - */ - float *blue; - -} libgamma_gamma_rampsf_t; - - -/** - * Gamma ramp structure for `double` gamma ramps - */ -typedef struct libgamma_gamma_rampsd -{ - /** - * The size of `red` - */ - size_t red_size; - - /** - * The size of `green` - */ - size_t green_size; - - /** - * The size of `blue` - */ - size_t blue_size; - - /** - * The gamma ramp for the red channel - */ - double *red; - - /** - * The gamma ramp for the green channel - */ - double *green; - - /** - * The gamma ramp for the blue channel - */ - double *blue; - -} libgamma_gamma_rampsd_t; - - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int libgamma_gamma_ramps8_initialise(libgamma_gamma_ramps8_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps8_initialise` or otherwise - * initialised in the proper manner - * - * @param this The gamma ramps - */ -void libgamma_gamma_ramps8_destroy(libgamma_gamma_ramps8_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps8_initialise` or otherwise - * initialised in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void libgamma_gamma_ramps8_free(libgamma_gamma_ramps8_t *restrict); - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int libgamma_gamma_ramps16_initialise(libgamma_gamma_ramps16_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps16_initialise` or otherwise - * initialised in the proper manner - * - * @param this The gamma ramps - */ -void libgamma_gamma_ramps16_destroy(libgamma_gamma_ramps16_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps16_initialise` or otherwise - * initialised in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void libgamma_gamma_ramps16_free(libgamma_gamma_ramps16_t *restrict); - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated. - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int libgamma_gamma_ramps32_initialise(libgamma_gamma_ramps32_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps32_initialise` or otherwise - * initialised in the proper manner - * - * @param this The gamma ramps - */ -void libgamma_gamma_ramps32_destroy(libgamma_gamma_ramps32_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps32_initialise` or otherwise - * initialised in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void libgamma_gamma_ramps32_free(libgamma_gamma_ramps32_t *restrict); - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation. - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated. - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int libgamma_gamma_ramps64_initialise(libgamma_gamma_ramps64_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps64_initialise` or otherwise - * initialised in the proper manner - * - * @param this The gamma ramps - */ -void libgamma_gamma_ramps64_destroy(libgamma_gamma_ramps64_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_ramps64_initialise` or otherwise - * initialised in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void libgamma_gamma_ramps64_free(libgamma_gamma_ramps64_t *restrict); - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int libgamma_gamma_rampsf_initialise(libgamma_gamma_rampsf_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_rampsf_initialise` or otherwise - * initialised in the proper manner - * - * @param this The gamma ramps - */ -void libgamma_gamma_rampsf_destroy(libgamma_gamma_rampsf_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_rampsf_initialise` or otherwise - * initialised in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void libgamma_gamma_rampsf_free(libgamma_gamma_rampsf_t *restrict); - - -/** - * Initialise a gamma ramp in the proper way that allows all adjustment - * methods to read from and write to it without causing segmentation violation - * - * The input must have `red_size`, `green_size`, and `blue_size` set to the - * sizes of the gamma ramps that should be allocated - * - * @param this The gamma ramps - * @return Zero on success, -1 on allocation error, `errno` will be set accordingly - */ -int libgamma_gamma_rampsd_initialise(libgamma_gamma_rampsd_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_rampsd_initialise` or otherwise - * initialised in the proper manner - * - * @param this The gamma ramps - */ -void libgamma_gamma_rampsd_destroy(libgamma_gamma_rampsd_t *restrict); - -/** - * Release resources that are held by a gamma ramp strcuture that - * has been allocated by `libgamma_gamma_rampsd_initialise` or otherwise - * initialised in the proper manner, as well as release the pointer - * to the structure - * - * @param this The gamma ramps - */ -void libgamma_gamma_rampsd_free(libgamma_gamma_rampsd_t *restrict); - - - -#ifndef __GNUC__ -# undef __attribute__ -#endif - -#endif diff --git a/src/lib/libgamma.h b/src/lib/libgamma.h deleted file mode 100644 index cc63fb8..0000000 --- a/src/lib/libgamma.h +++ /dev/null @@ -1,11 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#ifndef LIBGAMMA_H -#define LIBGAMMA_H - -#include "libgamma-config.h" /* Must be first. */ - -#include "libgamma-method.h" -#include "libgamma-facade.h" -#include "libgamma-error.h" - -#endif -- cgit v1.2.3-70-g09d2