diff options
-rw-r--r-- | .gitignore | 5 | ||||
-rw-r--r-- | DEPENDENCIES | 38 | ||||
-rw-r--r-- | Makefile | 126 | ||||
-rwxr-xr-x | src/__main__.py | 2 | ||||
-rw-r--r-- | src/blueshift_drm.pyx | 589 | ||||
-rw-r--r-- | src/blueshift_drm_c.c | 575 | ||||
-rw-r--r-- | src/blueshift_drm_c.h | 239 | ||||
-rw-r--r-- | src/blueshift_idcrtc.c | 406 | ||||
-rw-r--r-- | src/blueshift_quartz.pyx | 181 | ||||
-rw-r--r-- | src/blueshift_quartz_c.c | 255 | ||||
-rw-r--r-- | src/blueshift_quartz_c.h | 85 | ||||
-rw-r--r-- | src/blueshift_randr.pyx | 161 | ||||
-rw-r--r-- | src/blueshift_randr_c.c | 301 | ||||
-rw-r--r-- | src/blueshift_randr_c.h | 100 | ||||
-rw-r--r-- | src/blueshift_vidmode.pyx | 155 | ||||
-rw-r--r-- | src/blueshift_vidmode_c.c | 143 | ||||
-rw-r--r-- | src/blueshift_vidmode_c.h | 66 | ||||
-rw-r--r-- | src/blueshift_w32gdi.pyx | 151 | ||||
-rw-r--r-- | src/blueshift_w32gdi_c.c | 181 | ||||
-rw-r--r-- | src/blueshift_w32gdi_c.h | 83 | ||||
-rw-r--r-- | src/colour.py | 2 | ||||
-rw-r--r-- | src/fake_quartz.c | 235 | ||||
-rw-r--r-- | src/fake_quartz.h | 66 | ||||
-rw-r--r-- | src/fake_w32gdi.c | 204 | ||||
-rw-r--r-- | src/fake_w32gdi.h | 73 | ||||
-rw-r--r-- | src/icc.py | 2 | ||||
-rw-r--r-- | src/monitor.py | 9 |
27 files changed, 38 insertions, 4395 deletions
@@ -17,11 +17,6 @@ obj/ *.gch *.zip __pycache__/ -/src/blueshift_randr.c -/src/blueshift_vidmode.c -/src/blueshift_drm.c -/src/blueshift_w32gdi.c -/src/blueshift_quartz.c *.info *.pdf *.dvi diff --git a/DEPENDENCIES b/DEPENDENCIES index d190dd6..88d61e6 100644 --- a/DEPENDENCIES +++ b/DEPENDENCIES @@ -2,41 +2,30 @@ RUNTIME DEPENDENCIES: python3 + pylibgamma + solar-python argparser-python (required but can function partially if missing) - + libxcb (opt-out) - For RandR support and CRTC listing and ICC profile listing under X - - libxcb (opt-in) - For testing Windows support on GNU/Linux under X. - - libx11, libxxf86vm (opt-out) - For VidMode support under X - - libdrm (opt-out) - For support under TTY via DRM - + For ICC profile listing under X + linux (optional) For backlight support via sysfs - + linux, adjbacklight (optional) For permission-hasslefree backlight adjustments via sysfs - + wget (optional) For weather conditions, default command -MAKE DEPENDENCIES: +BUILD DEPENDENCIES: - cython - gcc python3 - libxcb (opt-out/opt-in, see runtime dependencies) - libx11 (opt-out, see runtime dependencies) - libxxf86vm (opt-out, see runtime dependencies) - libdrm (opt-out, see runtime dependencies) + gcc (opt-out) + libxcb (opt-out) make coreutils sed @@ -45,8 +34,15 @@ MAKE DEPENDENCIES: auto-auto-complete (opt-out) +BUILD DEPENDENCIES: + + make + coreutils + + DEPENDENCY SOURCES: + pylibgamma https://github.com/maandree/pylibgamma solar-python https://github.com/maandree/solar-python argparser-python https://github.com/maandree/argparser adjbacklight https://github.com/maandree/adjbacklight @@ -8,16 +8,12 @@ PREFIX ?= /usr # The command path excluding prefix BIN ?= /bin -# The library path excluding prefix -LIB ?= /lib # The executable library path excluding prefix LIBEXEC ?= /libexec # The resource path excluding prefix DATA ?= /share # The command path including prefix BINDIR ?= $(PREFIX)$(BIN) -# The library path including prefix -LIBDIR ?= $(PREFIX)$(LIB) # The executable library path including prefix LIBEXECDIR ?= $(PREFIX)$(LIBEXEC) # The resource path including prefix @@ -36,81 +32,42 @@ COMMAND ?= blueshift # The name of the package as it should be installed PKGNAME ?= blueshift -# Bindings for display server access -DEFAULT_SERVER_BINDINGS = randr vidmode drm -ifeq ($(FAKE_W32),y) -DEFAULT_SERVER_BINDINGS += w32gdi -endif -ifeq ($(FAKE_MAC),y) -DEFAULT_SERVER_BINDINGS += quartz -endif -SERVER_BINDINGS ?= $(DEFAULT_SERVER_BINDINGS) # Executable bindings for display server access -EXECS ?= idcrtc iccprofile +EXECS ?= iccprofile # Executable library files EXECLIBS = $(foreach E,$(EXECS),blueshift_$(E)) # The installed pkg-config command PKGCONFIG ?= pkg-config # Optimisation settings for C code compilation -OPTIMISE ?= -O6 -g +OPTIMISE ?= -O3 -g # Warnings settings for C code compilation -WARN = -Wall -Wextra -pedantic -Wdouble-promotion -Wformat=2 -Winit-self -Wmissing-include-dirs \ - -Wtrampolines -Wmissing-prototypes -Wmissing-declarations -Wnested-externs \ - -Wno-variadic-macros -Wsync-nand -Wunsafe-loop-optimizations -Wcast-align \ - -Wdeclaration-after-statement -Wundef -Wbad-function-cast -Wwrite-strings -Wlogical-op \ - -Wstrict-prototypes -Wold-style-definition -Wpacked -Wvector-operation-performance \ - -Wunsuffixed-float-constants -Wsuggest-attribute=const -Wsuggest-attribute=noreturn \ - -Wsuggest-attribute=format -Wnormalized=nfkc \ - -fstrict-aliasing -fipa-pure-const -ftree-vrp -fstack-usage -funsafe-loop-optimizations -# Warnings violated by Cython and therefore only use for C and not Cython -CWARN = -Wshadow -Wredundant-decls -Winline -Wcast-qual -Wsign-conversion -Wstrict-overflow=5 \ - -Wconversion -Wsuggest-attribute=pure -Wswitch-default -Wstrict-aliasing=1 \ - -fstrict-overflow -Wfloat-equal +WARN = -Wall -Wextra -pedantic -Wdouble-promotion -Wformat=2 -Winit-self -Wmissing-include-dirs \ + -Wtrampolines -Wmissing-prototypes -Wmissing-declarations -Wnested-externs \ + -Wno-variadic-macros -Wsync-nand -Wunsafe-loop-optimizations -Wcast-align \ + -Wdeclaration-after-statement -Wundef -Wbad-function-cast -Wwrite-strings -Wlogical-op \ + -Wstrict-prototypes -Wold-style-definition -Wpacked -Wvector-operation-performance \ + -Wunsuffixed-float-constants -Wsuggest-attribute=const -Wsuggest-attribute=noreturn \ + -Wsuggest-attribute=format -Wnormalized=nfkc -fstrict-aliasing -fipa-pure-const -ftree-vrp \ + -fstack-usage -funsafe-loop-optimizations -Wshadow -Wredundant-decls -Winline -Wcast-qual \ + -Wsign-conversion -Wstrict-overflow=5 -Wconversion -Wsuggest-attribute=pure -Wswitch-default \ + -Wstrict-aliasing=1 -fstrict-overflow -Wfloat-equal #not used: -Wtraditional (tranditional C function definitions are ridiculous), # -Wpadded (useless for this project), -Wc++-compat (bad practice) #not used because of libxcb's API: -Waggregate-return, -Wtraditional-conversion (also useless) # The C standard for C code compilation STD = c99 -LIBS_idcrtc = xcb-randr LIBS_iccprofile = xcb -LIBS_randr = xcb-randr -LIBS_vidmode = x11 xxf86vm -LIBS_drm = libdrm -LIBS_w32gdi = -LIBS_quartz = -ifeq ($(FAKE_W32),y) -LIBS_w32gdi = $(LIBS_randr) -endif -ifeq ($(FAKE_MAC),y) -LIBS_quartz = $(LIBS_randr) -endif -LD_idcrtc = LD_iccprofile = -LD_randr = -LD_vidmode = -LD_drm = -LD_w32gdi = -F_ApplicationServices = /System/Library/Frameworks/ApplicationServices.framework -I_ApplicationServices = $(F_ApplicationServices)/Versions/A/Frameworks/CoreGraphics.framework/Versions/A/Headers -LD_quartz = -I$(I_ApplicationServices) -F$(F_ApplicationServices) -framework ApplicationServices -ifeq ($(FAKE_W32),y) -LD_w32gdi = $(LD_randr) -endif -ifeq ($(FAKE_MAC),y) -LD_quartz = $(LD_randr) -endif -LIBS = python3 $(foreach B,$(SERVER_BINDINGS) $(EXECS),$(LIBS_$(B))) +LIBS = $(foreach B,$(EXECS),$(LIBS_$(B))) FLAGS = $$($(PKGCONFIG) --cflags $(LIBS)) -std=$(STD) $(WARN) $(OPTIMISE) \ - -fPIC $(CFLAGS) $(LDFLAGS) $(CPPFLAGS) + $(CFLAGS) $(LDFLAGS) $(CPPFLAGS) # Resource files DATAFILES = 2deg 10deg redshift redshift_old # Python source files PYFILES = __main__.py colour.py curve.py monitor.py solar.py icc.py adhoc.py \ backlight.py blackbody.py aux.py weather.py interpolation.py -# Library files -CBINDINGS = $(foreach B,$(SERVER_BINDINGS),blueshift_$(B).so) # Configuration script example files EXAMPLES = comprehensive sleepmode crtc-detection crtc-searching logarithmic \ xmobar xpybar stored-settings current-settings xmonad threaded \ @@ -127,7 +84,7 @@ default: command info shell all: command doc shell .PHONY: command -command: $(foreach C,$(CBINDINGS),bin/$(C)) $(foreach E,$(EXECLIBS),bin/$(E)) bin/blueshift +command: bin/blueshift $(foreach E,$(EXECLIBS),bin/$(E)) # Build rules for C source files @@ -136,50 +93,10 @@ bin/blueshift_%: obj/blueshift_%.o @mkdir -p bin $(CC) $(FLAGS) $$($(PKGCONFIG) --libs $(LIBS_$*)) $(LD_$*) -o $@ $^ -bin/blueshift_%.so: obj/blueshift_%.o obj/blueshift_%_c.o - @mkdir -p bin - $(CC) $(FLAGS) $$($(PKGCONFIG) --libs $(LIBS_$*)) $(LD_$*) -shared -o $@ $^ - obj/%.o: src/%.c @mkdir -p obj - $(CC) $(FLAGS) $(CWARN) -c -o $@ $< - -obj/%.o: src/%.c src/%.h - @mkdir -p obj - $(CC) $(FLAGS) $(CWARN) -c -o $@ $< - -obj/%_c.o: src/%_c.c src/%_c.h - @mkdir -p obj - $(CC) $(FLAGS) $(CWARN) -c -o $@ $< - -obj/%.o: obj/%.c - @mkdir -p obj $(CC) $(FLAGS) -c -o $@ $< -ifeq ($(FAKE_W32),y) -obj/blueshift_w32gdi_c.o: L=w32gdi -obj/blueshift_w32gdi_c.o: src/blueshift_w32gdi_c.c src/blueshift_w32gdi_c.h \ - obj/fake_w32gdi.o src/fake_w32gdi.h - @mkdir -p bin - $(CC) $(FLAGS) $$($(PKGCONFIG) --libs $(LIBS_$(L))) $(LD_$(L)) -shared -DFAKE_W32GDI -o $@ $^ -endif - -ifeq ($(FAKE_MAC),y) -obj/blueshift_quartz_c.o: L=quartz -obj/blueshift_quartz_c.o: src/blueshift_quartz_c.c src/blueshift_quartz_c.h \ - obj/fake_quartz.o src/fake_quartz.h - @mkdir -p bin - $(CC) $(FLAGS) $$($(PKGCONFIG) --libs $(LIBS_$(L))) $(LD_$(L)) -shared -DFAKE_QUARTZ -o $@ $^ -endif - - -# Build rules for Cython source files - -obj/%.c: src/%.pyx - @mkdir -p obj - if ! cython -3 -v $<; then rm src/$*.c ; false ; fi - mv src/$*.c $@ - # Build rules for Python source files @@ -193,9 +110,9 @@ obj/blueshift.zip: $(foreach F,$(PYFILES),obj/$(F)) cd obj && zip ../$@ $(foreach F,$(PYFILES),$(F)) obj/%.py: src/%.py + @mkdir -p obj cp $< $@ sed -i '/^DATADIR *= /s#^.*$$#DATADIR = '\''$(DATADIR)/$(PKGNAME)'\''#' $@ - sed -i '/^LIBDIR *= /s#^.*$$#LIBDIR = '\''$(LIBDIR)'\''#' $@ sed -i '/^LIBEXECDIR *= /s#^.*$$#LIBEXECDIR = '\''$(LIBEXECDIR)'\''#' $@ @@ -272,18 +189,13 @@ install-all: install-base install-doc install-shell install-base: install-command install-license .PHONY: install-command -install-command: install-command-bin install-command-lib install-command-libexec install-command-share +install-command: install-command-bin install-command-libexec install-command-share .PHONY: install-command-bin install-command-bin: bin/blueshift install -dm755 -- "$(DESTDIR)$(BINDIR)" install -m755 $< -- "$(DESTDIR)$(BINDIR)/$(COMMAND)" -.PHONY: install-command-lib -install-command-lib: $(foreach C,$(CBINDINGS),bin/$(C)) - install -dm755 -- "$(DESTDIR)$(LIBDIR)" - install -m755 $^ -- "$(DESTDIR)$(LIBDIR)" - .PHONY: install-command-libexec install-command-libexec: $(foreach E,$(EXECLIBS),bin/$(E)) install -dm755 -- "$(DESTDIR)$(LIBEXECDIR)" @@ -355,7 +267,6 @@ install-fish: bin/blueshift.fish .PHONY: uninstall uninstall: -rm -- "$(DESTDIR)$(BINDIR)/$(COMMAND)" - -rm -- $(foreach C,$(CBINDINGS),"$(DESTDIR)$(LIBDIR)/$(C)") -rm -- $(foreach E,$(EXECLIBS),"$(DESTDIR)$(LIBEXECDIR)/$(E)") -rmdir -- "$(DESTDIR)$(LIBEXECDIR)" -rm -- "$(DESTDIR)$(LICENSEDIR)/$(PKGNAME)/COPYING" @@ -387,6 +298,5 @@ uninstall: .PHONY: all clean: - -rm -r bin obj src/blueshift_{randr,vidmode,drm,w32gid,quartz}.c \ - blueshift.{info,pdf,ps,dvi} *.su src/*.su + -rm -r bin obj blueshift.{info,pdf,ps,dvi} *.su src/*.su diff --git a/src/__main__.py b/src/__main__.py index e93b196..a9ffbba 100755 --- a/src/__main__.py +++ b/src/__main__.py @@ -81,7 +81,7 @@ setproctitle(sys.argv[0]) global i_size, o_size, r_curve, g_curve, b_curve, clip_result, reset, panicgate, reset_on_error global periodically, wait_period, fadein_time, fadeout_time, fadein_steps, fadeout_steps global monitor_controller, running, continuous_run, panic, _globals_, conf_storage, parser -global signal_SIGTERM, signal_SIGUSR1, signal_SIGUSR2, DATADIR, LIBDIR, LIBEXECDIR +global signal_SIGTERM, signal_SIGUSR1, signal_SIGUSR2, DATADIR, LIBEXECDIR ## Open all modules that the configuration diff --git a/src/blueshift_drm.pyx b/src/blueshift_drm.pyx deleted file mode 100644 index dae3bb7..0000000 --- a/src/blueshift_drm.pyx +++ /dev/null @@ -1,589 +0,0 @@ -# -*- python -*- - -# Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - -cimport cython -from libc.stdlib cimport malloc, free, realloc -from libc.stdint cimport * - - -cdef extern void blueshift_drm_close() -''' -Free all resources, but you need to close all connections first -''' - -cdef extern int blueshift_drm_card_count() -''' -Get the number of cards present on the system - -@return The number of cards present on the system -''' - -cdef extern int blueshift_drm_open_card(int card_index) -''' -Open connection to a graphics card - -@param card_index The index of the graphics card -@return -1 on failure, otherwise an identifier for the connection to the card -''' - -cdef extern void blueshift_drm_update_card(int connection) -''' -Update the resource, required after `blueshift_drm_open_card` - -@param connection The identifier for the connection to the card -''' - -cdef extern void blueshift_drm_close_card(int connection) -''' -Close connection to the graphics card - -@param connection The identifier for the connection to the card -''' - -cdef extern int blueshift_drm_crtc_count(int connection) -''' -Return the number of CRTC:s on the opened card - -@param connection The identifier for the connection to the card -@return The number of CRTC:s on the opened card -''' - -cdef extern int blueshift_drm_connector_count(int connection) -''' -Return the number of connectors on the opened card - -@param connection The identifier for the connection to the card -@return The number of connectors on the opened card -''' - -cdef extern int blueshift_drm_gamma_size(int connection, int crtc_index) -''' -Return the size of the gamma ramps on a CRTC - -@param connection The identifier for the connection to the card -@param crtc_index The index of the CRTC -@return The size of the gamma ramps on a CRTC -''' - -cdef extern int blueshift_drm_get_gamma_ramps(int connection, int crtc_index, int gamma_size, - uint16_t* red, uint16_t* green, uint16_t* blue) -''' -Get the current gamma ramps of a monitor - -@param connection The identifier for the connection to the card -@param crtc_index The index of the CRTC to read from -@param gamma_size The size a gamma ramp -@param red Storage location for the red gamma ramp -@param green Storage location for the green gamma ramp -@param blue Storage location for the blue gamma ramp -@return Zero on success -''' - -cdef extern int blueshift_drm_set_gamma_ramps(int connection, int crtc_index, int gamma_size, - uint16_t* red, uint16_t* green, uint16_t* blue) -''' -Set the gamma ramps of the of a monitor - -@param connection The identifier for the connection to the card -@param crtc_index The index of the CRTC to read from -@param gamma_size The size a gamma ramp -@param red The red gamma ramp -@param green The green gamma ramp -@param blue The blue gamma ramp -@return Zero on success -''' - -cdef extern void blueshift_drm_open_connector(int connection, int connector_index) -''' -Acquire information about a connector - -@param connection The identifier for the connection to the card -@param connector_index The index of the connector -''' - -cdef extern void blueshift_drm_close_connector(int connection, int connector_index) -''' -Release information about a connector - -@param connection The identifier for the connection to the card -@param connector_index The index of the connector -''' - -cdef extern int blueshift_drm_get_width(int connection, int connector_index) -''' -Get the physical width the monitor connected to a connector - -@param connection The identifier for the connection to the card -@param connector_index The index of the connector -@return The physical width of the monitor in millimetres, 0 if unknown or not connected -''' - -cdef extern int blueshift_drm_get_height(int connection, int connector_index) -''' -Get the physical height the monitor connected to a connector - -@param connection The identifier for the connection to the card -@param connector_index The index of the connector -@return The physical height of the monitor in millimetres, 0 if unknown or not connected -''' - -cdef extern int blueshift_drm_is_connected(int connection, int connector_index) -''' -Get whether a monitor is connected to a connector - -@param connection The identifier for the connection to the card -@param connector_index The index of the connector -@return 1 if there is a connection, 0 otherwise, -1 if unknown -''' - -cdef extern int blueshift_drm_get_crtc(int connection, int connector_index) -''' -Get the index of the CRTC of the monitor connected to a connector - -@param connection The identifier for the connection to the card -@param connector_index The index of the connector -@return The index of the CRTC -''' - -cdef extern int blueshift_drm_get_connector_type_index(int connection, int connector_index) -''' -Get the index of the type of a connector - -@param connection The identifier for the connection to the card -@param connector_index The index of the connector -@return The connector type by index, 0 for unknown -''' - -cdef extern const char* blueshift_drm_get_connector_type_name(int connection, int connector_index) -''' -Get the name of the type of a connector - -@param connection The identifier for the connection to the card -@param connector_index The index of the connector -@return The connector type by name, "Unknown" if not identifiable, - "Unrecognised" if Blueshift does not recognise it. -''' - -cdef extern long int blueshift_drm_get_edid(int connection, int connector_index, char* edid, - long int size, int hexadecimal) -''' -Get the extended display identification data for the monitor connected to a connector - -@param connection The identifier for the connection to the card -@param connector_index The index of the connector -@param edid Storage location for the EDID, it should be 128 bytes, - or 256 bytes if you are counting on the depricated EDID 2.0, - If hexadecimal, twice that + zero termiation. -@param size The size allocated to `edid` excluding your zero termination -@param hexadecimal Whether to convert to hexadecimal representation, this is preferable -@return The length of the found value, 0 if none, as if hex is false -''' - - - -cdef uint16_t* r_shared -''' -Non-threadsafe storage for the red colour curve to be used in native code -''' - -cdef uint16_t* g_shared -''' -Non-threadsafe storage for the green colour curve to be used in native code -''' - -cdef uint16_t* b_shared -''' -Non-threadsafe storage for the blue colour curve to be used in native code -''' - -r_shared = NULL -g_shared = NULL -b_shared = NULL - - - -def drm_close(): - ''' - Free all resources, but you need to close all connections first - ''' - global r_shared, g_shared, b_shared - # Deallocate colour curve storage - if r_shared is not NULL: - free(r_shared) - r_shared = NULL - if g_shared is not NULL: - free(g_shared) - g_shared = NULL - if b_shared is not NULL: - free(b_shared) - b_shared = NULL - # Close all native resources - blueshift_drm_close() - - -def drm_card_count(): - ''' - Get the number of cards present on the system - - @return :int The number of cards present on the system - ''' - return blueshift_drm_card_count() - - -def drm_open_card(int card_index): - ''' - Open connection to a graphics card - - @param card_index The index of the graphics card - @return :int -1 on failure, otherwise an identifier for the connection to the card - ''' - return blueshift_drm_open_card(card_index) - - -def drm_update_card(int connection): - ''' - Update the resource, required after `blueshift_drm_open_card` - - @param connection The identifier for the connection to the card - ''' - blueshift_drm_update_card(connection) - - -def drm_close_card(int connection): - ''' - Close connection to the graphics card - - @param connection The identifier for the connection to the card - ''' - blueshift_drm_close_card(connection) - - -def drm_crtc_count(int connection): - ''' - Return the number of CRTC:s on the opened card - - @param connection The identifier for the connection to the card - @return :int The number of CRTC:s on the opened card - ''' - return blueshift_drm_crtc_count(connection) - - -def drm_connector_count(int connection): - ''' - Return the number of connectors on the opened card - - @param connection The identifier for the connection to the card - @return :int The number of connectors on the opened card - ''' - return blueshift_drm_connector_count(connection) - - -def drm_gamma_size(int connection, int crtc_index): - ''' - Return the size of the gamma ramps on a CRTC - - @param connection The identifier for the connection to the card - @param crtc_index The index of the CRTC - @return :int The size of the gamma ramps on a CRTC - ''' - return blueshift_drm_gamma_size(connection, crtc_index) - - -def drm_get_gamma_ramps(int connection, int crtc_index, int gamma_size, threadsafe = False): - ''' - Get the gamma ramps of the of a monitor - - @param connection The identifier for the connection to the card - @param crtc_index The index of the CRTC to read from - @param gamma_size The size a gamma ramp - @param threadsafe:bool Whether to decrease memory efficiency and performace so - multiple threads can use DRM concurrently - @return :(r:list<int>, g:list<int>, b:list<int>)? The current red, green and blue colour curves - ''' - global r_shared, g_shared, b_shared - cdef uint16_t* r - cdef uint16_t* g - cdef uint16_t* b - # If not running in thread-safe mode, - if not threadsafe: - # allocate the shared storage space for colour curves - # if not already allocated. - if r_shared is NULL: - r_shared = <uint16_t*>malloc(gamma_size * sizeof(uint16_t)) - if g_shared is NULL: - g_shared = <uint16_t*>malloc(gamma_size * sizeof(uint16_t)) - if b_shared is NULL: - b_shared = <uint16_t*>malloc(gamma_size * sizeof(uint16_t)) - # If not thread-safe use those, otherwise allocate ad-hoc ones - r = <uint16_t*>malloc(gamma_size * sizeof(uint16_t)) if threadsafe else r_shared - g = <uint16_t*>malloc(gamma_size * sizeof(uint16_t)) if threadsafe else g_shared - b = <uint16_t*>malloc(gamma_size * sizeof(uint16_t)) if threadsafe else b_shared - # Check for out-of-memory error, both for thread-safe and thread-unsafe - if (r is NULL) or (g is NULL) or (b is NULL): - raise MemoryError() - # Get current curves - rc = blueshift_drm_get_gamma_ramps(connection, crtc_index, gamma_size, r, g, b) - if rc == 0: - # If successful: - # Move the C native colour curves to Python data structures - rc_r, rc_g, rc_b = [], [], [] - for i in range(gamma_size): - rc_r.append(r[i]) - rc_g.append(g[i]) - rc_b.append(b[i]) - # Then, if running in thread-safe mode, - if threadsafe: - # deallocate the ad-hoc curve storage. - free(r) - free(g) - free(b) - return (rc_r, rc_g, rc_b) - else: - # On failure, - if threadsafe: - # deallocate the ad-hoc curve storage - # if running in thread-safe mode. - free(r) - free(g) - free(b) - return None - - -def drm_set_gamma_ramps(int connection, crtc_indices, int gamma_size, r_curve, g_curve, b_curve, threadsafe = False): - ''' - Set the gamma ramps of the of a monitor - - @param connection The identifier for the connection to the card - @param crtc_indices:list<int> The indices of the CRTC:s to control - @param gamma_size The size a gamma ramp - @param r_curve:list<int> The red gamma ramp - @param g_curve:list<int> The green gamma ramp - @param b_curve:list<int> The blue gamma ramp - @param threadsafe:bool Whether to decrease memory efficiency and performace so - multiple threads can use DRM concurrently - @return :int Zero on success - ''' - global r_shared, g_shared, b_shared - cdef uint16_t* r - cdef uint16_t* g - cdef uint16_t* b - # If not running in thread-safe mode, - if not threadsafe: - # allocate the shared storage space for colour curves - # if not already allocated. - if r_shared is NULL: - r_shared = <uint16_t*>malloc(gamma_size * sizeof(uint16_t)) - if g_shared is NULL: - g_shared = <uint16_t*>malloc(gamma_size * sizeof(uint16_t)) - if b_shared is NULL: - b_shared = <uint16_t*>malloc(gamma_size * sizeof(uint16_t)) - # If not thread-safe use those, otherwise allocate ad-hoc ones - r = <uint16_t*>malloc(gamma_size * sizeof(uint16_t)) if threadsafe else r_shared - g = <uint16_t*>malloc(gamma_size * sizeof(uint16_t)) if threadsafe else g_shared - b = <uint16_t*>malloc(gamma_size * sizeof(uint16_t)) if threadsafe else b_shared - # Check for out-of-memory error, both for thread-safe and thread-unsafe - if (r is NULL) or (g is NULL) or (b is NULL): - raise MemoryError() - # Convert the Python colour curves to C native format - for i in range(gamma_size): - r[i] = r_curve[i] & 0xFFFF - g[i] = g_curve[i] & 0xFFFF - b[i] = b_curve[i] & 0xFFFF - rc = 0 - # For each selected CRTC, - for crtc_index in crtc_indices: - # adjust the colour curves. - rc |= blueshift_drm_set_gamma_ramps(connection, crtc_index, gamma_size, r, g, b) - if threadsafe: - # deallocate the ad-hoc curve storage - # if running in thread-safe mode - free(r) - free(g) - free(b) - return rc - - -def drm_open_connector(int connection, int connector_index): - ''' - Acquire information about a connector - - @param connection The identifier for the connection to the card - @param connector_index The index of the connector - ''' - blueshift_drm_open_connector(connection, connector_index) - - -def drm_close_connector(int connection, int connector_index): - ''' - Release information about a connector - - @param connection The identifier for the connection to the card - @param connector_index The index of the connector - ''' - blueshift_drm_close_connector(connection, connector_index) - - -def drm_get_width(int connection, int connector_index): - ''' - Get the physical width the monitor connected to a connector - - @param connection The identifier for the connection to the card - @param connector_index The index of the connector - @return :int The physical width of the monitor in millimetres, 0 if unknown or not connected - ''' - return blueshift_drm_get_width(connection, connector_index) - - -def drm_get_height(int connection, int connector_index): - ''' - Get the physical height the monitor connected to a connector - - @param connection The identifier for the connection to the card - @param connector_index The index of the connector - @return :int The physical height of the monitor in millimetres, 0 if unknown or not connected - ''' - return blueshift_drm_get_height(connection, connector_index) - - -def drm_is_connected(int connection, int connector_index): - ''' - Get whether a monitor is connected to a connector - - @param connection The identifier for the connection to the card - @param connector_index The index of the connector - @return :int 1 if there is a connection, 0 otherwise, -1 if unknown - ''' - return blueshift_drm_is_connected(connection, connector_index) - - -def drm_get_crtc(int connection, int connector_index): - ''' - Get the index of the CRTC of the monitor connected to a connector - - @param connection The identifier for the connection to the card - @param connector_index The index of the connector - @return :int The index of the CRTC - ''' - return blueshift_drm_get_crtc(connection, connector_index) - - -def drm_get_connector_type_index(int connection, int connector_index): - ''' - Get the index of the type of a connector - - @param connection The identifier for the connection to the card - @param connector_index The index of the connector - @return :int The connector type by index, 0 for unknown - ''' - return blueshift_drm_get_connector_type_index(connection, connector_index) - - -def drm_get_connector_type_name(int connection, int connector_index): - ''' - Get the name of the type of a connector - - @param connection The identifier for the connection to the card - @param connector_index The index of the connector - @return :str The connector type by name, "Unknown" if not identifiable, - "Unrecognised" if Blueshift does not recognise it. - ''' - return (<bytes>blueshift_drm_get_connector_type_name(connection, connector_index)).decode('utf-8', 'replace') - - -def drm_get_edid(int connection, int connector_index): - ''' - Get the extended display identification data for the monitor connected to a connector - - @param connection The identifier for the connection to the card - @param connector_index The index of the connector - @return :str? The extended display identification data for the monitor - ''' - global edid_shared - cdef long int size - cdef long int got - cdef char* edid - cdef bytes rc - - # Prototype side of the hexadecimal representation - # of the EDID, should be exact - size = 256 - # It could be twice that, but we will not base - # our start value on something that is unlikely. - - # Allocate storage space for the EDID, with one - # extra character for NUL-termination - edid = <char*>malloc((size + 1) * sizeof(char)) - # Check for out-of-memory error - if edid is NULL: - raise MemoryError() - # Fill the storage space for the EDID, with the - # EDID of the monitor connected to the selected - # connector, in hexadecimal, representation. - got = blueshift_drm_get_edid(connection, connector_index, edid, size, 1) - - # If the length of the EDID is zero, - if got == 0: - # the free the storage space, - free(edid) - # and return that it failed. - return None - - # But if we got an non-zero length, it is of the - # EDID's byte-length, not in hexadecimal representation - # that is twice as long. - if got * 2 > size: - # In if that length is larger than we have anticipated, - # update to new size (of the hexadecimal representation), - size = got * 2 - # and reallocate the storage. - edid = <char*>realloc(edid, (size + 1) * sizeof(char)) - # Check for out-of-memory error - if edid is NULL: - raise MemoryError() - # Get the full EDID. - got = blueshift_drm_get_edid(connection, connector_index, edid, size, 1) - # Check that we got the EDID. There is an unlikely - # race condition where the user can have unplugged - # the monitor. - if got == 0: - # If we did not get an EDID, - # free the storage for it, - free(edid) - # and return that it failed. - return None - # If we got a large EDID yet, - if got * 2 > size: - # ignore it because it should happen, - # EDID:s are 128 bytes long and the risk that - # the use plugged in new monitor that did not - # have the same EDID format, is super unlikely. - # She would have to pause the program or used - # a KVM switch between the two readings. - # Instead just truncate the EDID to the size - # that we expected; it is not fatal. - got = size // 2 - - # NUL-terminate the EDID, - edid[got * 2] = 0 - # and convert it to bytes so that we can - # later convert it to a Python string, - rc = edid - # and deallocate the C string. - free(edid) - # Convert the EDID to a Sython string - return rc.decode('utf-8', 'replace') - diff --git a/src/blueshift_drm_c.c b/src/blueshift_drm_c.c deleted file mode 100644 index 5da1481..0000000 --- a/src/blueshift_drm_c.c +++ /dev/null @@ -1,575 +0,0 @@ -/** - * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -#include "blueshift_drm_c.h" - - -/** - * Mapping from card connection identifiers to card connection resources - */ -static card_connection* card_connections = NULL; - -/** - * Next card connection identifiers - */ -static size_t card_connection_ptr = 0; - -/** - * Size of the storage allocated for card connection resouces - */ -static size_t card_connection_size = 0; - -/** - * Card connection identifier reuse stack - */ -static long* card_connection_reusables = NULL; - -/** - * The head of `card_connection_reusables` - */ -static size_t card_connection_reuse_ptr = 0; - -/** - * The allocation size of `card_connection_reusables` - */ -static size_t card_connection_reuse_size = 0; - - - -/** - * Free all resources, but you need to close all connections first - */ -void blueshift_drm_close(void) -{ - if (card_connections) - free(card_connections); - - if (card_connection_reusables) - free(card_connection_reusables); - - card_connections = NULL; - card_connection_ptr = 0; - card_connection_size = 0; - card_connection_reusables = NULL; - card_connection_reuse_ptr = 0; - card_connection_reuse_size = 0; -} - - -/** - * Get the number of cards present on the system - * - * @return The number of cards present on the system - */ -int blueshift_drm_card_count(void) -{ - char pathname[PATH_MAX]; - int count = 0; - struct stat _attr; - - for (;;) - { - snprintf(pathname, PATH_MAX, DRM_DEV_NAME, DRM_DIR_NAME, count); - if (stat(pathname, &_attr)) - break; - count++; - } - - return count; -} - - -/** - * Open connection to a graphics card - * - * @param card_index The index of the graphics card - * @return -1 on failure, otherwise an identifier for the connection to the card - */ -int blueshift_drm_open_card(int card_index) -{ - char pathname[PATH_MAX]; - int fd; - int rc; - - snprintf(pathname, PATH_MAX, DRM_DEV_NAME, DRM_DIR_NAME, card_index); - - fd = open(pathname, O_RDWR | O_CLOEXEC); - if (fd < 0) - { - perror("open"); - return -1; - } - - if (card_connection_reuse_ptr) - rc = (int)*(card_connection_reusables + --card_connection_reuse_ptr); - else - { - if (card_connection_size == 0) - card_connections = malloc((card_connection_size = 8) * sizeof(card_connection)); - else if (card_connection_ptr == card_connection_size) - card_connections = realloc(card_connections, (card_connection_size <<= 1) * sizeof(card_connection)); - rc = (int)(card_connection_ptr++); - } - - (card_connections + rc)->fd = fd; - (card_connections + rc)->res = NULL; - (card_connections + rc)->connectors = NULL; - - return rc; -} - - -/** - * Update the resource, required after `blueshift_drm_open_card` - * - * @param connection The identifier for the connection to the card - */ -void blueshift_drm_update_card(int connection) -{ - card_connection* card = card_connections + connection; - - if (card->res) - drmModeFreeResources(card->res); - - card->res = drmModeGetResources(card->fd); -} - - -/** - * Close connection to the graphics card - * - * @param connection The identifier for the connection to the card - */ -void blueshift_drm_close_card(int connection) -{ - card_connection* card = card_connections + connection; - - drmModeFreeResources(card->res); - if (card->connectors) - free(card->connectors); - close(card->fd); - - if ((size_t)connection + 1 == card_connection_reuse_ptr) - card_connection_reuse_ptr--; - else - { - if (card_connection_reuse_size == 0) - card_connection_reusables = malloc((card_connection_reuse_size = 8) * sizeof(long)); - else if (card_connection_reuse_ptr == card_connection_reuse_size) - card_connection_reusables = realloc(card_connection_reusables, (card_connection_reuse_size <<= 1) * sizeof(long)); - *(card_connection_reusables + card_connection_reuse_ptr++) = connection; - } -} - - -/** - * Return the number of CRTC:s on the opened card - * - * @param connection The identifier for the connection to the card - * @return The number of CRTC:s on the opened card - */ -int blueshift_drm_crtc_count(int connection) -{ - return (card_connections + connection)->res->count_crtcs; -} - - -/** - * Return the number of connectors on the opened card - * - * @param connection The identifier for the connection to the card - * @return The number of connectors on the opened card - */ -int blueshift_drm_connector_count(int connection) -{ - return (card_connections + connection)->res->count_connectors; -} - - -/** - * Return the size of the gamma ramps on a CRTC - * - * @param connection The identifier for the connection to the card - * @param crtc_index The index of the CRTC - * @return The size of the gamma ramps on a CRTC - */ -int blueshift_drm_gamma_size(int connection, int crtc_index) -{ - card_connection* card = card_connections + connection; - drmModeCrtc* crtc = drmModeGetCrtc(card->fd, *(card->res->crtcs + crtc_index)); - int gamma_size = crtc->gamma_size; - - drmModeFreeCrtc(crtc); - return gamma_size; -} - - -/** - * Get the current gamma ramps of a monitor - * - * @param connection The identifier for the connection to the card - * @param crtc_index The index of the CRTC to read from - * @param gamma_size The size a gamma ramp - * @param red Storage location for the red gamma ramp - * @param green Storage location for the green gamma ramp - * @param blue Storage location for the blue gamma ramp - * @return Zero on success - */ -int blueshift_drm_get_gamma_ramps(int connection, int crtc_index, int gamma_size, uint16_t* red, uint16_t* green, uint16_t* blue) -{ - card_connection* card = card_connections + connection; - - /* We need to initialise it to avoid valgrind warnings */ - memset(red, 0, (size_t)gamma_size * sizeof(uint16_t)); - memset(green, 0, (size_t)gamma_size * sizeof(uint16_t)); - memset(blue, 0, (size_t)gamma_size * sizeof(uint16_t)); - - return drmModeCrtcGetGamma(card->fd, *(card->res->crtcs + crtc_index), (uint32_t)gamma_size, red, green, blue); -} - - -/** - * Set the gamma ramps of the of a monitor - * - * @param connection The identifier for the connection to the card - * @param crtc_index The index of the CRTC to read from - * @param gamma_size The size a gamma ramp - * @param red The red gamma ramp - * @param green The green gamma ramp - * @param blue The blue gamma ramp - * @return Zero on success - */ -int blueshift_drm_set_gamma_ramps(int connection, int crtc_index, int gamma_size, uint16_t* red, uint16_t* green, uint16_t* blue) -{ - card_connection* card = card_connections + connection; - - /* Fails if inside a graphical environment */ - return drmModeCrtcSetGamma(card->fd, *(card->res->crtcs + crtc_index), (uint32_t)gamma_size, red, green, blue); -} - - -/** - * Acquire information about a connector - * - * @param connection The identifier for the connection to the card - * @param connector_index The index of the connector - */ -void blueshift_drm_open_connector(int connection, int connector_index) -{ - card_connection* card = card_connections + connection; - - if (card->connectors == NULL) - card->connectors = malloc((size_t)(card->res->count_connectors) * sizeof(drmModeConnector*)); - *(card->connectors + connector_index) = drmModeGetConnector(card->fd, *(card->res->connectors + connector_index)); -} - - -/** - * Release information about a connector - * - * @param connection The identifier for the connection to the card - * @param connector_index The index of the connector - */ -void blueshift_drm_close_connector(int connection, int connector_index) -{ - drmModeFreeConnector(*((card_connections + connection)->connectors + connector_index)); -} - - -/** - * Get the physical width the monitor connected to a connector - * - * @param connection The identifier for the connection to the card - * @param connector_index The index of the connector - * @return The physical width of the monitor in millimetres, 0 if unknown or not connected - */ -int blueshift_drm_get_width(int connection, int connector_index) -{ - /* Accurate dimension on area not covered by the edges */ - return (int)((card_connections + connection)->connectors[connector_index]->mmWidth); -} - - -/** - * Get the physical height the monitor connected to a connector - * - * @param connection The identifier for the connection to the card - * @param connector_index The index of the connector - * @return The physical height of the monitor in millimetres, 0 if unknown or not connected - */ -int blueshift_drm_get_height(int connection, int connector_index) -{ - /* Accurate dimension on area not covered by the edges */ - return (int)((card_connections + connection)->connectors[connector_index]->mmHeight); -} - - -/** - * Get whether a monitor is connected to a connector - * - * @param connection The identifier for the connection to the card - * @param connector_index The index of the connector - * @return 1 if there is a connection, 0 otherwise, -1 if unknown - */ -int blueshift_drm_is_connected(int connection, int connector_index) -{ - switch ((card_connections + connection)->connectors[connector_index]->connection) - { - case DRM_MODE_CONNECTED: - return 1; - case DRM_MODE_DISCONNECTED: - return 0; - case DRM_MODE_UNKNOWNCONNECTION: - default: - return -1; - } -} - - -/** - * Get the index of the CRTC of the monitor connected to a connector - * - * @param connection The identifier for the connection to the card - * @param connector_index The index of the connector - * @return The index of the CRTC - */ -int blueshift_drm_get_crtc(int connection, int connector_index) -{ - card_connection* card = card_connections + connection; - drmModeEncoder* encoder = drmModeGetEncoder(card->fd, card->connectors[connector_index]->encoder_id); - uint32_t crtc_id = encoder->crtc_id; - drmModeRes* res = card->res; - int crtc; - int n; - - drmModeFreeEncoder(encoder); - - n = res->count_crtcs; - for (crtc = 0; crtc < n; crtc++) - if (*(res->crtcs + crtc) == crtc_id) - return crtc; - - return -1; -} - - -/** - * Get the index of the type of a connector - * - * @param connection The identifier for the connection to the card - * @param connector_index The index of the connector - * @return The connector type by index, 0 for unknown - */ -int blueshift_drm_get_connector_type_index(int connection, int connector_index) -{ - return (int)((card_connections + connection)->connectors[connector_index]->connector_type); -} - - -/** - * Get the name of the type of a connector - * - * @param connection The identifier for the connection to the card - * @param connector_index The index of the connector - * @return The connector type by name, "Unknown" if not identifiable, - * "Unrecognised" if Blueshift does not recognise it. - */ -const char* blueshift_drm_get_connector_type_name(int connection, int connector_index) -{ - static const char* TYPE_NAMES[] = { - "Unknown", "VGA", "DVII", "DVID", "DVIA", "Composite", "SVIDEO", "LVDS", "Component", - "9PinDIN", "DisplayPort", "HDMIA", "HDMIB", "TV", "eDP", "VIRTUAL", "DSI"}; - - uint32_t type = ((card_connections + connection)->connectors[connector_index])->connector_type; - return (size_t)type < sizeof(TYPE_NAMES) / sizeof(char*) ? TYPE_NAMES[type] : "Unrecognised"; -} - - -/** - * Get the extended display identification data for the monitor connected to a connector - * - * @param connection The identifier for the connection to the card - * @param connector_index The index of the connector - * @param edid Storage location for the EDID, it should be 128 bytes, - * or 256 bytes if you are counting on the depricated EDID 2.0, - * If hexadecimal, twice that + zero termiation. - * @param size The size allocated to `edid` excluding your zero termination - * @param hexadecimal Whether to convert to hexadecimal representation, this is preferable - * @return The length of the found value, 0 if none, as if hex is false - */ -long blueshift_drm_get_edid(int connection, int connector_index, char* edid, long size, int hexadecimal) -{ - card_connection* card = card_connections + connection; - drmModeConnector* connector = *(card->connectors + connector_index); - int fd = card->fd; - long rc = 0; - int prop_n = connector->count_props; - int prop_i; - - for (prop_i = 0; prop_i < prop_n; prop_i++) - { - drmModePropertyRes* prop = drmModeGetProperty(fd, connector->props[prop_i]); - if (!strcmp("EDID", prop->name)) - { - drmModePropertyBlobRes* blob = drmModeGetPropertyBlob(fd, (uint32_t)(connector->prop_values[prop_i])); - if (hexadecimal) - { - uint32_t n = (uint32_t)size / 2; - uint32_t i; - rc += blob->length; - if (n > blob->length) - n = blob->length; - for (i = 0; i < n ; i++) - { - *(edid + i * 2 + 0) = "0123456789abcdef"[(*((char*)(blob->data) + i) >> 4) & 15]; - *(edid + i * 2 + 1) = "0123456789abcdef"[(*((char*)(blob->data) + i) >> 0) & 15]; - } - } - else - { - uint32_t len = blob->length < size ? blob->length : (uint32_t)size; - memcpy(edid, blob->data, (size_t)len * sizeof(char)); - } - drmModeFreePropertyBlob(blob); - prop_i = prop_n; /* stop the for-loop */ - } - drmModeFreeProperty(prop); - } - - return rc; -} - - -/* -int main(int argc, char** argv) -{ - int card_n = blueshift_drm_card_count(); - int* cards = alloca(card_n * sizeof(int*)); - int card_i; - - (void) argc; - (void) argv; - - printf("Card count: %i\n", card_n); - for (card_i = 0; card_i < card_n; card_i++) - { - *(cards + card_i) = blueshift_drm_open_card(card_i); - blueshift_drm_update_card(*(cards + card_i)); - } - - for (card_i = 0; card_i < card_n; card_i++) - { - int card = *(cards + card_i); - int connector_n; - int connector_i; - - printf("Card: %i\n", card_i); - - connector_n = blueshift_drm_connector_count(card); - - printf(" CRTC count: %i\n", blueshift_drm_crtc_count(card)); - printf(" Connector count: %i\n", connector_n); - - for (connector_i = 0; connector_i < connector_n; connector_i++) - { - blueshift_drm_open_connector(card, connector_i); - - printf(" Connector: %i\n", connector_i); - printf(" Connected: %i\n", blueshift_drm_is_connected(card, connector_i)); - printf(" Connector type: %s (%i)\n", - blueshift_drm_get_connector_type_name(card, connector_i), - blueshift_drm_get_connector_type_index(card, connector_i)); - - if (blueshift_drm_is_connected(card, connector_i) == 1) - { - long size = 128; - char* edid; - long n; - int crtc; - - printf(" Physical size: %i mm by %i mm\n", - blueshift_drm_get_width(card, connector_i), - blueshift_drm_get_height(card, connector_i)); - - edid = malloc((size * 2 + 1) * sizeof(char)); - if ((n = blueshift_drm_get_edid(card, connector_i, edid, size, 1))) - { - if (n > size) - { - size = n; - edid = realloc(edid, (size * 2 + 1) * sizeof(char)); - blueshift_drm_get_edid(card, connector_i, edid, size, 1); - } - *(edid + n * 2) = 0; - printf(" EDID: %s\n", edid); - } - free(edid); - - if ((crtc = blueshift_drm_get_crtc(card, connector_i)) >= 0) - { - int gamma_size = blueshift_drm_gamma_size(card, crtc); - uint16_t* red = alloca(3 * gamma_size * sizeof(uint16_t)); - uint16_t* green = red + gamma_size; - uint16_t* blue = green + gamma_size; - - printf(" CRTC: %i\n", crtc); - printf(" Gamma size: %i\n", gamma_size); - - if (!blueshift_drm_get_gamma_ramps(card, crtc, gamma_size, red, green, blue)) - { - int i; - printf(" Red:"); - for (i = 0; i < gamma_size; i++) - printf(" %u", *(red + i)); - printf("\n Green:"); - for (i = 0; i < gamma_size; i++) - printf(" %u", *(green + i)); - printf("\n Blue:"); - for (i = 0; i < gamma_size; i++) - printf(" %u", *(blue + i)); - printf("\n"); - - for (i = 0; i < gamma_size; i++) - *(red + i) /= 2; - for (i = 0; i < gamma_size; i++) - *(green + i) /= 2; - for (i = 0; i < gamma_size; i++) - *(blue + i) /= 2; - - blueshift_drm_set_gamma_ramps(card, crtc, gamma_size, red, green, blue); - } - } - } - } - } - - for (card_i = 0; card_i < card_n; card_i++) - { - int card = *(cards + card_i); - int connector_n = blueshift_drm_connector_count(card); - int connector_i; - - for (connector_i = 0; connector_i < connector_n; connector_i++) - blueshift_drm_close_connector(card, connector_i); - - blueshift_drm_close_card(card); - } - - blueshift_drm_close(); - return 0; -} -*/ - diff --git a/src/blueshift_drm_c.h b/src/blueshift_drm_c.h deleted file mode 100644 index 36913cc..0000000 --- a/src/blueshift_drm_c.h +++ /dev/null @@ -1,239 +0,0 @@ -/** - * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -#ifndef BLUESHIFT_DRM_C_H -#define BLUESHIFT_DRM_C_H - - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include <unistd.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <fcntl.h> -#include <alloca.h> -#include <limits.h> - -#ifndef O_CLOEXEC -# define O_CLOEXEC 02000000 -#endif -#ifndef PATH_MAX -# define PATH_MAX 4096 -#endif - -/* Requires video group */ -#include <xf86drm.h> -#include <xf86drmMode.h> - - - -/** - * Resources for an open connection to a graphics card - */ -typedef struct _card_connection -{ - /** - * File descriptor for the connection - */ - int fd; - - /** - * Card resources - */ - drmModeRes* res; - - /** - * Resources for open connectors - */ - drmModeConnector** connectors; - -} card_connection; - - - -/** - * Free all resources, but you need to close all connections first - */ -void blueshift_drm_close(void); - -/** - * Get the number of cards present on the system - * - * @return The number of cards present on the system - */ -int blueshift_drm_card_count(void); - -/** - * Open connection to a graphics card - * - * @param card_index The index of the graphics card - * @return -1 on failure, otherwise an identifier for the connection to the card - */ -int blueshift_drm_open_card(int card_index); - -/** - * Update the resource, required after `blueshift_drm_open_card` - * - * @param connection The identifier for the connection to the card - */ -void blueshift_drm_update_card(int connection); - -/** - * Close connection to the graphics card - * - * @param connection The identifier for the connection to the card - */ -void blueshift_drm_close_card(int connection); - -/** - * Return the number of CRTC:s on the opened card - * - * @param connection The identifier for the connection to the card - * @return The number of CRTC:s on the opened card - */ -int blueshift_drm_crtc_count(int connection) __attribute__((pure)); - -/** - * Return the number of connectors on the opened card - * - * @param connection The identifier for the connection to the card - * @return The number of connectors on the opened card - */ -int blueshift_drm_connector_count(int connection) __attribute__((pure)); - -/** - * Return the size of the gamma ramps on a CRTC - * - * @param connection The identifier for the connection to the card - * @param crtc_index The index of the CRTC - * @return The size of the gamma ramps on a CRTC - */ -int blueshift_drm_gamma_size(int connection, int crtc_index); - -/** - * Get the current gamma ramps of a monitor - * - * @param connection The identifier for the connection to the card - * @param crtc_index The index of the CRTC to read from - * @param gamma_size The size a gamma ramp - * @param red Storage location for the red gamma ramp - * @param green Storage location for the green gamma ramp - * @param blue Storage location for the blue gamma ramp - * @return Zero on success - */ -int blueshift_drm_get_gamma_ramps(int connection, int crtc_index, int gamma_size, uint16_t* red, uint16_t* green, uint16_t* blue); - -/** - * Set the gamma ramps of the of a monitor - * - * @param connection The identifier for the connection to the card - * @param crtc_index The index of the CRTC to read from - * @param gamma_size The size a gamma ramp - * @param red The red gamma ramp - * @param green The green gamma ramp - * @param blue The blue gamma ramp - * @return Zero on success - */ -int blueshift_drm_set_gamma_ramps(int connection, int crtc_index, int gamma_size, uint16_t* red, uint16_t* green, uint16_t* blue); - -/** - * Acquire information about a connector - * - * @param connection The identifier for the connection to the card - * @param connector_index The index of the connector - */ -void blueshift_drm_open_connector(int connection, int connector_index); - -/** - * Release information about a connector - * - * @param connection The identifier for the connection to the card - * @param connector_index The index of the connector - */ -void blueshift_drm_close_connector(int connection, int connector_index); - -/** - * Get the physical width the monitor connected to a connector - * - * @param connection The identifier for the connection to the card - * @param connector_index The index of the connector - * @return The physical width of the monitor in millimetres, 0 if unknown or not connected - */ -int blueshift_drm_get_width(int connection, int connector_index) __attribute__((pure)); - -/** - * Get the physical height the monitor connected to a connector - * - * @param connection The identifier for the connection to the card - * @param connector_index The index of the connector - * @return The physical height of the monitor in millimetres, 0 if unknown or not connected - */ -int blueshift_drm_get_height(int connection, int connector_index) __attribute__((pure)); - -/** - * Get whether a monitor is connected to a connector - * - * @param connection The identifier for the connection to the card - * @param connector_index The index of the connector - * @return 1 if there is a connection, 0 otherwise, -1 if unknown - */ -int blueshift_drm_is_connected(int connection, int connector_index) __attribute__((pure)); - -/** - * Get the index of the CRTC of the monitor connected to a connector - * - * @param connection The identifier for the connection to the card - * @param connector_index The index of the connector - * @return The index of the CRTC - */ -int blueshift_drm_get_crtc(int connection, int connector_index); - -/** - * Get the index of the type of a connector - * - * @param connection The identifier for the connection to the card - * @param connector_index The index of the connector - * @return The connector type by index, 0 for unknown - */ -int blueshift_drm_get_connector_type_index(int connection, int connector_index) __attribute__((pure)); - -/** - * Get the name of the type of a connector - * - * @param connection The identifier for the connection to the card - * @param connector_index The index of the connector - * @return The connector type by name, "Unknown" if not identifiable, - * "Unrecognised" if Blueshift does not recognise it. - */ -const char* blueshift_drm_get_connector_type_name(int connection, int connector_index) __attribute__((pure)); - -/** - * Get the extended display identification data for the monitor connected to a connector - * - * @param connection The identifier for the connection to the card - * @param connector_index The index of the connector - * @param edid Storage location for the EDID, it should be 128 bytes, 256 bytes + zero termination if hex - * @param size The size allocated to `edid` excluding your zero termination - * @param hexadecimal Whether to convert to hexadecimal representation, this is preferable - * @return The length of the found value, 0 if none, as if hex is false - */ -long blueshift_drm_get_edid(int connection, int connector_index, char* edid, long size, int hexadecimal); - - - -#endif - diff --git a/src/blueshift_idcrtc.c b/src/blueshift_idcrtc.c deleted file mode 100644 index 8e41c11..0000000 --- a/src/blueshift_idcrtc.c +++ /dev/null @@ -1,406 +0,0 @@ -/** - * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include <inttypes.h> -#include <alloca.h> - -#include <xcb/xcb.h> -#include <xcb/randr.h> - - - -/** - * The major version of RANDR the program expects - */ -#define RANDR_VERSION_MAJOR 1 - -/** - * The minor version of RANDR the program expects - */ -#define RANDR_VERSION_MINOR 3 - - - -/** - * Connection to the X server - */ -static xcb_connection_t* connection; - -/** - * Used to store errors in - */ -static xcb_generic_error_t* error; - - - -/** - * Main entry point of the program - * - * @param argc Length of `argv` - * @param argv Command line arguments - * @return Zero on success - */ - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Wstack-protector" -int main(int argc, char** argv) -{ - #pragma GCC diagnostic pop - char* display = NULL; - xcb_randr_query_version_cookie_t version_cookie; - xcb_randr_query_version_reply_t* randr_version; - xcb_screen_iterator_t iter; - int screen_count; - int screen_i; - int i; - - - /* Get X connection */ - - /* This acquires a connection to the - X display indicated by the DISPLAY - environ variable, or as indicated - by the first command line argument - if existent. */ - if (argc > 1) - display = *(argv + 1); - connection = xcb_connect(display, NULL); - - - /* Check RandR protocol version */ - - /* Check that the version of RandR is supported. */ - version_cookie = xcb_randr_query_version(connection, RANDR_VERSION_MAJOR, RANDR_VERSION_MINOR); - randr_version = xcb_randr_query_version_reply(connection, version_cookie, &error); - - if (error || (randr_version == NULL)) - { - /* If not possible, print an error message and - close the connection to the display. */ - fprintf(stderr, "RandR version query returned %i\n", error ? error->error_code : -1); - xcb_disconnect(connection); - return 1; - } - - if (randr_version->major_version != RANDR_VERSION_MAJOR || randr_version->minor_version < RANDR_VERSION_MINOR) - { - /* If the version did not match, print a warning, */ - fprintf(stderr, "Unsupported RandR version, got %u.%u, expected %u.%u\n", - randr_version->major_version, randr_version->minor_version, - RANDR_VERSION_MAJOR, RANDR_VERSION_MINOR); - /* free the version information resources, */ - free(randr_version); - /* and close the connection to the display. */ - xcb_disconnect(connection); - return 1; - } - - /* Free the version information resources. */ - free(randr_version); - - - /* Get screen information */ - - /* Acquire a list of all screens in the display, */ - iter = xcb_setup_roots_iterator(xcb_get_setup(connection)); - /* count the list. */ - screen_count = iter.rem; - - /* Print the number available screens. */ - printf("Screen count: %i\n", screen_count); - for (screen_i = 0; screen_i < screen_count; screen_i++) - { - /* For each screen */ - xcb_screen_t* screen = iter.data; - xcb_randr_get_screen_resources_current_cookie_t res_cookie; - xcb_randr_get_screen_resources_current_reply_t* res_reply; - xcb_randr_output_t* outputs; - xcb_randr_crtc_t* crtcs; - int output_i; - - /* We have acquired the screen, go to next in preperation for next iteration. */ - xcb_screen_next(&iter); - - /* Print the screen index. */ - printf("Screen: %i\n", screen_i); - - /* Acquire information about the screen. */ - res_cookie = xcb_randr_get_screen_resources_current(connection, screen->root); - res_reply = xcb_randr_get_screen_resources_current_reply(connection, res_cookie, &error); - - if (error) - { - /* On error print an error message and close the connection to the display. */ - fprintf(stderr, "RandR screen resource query returned %i\n", error->error_code); - xcb_disconnect(connection); - return 1; - } - - /* Print the CRTC count, */ - printf(" CRTC count: %i\n", res_reply->num_crtcs); - /* and the output count. */ - printf(" Output count: %i\n", res_reply->num_outputs); - /* There are as many outputs as there are - connectors, that is, the number of - monitors that you could potentially - plugg into the graphics cards that - are associated with the screen, even - if that many are not supported. But - there are only as many CRTC:s as - there are monitors actually plugged - in and supported. */ - - - /* Get output information */ - - /* Extract output list. */ - outputs = xcb_randr_get_screen_resources_current_outputs(res_reply); - /* Extract CRTC list. */ - crtcs = xcb_randr_get_screen_resources_current_crtcs(res_reply); - /* For each output */ - for (output_i = 0; output_i < res_reply->num_outputs; output_i++) - { - xcb_randr_get_output_info_cookie_t out_cookie; - xcb_randr_get_output_info_reply_t* out_reply; - uint8_t* name; - int name_len; - - /* Acquire information about the output. */ - out_cookie = xcb_randr_get_output_info(connection, outputs[output_i], res_reply->config_timestamp); - out_reply = xcb_randr_get_output_info_reply(connection, out_cookie, &error); - - if (error) - { - /* On error print an error message, release the screen resources, - and close the connection to the display. */ - fprintf(stderr, "RandR output query returned %i\n", error->error_code); - free(res_reply); - xcb_disconnect(connection); - return 1; - } - - /* Print the index, in the scope of the screen, of the output. */ - printf(" Output: %i\n", output_i); - - /* We use indention not for computer-readability, but - exclusively for human-readability, especially for - debugging. */ - - /* Extract the output name from the data structure that holds it. */ - name = xcb_randr_get_output_info_name(out_reply); - /* As well as the length of the name; it is not NUL-termianted. */ - name_len = out_reply->name_len; - - /* Print the output name, we specified length, so that - printf does not attempt to read outside the name, - as it is not NUL-terminated. */ - printf(" Name: %.*s\n", name_len, name); - - /* Check connection status */ - switch (out_reply->connection) - { - case XCB_RANDR_CONNECTION_CONNECTED: - /* If connectioned, do stuff! */ - { - xcb_randr_list_output_properties_cookie_t prop_cookie; - xcb_randr_list_output_properties_reply_t* prop_reply; - xcb_atom_t* atoms; - xcb_atom_t* atoms_end; - int crtc_i; - - /* Print that the connector is used. */ - printf(" Connection: connected\n"); - /* And print what dimensions RandR thinks the monitor has, - physically in millimeters, however common that it is - extremely wrong as it does not read it from the extended - display identication data, but tries to calculate it. */ - printf(" Size: %i %i\n", out_reply->mm_width, out_reply->mm_height); - - /* Iterate over all CRTC:s, */ - for (crtc_i = 0; crtc_i < res_reply->num_crtcs; crtc_i++) - /* and look for a CRTC with the same ID, */ - if (crtcs[crtc_i] == out_reply->crtc) - { - /* and print that CRTC:s index. */ - printf(" CRTC: %i\n", crtc_i); - break; - } - - /* Acquire a list of all properties of the output. */ - prop_cookie = xcb_randr_list_output_properties(connection, outputs[output_i]); - prop_reply = xcb_randr_list_output_properties_reply(connection, prop_cookie, &error); - - if (error) - { - /* On error print an error message, */ - fprintf(stderr, "RandR output property query returned %i\n", error->error_code); - /* and free that output and screen information resources, */ - free(out_reply); - free(res_reply); - /* and then close the connection to the display. */ - xcb_disconnect(connection); - return 1; - } - - - /* Get output atoms */ - - /* 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); - - /* For each property */ - for (; atoms != atoms_end; atoms++) - { - xcb_get_atom_name_cookie_t atom_name_cookie; - xcb_get_atom_name_reply_t* atom_name_reply; - char* atom_name; - char* atom_name_; - int atom_name_len; - - /* 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) - { - /* On error print an error message, */ - fprintf(stderr, "RandR atom name query returned %i\n", error->error_code); - /* and release the property list, */ - free(prop_reply); - /* the output information resources */ - free(out_reply); - /* and the screen information resources. */ - free(res_reply); - /* And then close the connection to the display. */ - xcb_disconnect(connection); - return 1; - } - - /* 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); - - /* NUL-terminate the atom name. */ - atom_name = alloca(((size_t)atom_name_len + 1U) * sizeof(char)); - memcpy(atom_name, atom_name_, (size_t)atom_name_len * sizeof(char)); - *(atom_name + atom_name_len) = 0; - /* (It is allocated on the stack, so it should not be free:d.) */ - - - /* Get output identifier */ - - /* Look for the the property named EDID. */ - if (!strcmp(atom_name, "EDID")) - { - xcb_randr_get_output_property_cookie_t atom_cookie; - xcb_randr_get_output_property_reply_t* atom_reply; - int length; - unsigned char* atom_data_; - char* atom_data; - - /* Acquire the property's value, we know that it is either 128 or 256 byte long. */ - atom_cookie = xcb_randr_get_output_property(connection, outputs[output_i], *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) - { - /* On error print an error message, */ - fprintf(stderr, "RandR atom data query returned %i\n", error->error_code); - /* and release the property name, */ - free(atom_name_reply); - /* release the property list, */ - free(prop_reply); - /* the output information resources */ - free(out_reply); - /* and the screen information resources. */ - free(res_reply); - /* And then close the connection to the display. */ - xcb_disconnect(connection); - return 1; - } - - /* 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); - - /* Convert to hexadecimal representation. */ - atom_data = alloca((2U * (size_t)length + 1U) * sizeof(char)); - for (i = 0; i < length; i++) - { - *(atom_data + i * 2 + 0) = "0123456789abcdef"[(*(atom_data_ + i) >> 4) & 15]; - *(atom_data + i * 2 + 1) = "0123456789abcdef"[(*(atom_data_ + i) >> 0) & 15]; - } - /* NUL-terminate. */ - *(atom_data + 2 * length) = 0; - - /* Print the property's name and values. */ - printf(" %s: %s\n", atom_name, atom_data); - - /* Free the proprty value. */ - free(atom_reply); - } - - /* Free the name of the atom. */ - free(atom_name_reply); - } - - /* Free the list of properties. */ - free(prop_reply); - } - break; - - case XCB_RANDR_CONNECTION_DISCONNECTED: - /* If disconnected, print that and continue to next output. */ - printf(" Connection: disconnected\n"); - break; - - case XCB_RANDR_CONNECTION_UNKNOWN: - default: - /* If the connection status is unkown, print that and continue to next output. */ - printf(" Connection: unknown\n"); - break; - } - - /* Free the output information resources. */ - free(out_reply); - } - - /* Free the screen information resources. */ - free(res_reply); - } - - - /* Free resources **/ - - /* Close the connection to the display.*/ - xcb_disconnect(connection); - return 0; -} - diff --git a/src/blueshift_quartz.pyx b/src/blueshift_quartz.pyx deleted file mode 100644 index 8b8da88..0000000 --- a/src/blueshift_quartz.pyx +++ /dev/null @@ -1,181 +0,0 @@ -# -*- python -*- - -# Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - -cimport cython -from libc.stdlib cimport malloc, free -from libc.stdint cimport * - - -cdef extern int blueshift_quartz_open() -''' -Start stage of colour curve control - -@return Zero on success -''' - -cdef extern int blueshift_quartz_crtc_count() -''' -Get the number of CRTC:s on the system - -@return The number of CRTC:s on the system -''' - -cdef extern uint16_t* blueshift_quartz_read(int use_crtc) -''' -Gets the current colour curves - -@param use_crtc The CRTC to use -@return {the size of the each curve, *the red curve, - *the green curve, *the blue curve}, - needs to be free:d. `NULL` on error. -''' - -cdef extern int blueshift_quartz_apply(int use_crtc, float* r_curves, float* g_curves, float* b_curves) -''' -Apply stage of colour curve control - -@param use_crtc The CRTC to use, -1 for all -@param r_curve The red colour curve -@param g_curve The green colour curve -@param b_curve The blue colour curve -@return Zero on success -''' - -cdef extern void blueshift_quartz_close() -''' -Resource freeing stage of colour curve control -''' - -cdef extern void blueshift_quartz_restore() -''' -Restore all gamma curves (on each and every CRTC on the system) -to the settings on ColorSync -''' - - - -cdef float* r_c -''' -Storage space for the red colour curve in C native data structure -''' - -cdef float* g_c -''' -Storage space for the green colour curve in C native data structure -''' - -cdef float* b_c -''' -Storage space for the blue colour curve in C native data structure -''' - - - -def quartz_open(): - ''' - Start stage of colour curve control - - @return :int Zero on success - ''' - global r_c, g_c, b_c - # Allocate the storage space for the C native colour curves - r_c = <float*>malloc(256 * sizeof(float)) - g_c = <float*>malloc(256 * sizeof(float)) - b_c = <float*>malloc(256 * sizeof(float)) - # Check for out-of-memory error - if (r_c is NULL) or (g_c is NULL) or (b_c is NULL): - raise MemoryError() - # Start using Quartz - return blueshift_quartz_open() - - -def quartz_crtc_count(): - ''' - Get the number of CRTC:s on the system - - @return :int The number of CRTC:s on the system - ''' - return blueshift_quartz_crtc_count() - - -def quartz_read(int use_crtc): - ''' - Gets the current colour curves - - @param use_crtc The CRTC to use - @return :(r:list<int>, g:list<int>, b:list<int>) The current red, green and blue colour curves - ''' - cdef uint16_t* got - # Read the current curves - got = blueshift_quartz_read(use_crtc) - if got is NULL: - raise Exception() - # Convert to Python integer lists - r, g, b, i = [], [], [], 1 - s = got[0] - for c in (r, g, b): - # while extracting the sizes of the curves - for j in range(s): - c.append(got[i + j]) - i += s - # Free the native curves - free(got) - return (r, g, b) - - -def quartz_apply(crtc_indices, r_curve, g_curve, b_curve): - ''' - Apply stage of colour curve control - - @param crtc_indices:list<int> The indices of the CRTC:s to control, -1 for all - @param r_curve:list<float> The red colour curve - @param g_curve:list<float> The green colour curve - @param b_curve:list<float> The blue colour curve - @return :int Zero on success - ''' - # Convert curves to C floats - for i in range(256): - r_c[i] = r_curve[i] - g_c[i] = g_curve[i] - b_c[i] = b_curve[i] - rc = 0 - # For each selected CRTC, - for crtc_index in crtc_indices: - # apply curves. - rc |= blueshift_quartz_apply(crtc_index, r_c, g_c, b_c) - return rc - - -def quartz_close(): - ''' - Resource freeing stage of colour curve control - ''' - # Free the storage space for the colour curves - free(r_c) - free(g_c) - free(b_c) - # Close free all resources in the native code - blueshift_quartz_close() - - -def quartz_restore(): - ''' - Restore all gamma curves (on each and every CRTC on the system) - to the settings on ColorSync - ''' - blueshift_quartz_restore() - diff --git a/src/blueshift_quartz_c.c b/src/blueshift_quartz_c.c deleted file mode 100644 index 94d1291..0000000 --- a/src/blueshift_quartz_c.c +++ /dev/null @@ -1,255 +0,0 @@ -/** - * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -#include "blueshift_quartz_c.h" - - - -/** - * The number of CRTC:s on the system - */ -static uint32_t crtc_count = 0; - -/** - * The CRTC:s on the system - */ -static CGDirectDisplayID* crtcs = NULL; - -/** - */ -static uint32_t* gamma_sizes = NULL; - - - -/** - * Start stage of colour curve control - * - * @return Zero on success - */ -int blueshift_quartz_open(void) -{ - uint32_t cap = 4; - uint32_t i; - CGError r; - - crtcs = malloc((size_t)cap * sizeof(CGDirectDisplayID)); - if (crtcs == NULL) - { - perror("malloc"); - return -1; - } - - for (;;) - { - r = CGGetOnlineDisplayList(cap, crtcs, &crtc_count); - if (r != kCGErrorSuccess) - { - free(crtcs); - crtcs = NULL; - close_fake_quartz(); - } - if (crtc_count == cap) - { - cap <<= 1; - if (cap == 0) /* We could also test ~0, but it is still too many. */ - { - fprintf(stderr, "An impossible number of CRTC:s are available according to Quartz\n"); - free(crtcs); - close_fake_quartz(); - return -1; - } - crtcs = realloc(crtcs, (size_t)cap * sizeof(CGDirectDisplayID)); - if (crtcs == NULL) - { - perror("realloc"); - close_fake_quartz(); - return -1; - } - } - else - break; - } - - if (crtc_count > 0) - { - gamma_sizes = malloc((size_t)crtc_count * sizeof(uint32_t)); - if (gamma_sizes == NULL) - { - perror("malloc"); - free(crtcs); - close_fake_quartz(); - return -1; - } - for (i = 0; i < crtc_count; i++) - { - gamma_sizes[i] = CGDisplayGammaTableCapacity(crtcs[i]); - if (gamma_sizes[i] < 2) - { - fprintf(stderr, "Quartz reported impossibly small gamma ramps.\n"); - free(gamma_sizes); - free(crtcs); - close_fake_quartz(); - return -1; - } - } - } - - return 0; -} - - -/** - * Get the number of CRTC:s on the system - * - * @return The number of CRTC:s on the system - */ -int blueshift_quartz_crtc_count(void) -{ - return (int)crtc_count; -} - - -/** - * Gets the current colour curves - * - * @param use_crtc The CRTC to use - * @return {the size of the each curve, *the red curve, - * *the green curve, *the blue curve}, - * needs to be free:d. `NULL` on error. - */ -uint16_t* blueshift_quartz_read(int use_crtc) -{ - if ((use_crtc < 0) || (use_crtc >= (int)crtc_count)) - { - fprintf(stderr, "CRTC %i does not exist\n", use_crtc); - return NULL; - } - else - { - uint32_t gamma_size = gamma_sizes[use_crtc]; - uint16_t* rc = malloc((1 + 3 * (size_t)(gamma_size)) * sizeof(uint16_t)); - uint32_t i; - CGGammaValue* red; - CGGammaValue* green; - CGGammaValue* blue; - CGError r; - uint32_t _; - - if (rc == NULL) - { - perror("malloc"); - return NULL; - } - - red = malloc((3 * (size_t)gamma_size) * sizeof(CGGammaValue)); - green = red + (size_t)gamma_size; - blue = green + (size_t)gamma_size; - - if (red == NULL) - { - perror("malloc"); - free(rc); - return NULL; - } - - r = CGGetDisplayTransferByTable(crtcs[use_crtc], gamma_size, red, green, blue, &_); - if (r != kCGErrorSuccess) - { - fprintf(stderr, "Failed to get gamma ramps for CRTC %i\n", use_crtc); - free(red); - free(rc); - return 0; - } - - *rc++ = gamma_sizes[use_crtc]; - for (i = 0; i < gamma_size; i++) - { - int32_t v = red[i] * UINT16_MAX; - rc[i] = (uint16_t)(v < 0 ? 0 : v > UINT16_MAX ? UINT16_MAX : v); - } - rc += gamma_size; - for (i = 0; i < gamma_size; i++) - { - int32_t v = green[i] * UINT16_MAX; - rc[i] = (uint16_t)(v < 0 ? 0 : v > UINT16_MAX ? UINT16_MAX : v); - } - rc += gamma_size; - for (i = 0; i < gamma_size; i++) - { - int32_t v = blue[i] * UINT16_MAX; - rc[i] = (uint16_t)(v < 0 ? 0 : v > UINT16_MAX ? UINT16_MAX : v); - } - - return rc - (1 + 2 * gamma_size); - } -} - - -/** - * Apply stage of colour curve control - * - * @param use_crtc The CRTC to use, -1 for all - * @param r_curve The red colour curve - * @param g_curve The green colour curve - * @param b_curve The blue colour curve - * @return Zero on success - */ -int blueshift_quartz_apply(int use_crtc, float* r_curves, float* g_curves, float* b_curves) -{ - if (use_crtc < (int)crtc_count) - { - int c = use_crtc < 0 ? 0 : use_crtc; - int n = use_crtc < 0 ? (int)crtc_count : (use_crtc + 1); - CGError r = kCGErrorSuccess; - - for (; c < n; c++) - { - r = CGSetDisplayTransferByTable(crtcs[c], gamma_sizes[c], r_curves, g_curves, b_curves); - if (r != kCGErrorSuccess) - { - fprintf(stderr, "Failed to set gamma ramps for CRTC %i\n", c); - break; - } - } - return r != kCGErrorSuccess; - } - fprintf(stderr, "CRTC %i does not exist\n", use_crtc); - return -1; -} - - -/** - * Resource freeing stage of colour curve control - */ -void blueshift_quartz_close(void) -{ - if (crtcs != NULL) - free(crtcs); - if (gamma_sizes != NULL) - free(gamma_sizes); - close_fake_quartz(); -} - - -/** - * Restore all gamma curves (on each and every CRTC on the system) - * to the settings on ColorSync - */ -void blueshift_quartz_restore(void) -{ - CGDisplayRestoreColorSyncSettings(); -} - diff --git a/src/blueshift_quartz_c.h b/src/blueshift_quartz_c.h deleted file mode 100644 index d21ef02..0000000 --- a/src/blueshift_quartz_c.h +++ /dev/null @@ -1,85 +0,0 @@ -/** - * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -#ifndef BLUESHIFT_QUARTZ_C_H -#define BLUESHIFT_QUARTZ_C_H - - -#include <stdio.h> -#include <stdlib.h> - -#ifdef FAKE_QUARTZ -# include "fake_quartz.h" -#else -# include <CoreGraphics/CGDirectDisplay.h> -# include <CoreGraphics/CGError.h> -#endif - - -#ifndef FAKE_QUARTZ -# define close_fake_quartz() /* Do nothing */ -#endif - - -/** - * Start stage of colour curve control - * - * @return Zero on success - */ -int blueshift_quartz_open(void); - -/** - * Get the number of CRTC:s on the system - * - * @return The number of CRTC:s on the system - */ -int blueshift_quartz_crtc_count(void); - -/** - * Gets the current colour curves - * - * @param use_crtc The CRTC to use - * @return {the size of the each curve, *the red curve, - * *the green curve, *the blue curve}, - * needs to be free:d. `NULL` on error. - */ -uint16_t* blueshift_quartz_read(int use_crtc); - -/** - * Apply stage of colour curve control - * - * @param use_crtc The CRTC to use, -1 for all - * @param r_curve The red colour curve - * @param g_curve The green colour curve - * @param b_curve The blue colour curve - * @return Zero on success - */ -int blueshift_quartz_apply(int use_crtc, float* r_curves, float* g_curves, float* b_curves); - -/** - * Resource freeing stage of colour curve control - */ -void blueshift_quartz_close(void); - -/** - * Restore all gamma curves (on each and every CRTC on the system) - * to the settings on ColorSync - */ -void blueshift_quartz_restore(void); - - -#endif - diff --git a/src/blueshift_randr.pyx b/src/blueshift_randr.pyx deleted file mode 100644 index 842a026..0000000 --- a/src/blueshift_randr.pyx +++ /dev/null @@ -1,161 +0,0 @@ -# -*- python -*- - -# Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - -cimport cython -from libc.stdlib cimport malloc, free -from libc.stdint cimport * - - -cdef extern int blueshift_randr_open(int use_screen, char* display) -''' -Start stage of colour curve control - -@param use_screen The screen to use -@param display The display to use, `NULL` for the current one -@return Zero on success -''' - -cdef extern uint16_t* blueshift_randr_read(int use_crtc) -''' -Gets the current colour curves - -@param use_crtc The CRTC to use -@return {the size of the red curve, *the red curve, - the size of the green curve, *the green curve, - the size of the blue curve, *the blue curve}, - needs to be free:d. `NULL` on error. -''' - -cdef extern int blueshift_randr_apply(int use_crtc, uint16_t* r_curve, uint16_t* g_curve, uint16_t* b_curve) -''' -Apply stage of colour curve control - -@param use_crtc The CRTC to use, -1 for all -@param r_curve The red colour curve -@param g_curve The green colour curve -@param b_curve The blue colour curve -@return Zero on success -''' - -cdef extern void blueshift_randr_close() -''' -Resource freeing stage of colour curve control -''' - - - -cdef uint16_t* r_c -''' -Storage space for the red colour curve in C native data structure -''' - -cdef uint16_t* g_c -''' -Storage space for the green colour curve in C native data structure -''' - -cdef uint16_t* b_c -''' -Storage space for the blue colour curve in C native data structure -''' - - - -def randr_open(int use_screen, display): - ''' - Start stage of colour curve control - - @param use_screen The screen to use - @param display:bytes? The display to use, `None` for the current - @return :int Zero on success - ''' - global r_c, g_c, b_c - # Get the display to use - cdef char* display_ = NULL - if display is not None: - display_ = display - # Allocate the storage space for the C native colour curves - r_c = <uint16_t*>malloc(256 * sizeof(uint16_t)) - g_c = <uint16_t*>malloc(256 * sizeof(uint16_t)) - b_c = <uint16_t*>malloc(256 * sizeof(uint16_t)) - # Check for out-of-memory error - if (r_c is NULL) or (g_c is NULL) or (b_c is NULL): - raise MemoryError() - # Start using RandR for the screen and display - return blueshift_randr_open(use_screen, display_) - - -def randr_read(int use_crtc): - ''' - Gets the current colour curves - - @param use_crtc The CRTC to use - @return :(r:list<int>, g:list<int>, b:list<int>) The current red, green and blue colour curves - ''' - cdef uint16_t* got - # Read the current curves - got = blueshift_randr_read(use_crtc) - if got is NULL: - raise Exception() - # Convert to Python integer lists - r, g, b, i = [], [], [], 0 - for c in (r, g, b): - # while extracting the sizes of the curves - s = got[i] - i += 1 - for j in range(s): - c.append(got[i + j]) - i += s - # Free the native curves - free(got) - return (r, g, b) - - -def randr_apply(crtc_indices, r_curve, g_curve, b_curve): - ''' - Apply stage of colour curve control - - @param crtc_indices:list<int> The indices of the CRTC:s to control, -1 for all - @param r_curve:list<int> The red colour curve - @param g_curve:list<int> The green colour curve - @param b_curve:list<int> The blue colour curve - @return :int Zero on success - ''' - # Convert curves to 16-bit C integers - for i in range(256): - r_c[i] = r_curve[i] & 0xFFFF - g_c[i] = g_curve[i] & 0xFFFF - b_c[i] = b_curve[i] & 0xFFFF - rc = 0 - # For each selected CRTC, - for crtc_index in crtc_indices: - # apply curves. - rc |= blueshift_randr_apply(crtc_index, r_c, g_c, b_c) - return rc - - -def randr_close(): - ''' - Resource freeing stage of colour curve control - ''' - # Free the storage space for the colour curves - free(r_c) - free(g_c) - free(b_c) - # Close free all resources in the native code - blueshift_randr_close() - diff --git a/src/blueshift_randr_c.c b/src/blueshift_randr_c.c deleted file mode 100644 index 19650b7..0000000 --- a/src/blueshift_randr_c.c +++ /dev/null @@ -1,301 +0,0 @@ -/** - * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -#include "blueshift_randr_c.h" - - -/** - * Connection to the X server - */ -static xcb_connection_t* connection; - -/** - * Used to store errors in - */ -static xcb_generic_error_t* error; - -/** - * Screen resources - */ -static xcb_randr_get_screen_resources_current_reply_t* res_reply; - -/** - * The first CRTC - */ -static blueshift_randr_crtc_t* crtcs; - -/** - * The CRTC after the last CRTC - */ -static blueshift_randr_crtc_t* crtcs_end; - - - -/** - * Start stage of colour curve control - * - * @param use_screen The screen to use - * @param display The display to use, `NULL` for the current one - * @return Zero on success - */ -int blueshift_randr_open(int use_screen, char* display) -{ - blueshift_randr_crtc_t* crtcs_; - - xcb_randr_query_version_cookie_t version_cookie; - xcb_randr_query_version_reply_t* randr_version; - xcb_screen_iterator_t iter; - xcb_screen_t* screen; - xcb_randr_get_screen_resources_current_cookie_t res_cookie; - unsigned int crtc_count; - xcb_randr_get_crtc_gamma_size_cookie_t gamma_size_cookie; - xcb_randr_get_crtc_gamma_size_reply_t* gamma_size_reply; - xcb_randr_get_crtc_gamma_cookie_t gamma_get_cookie; - xcb_randr_get_crtc_gamma_reply_t* gamma_get_reply; - int iter_i; - - - /* Get X connection */ - - connection = xcb_connect(display, NULL); - - - /* Check RandR protocol version */ - - version_cookie = xcb_randr_query_version(connection, RANDR_VERSION_MAJOR, RANDR_VERSION_MINOR); - randr_version = xcb_randr_query_version_reply(connection, version_cookie, &error); - - if (error || (randr_version == NULL)) - { - fprintf(stderr, "RandR version query returned %i\n", error ? error->error_code : -1); - xcb_disconnect(connection); - return 1; - } - - if (randr_version->major_version != RANDR_VERSION_MAJOR || randr_version->minor_version < RANDR_VERSION_MINOR) - { - fprintf(stderr, "Unsupported RandR version, got %u.%u, expected %u.%u\n", - randr_version->major_version, randr_version->minor_version, - RANDR_VERSION_MAJOR, RANDR_VERSION_MINOR); - free(randr_version); - xcb_disconnect(connection); - return 1; - } - - free(randr_version); - - - /* Get X resources */ - - iter = xcb_setup_roots_iterator(xcb_get_setup(connection)); - for (iter_i = 0; iter_i < use_screen; iter_i++) - xcb_screen_next(&iter); - screen = iter.data; - - res_cookie = xcb_randr_get_screen_resources_current(connection, screen->root); - res_reply = xcb_randr_get_screen_resources_current_reply(connection, res_cookie, &error); - - if (error) - { - fprintf(stderr, "RandR screen resource query returned %i\n", error->error_code); - xcb_disconnect(connection); - return 1; - } - - - /* Get CRTC:s */ - - crtc_count = res_reply->num_crtcs; - crtcs = malloc(crtc_count * sizeof(blueshift_randr_crtc_t)); - crtcs->crtc = xcb_randr_get_screen_resources_current_crtcs(res_reply); - crtcs_end = crtcs + crtc_count; - - - /* Prepare CRTC:s */ - - for (crtcs_ = crtcs; crtcs_ != crtcs_end; crtcs_++) - { - /* Set CRTC */ - - if (crtcs_ != crtcs) - crtcs_->crtc = (crtcs_ - 1)->crtc + 1; - - - /* Get curve X-axis size */ - - gamma_size_cookie = xcb_randr_get_crtc_gamma_size(connection, *(crtcs_->crtc)); - gamma_size_reply = xcb_randr_get_crtc_gamma_size_reply(connection, gamma_size_cookie, &error); - - if (error) - { - fprintf(stderr, "RandR CRTC gamma size query returned %i\n", error->error_code); - xcb_disconnect(connection); - return 1; - } - - crtcs_->curve_size = gamma_size_reply->size; - free(gamma_size_reply); - - - /* Acquire curve control */ - - gamma_get_cookie = xcb_randr_get_crtc_gamma(connection, *(crtcs_->crtc)); - gamma_get_reply = xcb_randr_get_crtc_gamma_reply(connection, gamma_get_cookie, &error); - - if (error) - { - fprintf(stderr, "RandR CRTC gamma query returned %i\n", error->error_code); - xcb_disconnect(connection); - return 1; - } - - free(gamma_get_reply); - } - - return 0; -} - - -/** - * Gets the current colour curves - * - * @param use_crtc The CRTC to use - * @return {the size of the red curve, *the red curve, - * the size of the green curve, *the green curve, - * the size of the blue curve, *the blue curve}, - * needs to be free:d. `NULL` on error. - */ -uint16_t* blueshift_randr_read(int use_crtc) -{ - xcb_randr_get_crtc_gamma_cookie_t gamma_get_cookie; - xcb_randr_get_crtc_gamma_reply_t* gamma_get_reply; - uint16_t* r_gamma, * R_gamma; - uint16_t* g_gamma, * G_gamma; - uint16_t* b_gamma, * B_gamma; - int i, R_size, G_size, B_size; - - /* Read curves */ - - gamma_get_cookie = xcb_randr_get_crtc_gamma(connection, *((crtcs + use_crtc)->crtc)); - gamma_get_reply = xcb_randr_get_crtc_gamma_reply(connection, gamma_get_cookie, &error); - - if (error) - { - fprintf(stderr, "RandR CRTC gamma query returned %i\n", error->error_code); - xcb_disconnect(connection); - return NULL; - } - - R_size = xcb_randr_get_crtc_gamma_red_length(gamma_get_reply); - G_size = xcb_randr_get_crtc_gamma_green_length(gamma_get_reply); - B_size = xcb_randr_get_crtc_gamma_blue_length(gamma_get_reply); - - if ((R_size < 2) || (G_size < 2) || (B_size < 2)) - { - fprintf(stderr, "RandR CRTC gamma query returned impossibly small ramps\n"); - xcb_disconnect(connection); - return NULL; - } - - if ((R_size | G_size | B_size) > UINT16_MAX) - { - fprintf(stderr, "RandR CRTC gamma query returned unexpectedly large ramps\n"); - xcb_disconnect(connection); - return NULL; - } - - R_gamma = xcb_randr_get_crtc_gamma_red(gamma_get_reply); - G_gamma = xcb_randr_get_crtc_gamma_green(gamma_get_reply); - B_gamma = xcb_randr_get_crtc_gamma_blue(gamma_get_reply); - - r_gamma = malloc((3 + (size_t)R_size + (size_t)G_size + (size_t)B_size) * sizeof(uint16_t)); - g_gamma = r_gamma + R_size + 1; - b_gamma = g_gamma + G_size + 1; - if (r_gamma == NULL) - { - fprintf(stderr, "Out of memory\n"); - free(gamma_get_reply); - xcb_disconnect(connection); - return NULL; - } - - *r_gamma++ = (uint16_t)R_size; - *g_gamma++ = (uint16_t)G_size; - *b_gamma++ = (uint16_t)B_size; - - for (i = 0; i < R_size; i++) *(r_gamma + i) = *(R_gamma + i); - for (i = 0; i < G_size; i++) *(g_gamma + i) = *(G_gamma + i); - for (i = 0; i < B_size; i++) *(b_gamma + i) = *(B_gamma + i); - - free(gamma_get_reply); - return r_gamma - 1; -} - - -/** - * Apply stage of colour curve control - * - * @param use_crtc The CRTC to use, -1 for all - * @param r_curve The red colour curve - * @param g_curve The green colour curve - * @param b_curve The blue colour curve - * @return Zero on success - */ -int blueshift_randr_apply(int use_crtc, uint16_t* r_curve, uint16_t* g_curve, uint16_t* b_curve) -{ - /* Select first CRTC */ - blueshift_randr_crtc_t* crtc_start = crtcs + (use_crtc < 0 ? 0 : use_crtc); - - /* Select exclusive last CRTC */ - blueshift_randr_crtc_t* crtc_end = use_crtc < 0 ? crtcs_end : (crtc_start + 1); - - blueshift_randr_crtc_t* crtc; - xcb_void_cookie_t gamma_set_cookie; - - - /* Apply for all selected CRTC:s */ - - for (crtc = crtc_start; crtc != crtc_end; crtc++) - { - /* Apply curves */ - - gamma_set_cookie = xcb_randr_set_crtc_gamma_checked(connection, *(crtc->crtc), crtc->curve_size, - r_curve, g_curve, b_curve); - error = xcb_request_check(connection, gamma_set_cookie); - - if (error) - { - fprintf(stderr, "RandR CRTC control returned %i\n", error->error_code); - return 1; - } - } - - return 0; -} - - -/** - * Resource freeing stage of colour curve control - */ -void blueshift_randr_close(void) -{ - /* Free remaining resources */ - - free(crtcs); - free(res_reply); - xcb_disconnect(connection); -} - diff --git a/src/blueshift_randr_c.h b/src/blueshift_randr_c.h deleted file mode 100644 index 2bc23f2..0000000 --- a/src/blueshift_randr_c.h +++ /dev/null @@ -1,100 +0,0 @@ -/** - * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -#ifndef BLUESHIFT_RANDR_C_H -#define BLUESHIFT_RANDR_C_H - - -#include <stdlib.h> -#include <stdio.h> -#include <inttypes.h> - -#include <xcb/xcb.h> -#include <xcb/randr.h> - - - -/** - * The major version of RandR the program expects - */ -#define RANDR_VERSION_MAJOR 1 - -/** - * The minor version of RandR the program expects - */ -#define RANDR_VERSION_MINOR 3 - - - -/** - * Data structure for CRTC caches - */ -typedef struct blueshift_randr_crtc -{ - /** - * Size of colour curves on the X-axis - */ - uint16_t curve_size; - - /** - * CRT controller - */ - xcb_randr_crtc_t* crtc; - -} blueshift_randr_crtc_t; - - - -/** - * Start stage of colour curve control - * - * @param use_screen The screen to use - * @param display The display to use, `NULL` for the current one - * @return Zero on success - */ -int blueshift_randr_open(int use_screen, char* display); - -/** - * Gets the current colour curves - * - * @param use_crtc The CRTC to use - * @return {the size of the red curve, *the red curve, - * the size of the green curve, *the green curve, - * the size of the blue curve, *the blue curve}, - * needs to be free:d. `NULL` on error. - */ -uint16_t* blueshift_randr_read(int use_crtc); - -/** - * Apply stage of colour curve control - * - * @param use_crtc The CRTC to use, -1 for all - * @param r_curve The red colour curve - * @param g_curve The green colour curve - * @param b_curve The blue colour curve - * @return Zero on success - */ -int blueshift_randr_apply(int use_crtc, uint16_t* r_curve, uint16_t* g_curve, uint16_t* b_curve); - -/** - * Resource freeing stage of colour curve control - */ -void blueshift_randr_close(void); - - - -#endif - diff --git a/src/blueshift_vidmode.pyx b/src/blueshift_vidmode.pyx deleted file mode 100644 index f5d3760..0000000 --- a/src/blueshift_vidmode.pyx +++ /dev/null @@ -1,155 +0,0 @@ -# -*- python -*- - -# Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - -cimport cython -from libc.stdlib cimport malloc, free -from libc.stdint cimport * - - -cdef extern int blueshift_vidmode_open(int use_screen, char* display) -''' -Start stage of colour curve control - -@param use_screen The screen to use -@param display The display to use, `NULL` for the current one -@return Zero on error, otherwise the size of colours curves -''' - -cdef extern int blueshift_vidmode_read(uint16_t* r_curve, uint16_t* g_curve, uint16_t* b_curve) -''' -Gets the current colour curves - -@param r_gamma Storage location for the red colour curve -@param g_gamma Storage location for the green colour curve -@param b_gamma Storage location for the blue colour curve -@return Zero on success -''' - -cdef extern int blueshift_vidmode_apply(uint16_t* r_curve, uint16_t* g_curve, uint16_t* b_curve) -''' -Apply stage of colour curve control - -@param r_curve The red colour curve -@param g_curve The green colour curve -@param b_curve The blue colour curve -@return Zero on success -''' - -cdef extern void blueshift_vidmode_close() -''' -Resource freeing stage of colour curve control -''' - - - -cdef int vidmode_gamma_size -''' -The size of the curves -''' -vidmode_gamma_size = 0 - - -cdef uint16_t* r_c -''' -Storage space for the red colour curve in C native data structure -''' - -cdef uint16_t* g_c -''' -Storage space for the green colour curve in C native data structure -''' - -cdef uint16_t* b_c -''' -Storage space for the blue colour curve in C native data structure -''' - - - -def vidmode_open(int use_screen, display): - ''' - Start stage of colour curve control - - @param use_screen The screen to use - @param display:bytes? The display to use, `None` for the current - @return :bool Whether call was successful - ''' - global vidmode_gamma_size, r_c, g_c, b_c - # Get the display to use - cdef char* display_ = NULL - if display is not None: - display_ = display - # Allocate the storage space for the C native colour curves - r_c = <uint16_t*>malloc(256 * sizeof(uint16_t)) - g_c = <uint16_t*>malloc(256 * sizeof(uint16_t)) - b_c = <uint16_t*>malloc(256 * sizeof(uint16_t)) - # Check for out-of-memory error - if (r_c is NULL) or (g_c is NULL) or (b_c is NULL): - raise MemoryError() - # Start using VidMode for the screen and display - vidmode_gamma_size = blueshift_vidmode_open(use_screen, display_) - # Successful only if we got an even usable size for the curves - return vidmode_gamma_size > 1 - - -def vidmode_read(): - ''' - Gets the current colour curves - - @return :(r:list<int>, g:list<int>, b:list<int>) The current red, green and blue colour curves - ''' - # Read the current curves - if not blueshift_vidmode_read(r_c, g_c, b_c) == 0: - raise Exception() - # Convert to Python integer lists - r, g, b = [], [], [] - for i in range(vidmode_gamma_size): - r.append(r_c[i]) - g.append(g_c[i]) - b.append(b_c[i]) - return (r, g, b) - - -def vidmode_apply(r_curve, g_curve, b_curve): - ''' - Apply stage of colour curve control - - @param r_curve:list<int> The red colour curve - @param g_curve:list<int> The green colour curve - @param b_curve:list<int> The blue colour curve - @return :int Zero on success - ''' - # Convert curves to 16-bit C integers - for i in range(256): - r_c[i] = r_curve[i] & 0xFFFF - g_c[i] = g_curve[i] & 0xFFFF - b_c[i] = b_curve[i] & 0xFFFF - # Apply curves - return blueshift_vidmode_apply(r_c, g_c, b_c) - - -def vidmode_close(): - ''' - Resource freeing stage of colour curve control - ''' - # Free the storage space for the colour curves - free(r_c) - free(g_c) - free(b_c) - # Close free all resources in the native code - blueshift_vidmode_close() - diff --git a/src/blueshift_vidmode_c.c b/src/blueshift_vidmode_c.c deleted file mode 100644 index 108f8c4..0000000 --- a/src/blueshift_vidmode_c.c +++ /dev/null @@ -1,143 +0,0 @@ -/** - * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -#include "blueshift_vidmode_c.h" - - -/** - * The X server display - */ -static Display* connection; - -/** - * The X screen - */ -static int screen; - -/** - * Size of colour curves on the X-axis - */ -static int curve_size; - - - -/** - * Start stage of colour curve control - * - * @param use_screen The screen to use - * @param display The display to use, `NULL` for the current one - * @return Zero on error, otherwise the size of colours curves - */ -int blueshift_vidmode_open(int use_screen, char* display) -{ - int _major, _minor; - - - /* Get X display */ - - if ((connection = XOpenDisplay(display)) == NULL) - { - fprintf(stderr, "Cannot open X display\n"); - return 0; - } - - - /* Check for VidMode extension */ - - if (XF86VidModeQueryVersion(connection, &_major, &_minor) == 0) - { - fprintf(stderr, "VidMode version query failed\n"); - XCloseDisplay(connection); - return 0; - } - - - /* Get curve's size on the encoding axis */ - - screen = use_screen; - if (XF86VidModeGetGammaRampSize(connection, screen, &curve_size) == 0) - { - fprintf(stderr, "VidMode gamma size query failed\n"); - XCloseDisplay(connection); - return 0; - } - - if (curve_size <= 1) - { - fprintf(stderr, "VidMode gamma size query failed, impossible dimension\n"); - XCloseDisplay(connection); - return 0; - } - - return curve_size; -} - - -/** - * Gets the current colour curves - * - * @param r_gamma Storage location for the red colour curve - * @param g_gamma Storage location for the green colour curve - * @param b_gamma Storage location for the blue colour curve - * @return Zero on success - */ -int blueshift_vidmode_read(uint16_t* r_gamma, uint16_t* g_gamma, uint16_t* b_gamma) -{ - /* Read curves */ - - if (XF86VidModeGetGammaRamp(connection, screen, curve_size, r_gamma, g_gamma, b_gamma) == 0) - { - fprintf(stderr, "VidMode gamma query failed\n"); - XCloseDisplay(connection); - return 1; - } - - return 0; -} - - -/** - * Apply stage of colour curve control - * - * @param r_curve The red colour curve - * @param g_curve The green colour curve - * @param b_curve The blue colour curve - * @return Zero on success - */ -int blueshift_vidmode_apply(uint16_t* r_curve, uint16_t* g_curve, uint16_t* b_curve) -{ - /* Apply curves */ - - if (XF86VidModeSetGammaRamp(connection, screen, curve_size, r_curve, g_curve, b_curve) == 0) - { - fprintf(stderr, "VidMode gamma control failed\n"); - return 1; - } - - return 0; -} - - -/** - * Resource freeing stage of colour curve control - */ -void blueshift_vidmode_close(void) -{ - /* Free remaining resources */ - - XCloseDisplay(connection); -} - diff --git a/src/blueshift_vidmode_c.h b/src/blueshift_vidmode_c.h deleted file mode 100644 index 188db4a..0000000 --- a/src/blueshift_vidmode_c.h +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -#ifndef BLUESHIFT_VIDMODE_C_H -#define BLUESHIFT_VIDMODE_C_H - - -#include <stdlib.h> -#include <stdio.h> -#include <inttypes.h> - -#include <X11/Xlib.h> -#include <X11/extensions/xf86vmode.h> - - - -/** - * Start stage of colour curve control - * - * @param use_screen The screen to use - * @param display The display to use, `NULL` for the current one - * @return Zero on error, otherwise the size of colours curves - */ -int blueshift_vidmode_open(int use_screen, char* display); - -/** - * Gets the current colour curves - * - * @param r_gamma Storage location for the red colour curve - * @param g_gamma Storage location for the green colour curve - * @param b_gamma Storage location for the blue colour curve - * @return Zero on success - */ -int blueshift_vidmode_read(uint16_t* r_gamma, uint16_t* g_gamma, uint16_t* b_gamma); - -/** - * Apply stage of colour curve control - * - * @param r_curve The red colour curve - * @param g_curve The green colour curve - * @param b_curve The blue colour curve - * @return Zero on success - */ -int blueshift_vidmode_apply(uint16_t* r_curve, uint16_t* g_curve, uint16_t* b_curve); - -/** - * Resource freeing stage of colour curve control - */ -void blueshift_vidmode_close(void); - - -#endif - diff --git a/src/blueshift_w32gdi.pyx b/src/blueshift_w32gdi.pyx deleted file mode 100644 index 2a9750b..0000000 --- a/src/blueshift_w32gdi.pyx +++ /dev/null @@ -1,151 +0,0 @@ -# -*- python -*- - -# Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - -cimport cython -from libc.stdlib cimport malloc, free -from libc.stdint cimport * - - -cdef extern int blueshift_w32gdi_open() -''' -Start stage of colour curve control - -@return Zero on success -''' - -cdef extern int blueshift_w32gdi_crtc_count() -''' -Get the number of CRTC:s on the system - -@return The number of CRTC:s on the system -''' - -cdef extern uint16_t* blueshift_w32gdi_read(int use_crtc) -''' -Gets the current colour curves - -@param use_crtc The CRTC to use -@return {the size of the each curve, *the red curve, - *the green curve, *the blue curve}, - needs to be free:d. `NULL` on error. -''' - -cdef extern int blueshift_w32gdi_apply(int use_crtc, uint16_t* rgb_curves) -''' -Apply stage of colour curve control - -@param use_crtc The CRTC to use, -1 for all -@param rgb_curve The concatenation of the red, the green and the blue colour curves -@return Zero on success -''' - -cdef extern void blueshift_w32gdi_close() -''' -Resource freeing stage of colour curve control -''' - - - -cdef uint16_t* rgb_c -''' -Storage space for the colour curves in C native data structure -''' - - - -def w32gdi_open(): - ''' - Start stage of colour curve control - - @return :int Zero on success - ''' - global rgb_c - # Allocate the storage space for the C native colour curves - rgb_c = <uint16_t*>malloc(3 * 256 * sizeof(uint16_t)) - # Check for out-of-memory error - if (rgb_c is NULL): - raise MemoryError() - # Start using W32 GDI - return blueshift_w32gdi_open() - - -def w32gdi_crtc_count(): - ''' - Get the number of CRTC:s on the system - - @return :int The number of CRTC:s on the system - ''' - return blueshift_w32gdi_crtc_count() - - -def w32gdi_read(int use_crtc): - ''' - Gets the current colour curves - - @param use_crtc The CRTC to use - @return :(r:list<int>, g:list<int>, b:list<int>) The current red, green and blue colour curves - ''' - cdef uint16_t* got - # Read the current curves - got = blueshift_w32gdi_read(use_crtc) - if got is NULL: - raise Exception() - # Convert to Python integer lists - r, g, b, i = [], [], [], 1 - s = got[0] - for c in (r, g, b): - # while extracting the sizes of the curves - for j in range(s): - c.append(got[i + j]) - i += s - # Free the native curves - free(got) - return (r, g, b) - - -def w32gdi_apply(crtc_indices, r_curve, g_curve, b_curve): - ''' - Apply stage of colour curve control - - @param crtc_indices:list<int> The indices of the CRTC:s to control, -1 for all - @param r_curve:list<int> The red colour curve - @param g_curve:list<int> The green colour curve - @param b_curve:list<int> The blue colour curve - @return :int Zero on success - ''' - # Convert curves to 16-bit C integers - for i in range(256): - rgb_c[0 * 256 + i] = r_curve[i] & 0xFFFF - rgb_c[1 * 256 + i] = g_curve[i] & 0xFFFF - rgb_c[2 * 256 + i] = b_curve[i] & 0xFFFF - rc = 0 - # For each selected CRTC, - for crtc_index in crtc_indices: - # apply curves. - rc |= blueshift_w32gdi_apply(crtc_index, rgb_c) - return rc - - -def w32gdi_close(): - ''' - Resource freeing stage of colour curve control - ''' - # Free the storage space for the colour curves - free(rgb_c) - # Close free all resources in the native code - blueshift_w32gdi_close() - diff --git a/src/blueshift_w32gdi_c.c b/src/blueshift_w32gdi_c.c deleted file mode 100644 index d304b25..0000000 --- a/src/blueshift_w32gdi_c.c +++ /dev/null @@ -1,181 +0,0 @@ -/** - * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -#include "blueshift_w32gdi_c.h" - - - -/** - * The number of CRTC:s on the system - */ -static int crtc_count; - -/** - * The device contexts for each CRTC - */ -static HDC* crtcs; - - - -/** - * Start stage of colour curve control - * - * @return Zero on success - */ -int blueshift_w32gdi_open(void) -{ - int c; - HDC hDC; - DISPLAY_DEVICE display; - display.cb = sizeof(DISPLAY_DEVICE); - - crtc_count = 0; - while (EnumDisplayDevices(NULL, crtc_count, &display, 0)) - crtc_count++; - - if (crtc_count == 0) - { - crtcs = NULL; - return 0; - } - - crtcs = malloc(crtc_count * sizeof(HDC)); - if (crtcs == NULL) - { - fprintf(stderr, "Out of memory\n"); - return 1; - } - - for (c = 0; c < crtc_count; c++) - { - /* Open device context */ - if (EnumDisplayDevices(NULL, c, &display, 0) == FALSE) - { - fprintf(stderr, "Cannot find display, are you unplugging stuff?\n"); - crtc_count = c; - return 1; - } - if (!(display.StateFlags & DISPLAY_DEVICE_ACTIVE)) - { - fprintf(stderr, "Cannot to open device context, it is not active\n"); - crtc_count = c; - return 1; - } - hDC = *(crtcs + c) = CreateDC(TEXT("DISPLAY"), display.DeviceName, NULL, NULL); - if (hDC == NULL) - { - fprintf(stderr, "Unable to open device context\n"); - crtc_count = c; - return 1; - } - - /* Check support for gamma ramps */ - if (GetDeviceCaps(hDC, COLORMGMTCAPS) != CM_GAMMA_RAMP) - { - fprintf(stderr, "CRTC %i does not support gamma ramps\n", c); - ReleaseDC(NULL, hDC); - crtc_count = c; - return 1; - } - } - - return 0; -} - - -/** - * Get the number of CRTC:s on the system - * - * @return The number of CRTC:s on the system - */ -int blueshift_w32gdi_crtc_count(void) -{ - return crtc_count; -} - - -/** - * Gets the current colour curves - * - * @param use_crtc The CRTC to use - * @return {the size of the each curve, *the red curve, - * *the green curve, *the blue curve}, - * needs to be free:d. `NULL` on error. - */ -uint16_t* blueshift_w32gdi_read(int use_crtc) -{ - uint16_t* rc = NULL; - if ((use_crtc < 0) || (use_crtc >= crtc_count)) - fprintf(stderr, "CRTC %i does not exist\n", use_crtc); - else - { - rc = malloc((1 + 3 * GAMMA_RAMP_SIZE) * sizeof(uint16_t)); - if (rc == NULL) - fprintf(stderr, "Out of memory\n"); - else - { - *rc = GAMMA_RAMP_SIZE; - if (GetDeviceGammaRamp(*(crtcs + use_crtc), rc + 1) == FALSE) - { - fprintf(stderr, "Unable to read current gamma ramps from CRTC %i\n", use_crtc); - free(rc); - rc = NULL; - } - } - } - return rc; -} - - -/** - * Apply stage of colour curve control - * - * @param use_crtc The CRTC to use, -1 for all - * @param rgb_curve The concatenation of the red, the green and the blue colour curves - * @return Zero on success - */ -int blueshift_w32gdi_apply(int use_crtc, uint16_t* rgb_curves) -{ - int r = 1; - if (use_crtc < crtc_count) - { - int c = use_crtc < 0 ? 0 : use_crtc; - int n = use_crtc < 0 ? crtc_count : (use_crtc + 1); - for (; c < n; c++) - if (!(r = SetDeviceGammaRamp(*(crtcs + c), rgb_curves))) - { - fprintf(stderr, "Unable to set gamma ramps\n"); - break; - } - } - else - fprintf(stderr, "CRTC %i does not exist\n", use_crtc); - return !r; -} - - -/** - * Resource freeing stage of colour curve control - */ -void blueshift_w32gdi_close(void) -{ - int c; - for (c = 0; c < crtc_count; c++) - ReleaseDC(NULL, *(crtcs + c)); - if (crtcs != NULL) - free(crtcs); -} - diff --git a/src/blueshift_w32gdi_c.h b/src/blueshift_w32gdi_c.h deleted file mode 100644 index 540930b..0000000 --- a/src/blueshift_w32gdi_c.h +++ /dev/null @@ -1,83 +0,0 @@ -/** - * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -#ifndef BLUESHIFT_W32GDI_C_H -#define BLUESHIFT_W32GDI_C_H - - -#include <stdio.h> -#include <stdlib.h> - -#ifndef WINVER -# define WINVER 0x0500 -#endif -#ifdef FAKE_W32GDI -# include "fake_w32gdi.h" -#else -# include <windows.h> -# include <wingdi.h> -#endif - - - -/** - * The size of gamma ramps - */ -#define GAMMA_RAMP_SIZE 256 - - - -/** - * Start stage of colour curve control - * - * @return Zero on success - */ -int blueshift_w32gdi_open(void); - -/** - * Get the number of CRTC:s on the system - * - * @return The number of CRTC:s on the system - */ -int blueshift_w32gdi_crtc_count(void); - -/** - * Gets the current colour curves - * - * @param use_crtc The CRTC to use - * @return {the size of the each curve, *the red curve, - * *the green curve, *the blue curve}, - * needs to be free:d. `NULL` on error. - */ -uint16_t* blueshift_w32gdi_read(int use_crtc); - -/** - * Apply stage of colour curve control - * - * @param use_crtc The CRTC to use, -1 for all - * @param rgb_curve The concatenation of the red, the green and the blue colour curves - * @return Zero on success - */ -int blueshift_w32gdi_apply(int use_crtc, uint16_t* rgb_curves); - -/** - * Resource freeing stage of colour curve control - */ -void blueshift_w32gdi_close(void); - - -#endif - diff --git a/src/colour.py b/src/colour.py index e2bbc01..3d1e40c 100644 --- a/src/colour.py +++ b/src/colour.py @@ -16,7 +16,7 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # This module implements functions from convertions between colour spaces -# and comparion of colours +# and comparion of colours. def linear_to_standard(*colour): diff --git a/src/fake_quartz.c b/src/fake_quartz.c deleted file mode 100644 index dca731f..0000000 --- a/src/fake_quartz.c +++ /dev/null @@ -1,235 +0,0 @@ -/** - * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -#include "fake_quartz.h" - - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include <xcb/xcb.h> -#include <xcb/randr.h> - - -/* 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. */ - - -static xcb_connection_t* conn = NULL; -static xcb_randr_get_screen_resources_current_reply_t* res_reply = NULL; -static uint32_t crtc_count = 0; -static xcb_randr_crtc_t* crtcs = NULL; -static uint16_t* original_ramps = NULL; - - -CGError CGGetOnlineDisplayList(uint32_t max_size, CGDirectDisplayID* displays_out, uint32_t* count_out) -{ - uint32_t i; - - if (conn == NULL) - { - xcb_generic_error_t* error; - xcb_screen_iterator_t iter; - xcb_randr_get_screen_resources_current_cookie_t res_cookie; - xcb_randr_get_crtc_gamma_cookie_t gamma_cookie; - xcb_randr_get_crtc_gamma_reply_t* gamma_reply; - - conn = xcb_connect(NULL, NULL); - - iter = xcb_setup_roots_iterator(xcb_get_setup(conn)); - res_cookie = xcb_randr_get_screen_resources_current(conn, iter.data->root); - res_reply = xcb_randr_get_screen_resources_current_reply(conn, res_cookie, &error); - - if (error) - { - fprintf(stderr, "Failed to open X connection\n"); - xcb_disconnect(conn); - crtc_count = 0; - return ~kCGErrorSuccess; - } - - crtc_count = (uint32_t)(res_reply->num_crtcs); - crtcs = xcb_randr_get_screen_resources_current_crtcs(res_reply); - - original_ramps = malloc(crtc_count * 3 * 256 * sizeof(uint16_t)); - if (original_ramps == NULL) - { - perror("malloc"); - xcb_disconnect(conn); - crtc_count = 0; - return ~kCGErrorSuccess; - } - - for (i = 0; i < crtc_count; i++) - { - gamma_cookie = xcb_randr_get_crtc_gamma(conn, crtcs[i]); - gamma_reply = xcb_randr_get_crtc_gamma_reply(conn, gamma_cookie, &error); - - if (error) - { - fprintf(stderr, "Failed to read gamma ramps\n"); - xcb_disconnect(conn); - crtc_count = 0; - return ~kCGErrorSuccess; - } - -#define __DEST(C) original_ramps + (C + 3 * i) * 256 -#define __SRC(C) xcb_randr_get_crtc_gamma_##C(gamma_reply) - memcpy(__DEST(0), __SRC(red), 256 * sizeof(uint16_t)); - memcpy(__DEST(1), __SRC(green), 256 * sizeof(uint16_t)); - memcpy(__DEST(2), __SRC(blue), 256 * sizeof(uint16_t)); -#undef __SRC -#undef __DEST - - free(gamma_reply); - } - } - - for (i = 0; (i < max_size) && (i < crtc_count); i++) - *(displays_out + i) = (CGDirectDisplayID)i; - - *count_out = i; - return kCGErrorSuccess; -} - - -CGError CGSetDisplayTransferByTable(CGDirectDisplayID display, uint32_t gamma_size, const CGGammaValue* red, - const CGGammaValue* green, const CGGammaValue* blue) -{ - xcb_void_cookie_t gamma_cookie; - uint16_t r_int[256]; - uint16_t g_int[256]; - uint16_t b_int[256]; - long i; - int32_t v; - - if (gamma_size != 256) - { - fprintf(stderr, "Gamma size should be 256\n"); - abort(); - } - - for (i = 0; i < 256; i++) - { - v = (int32_t)(red[i] * UINT16_MAX); - r_int[i] = (uint16_t)(v < 0 ? 0 : v > UINT16_MAX ? UINT16_MAX : v); - - v = (int32_t)(green[i] * UINT16_MAX); - g_int[i] = (uint16_t)(v < 0 ? 0 : v > UINT16_MAX ? UINT16_MAX : v); - - v = (int32_t)(blue[i] * UINT16_MAX); - b_int[i] = (uint16_t)(v < 0 ? 0 : v > UINT16_MAX ? UINT16_MAX : v); - } - - gamma_cookie = xcb_randr_set_crtc_gamma_checked(conn, crtcs[display], - (uint16_t)gamma_size, r_int, g_int, b_int); - return xcb_request_check(conn, gamma_cookie) == NULL ? kCGErrorSuccess : ~kCGErrorSuccess; -} - - -CGError CGGetDisplayTransferByTable(CGDirectDisplayID display, uint32_t gamma_size, CGGammaValue* red, - CGGammaValue* green, CGGammaValue* blue, uint32_t* gamma_size_out) -{ - xcb_randr_get_crtc_gamma_cookie_t gamma_cookie; - xcb_randr_get_crtc_gamma_reply_t* gamma_reply; - xcb_generic_error_t* error; - uint16_t* r_int; - uint16_t* g_int; - uint16_t* b_int; - long i; - - if (gamma_size != 256) - { - fprintf(stderr, "Gamma size should be 256, got %u\n", gamma_size); - abort(); - } - - *gamma_size_out = 256; - - gamma_cookie = xcb_randr_get_crtc_gamma(conn, crtcs[display]); - gamma_reply = xcb_randr_get_crtc_gamma_reply(conn, gamma_cookie, &error); - - if (error) - { - fprintf(stderr, "Failed to write gamma ramps\n"); - return ~kCGErrorSuccess; - } - - r_int = xcb_randr_get_crtc_gamma_red(gamma_reply); - g_int = xcb_randr_get_crtc_gamma_green(gamma_reply); - b_int = xcb_randr_get_crtc_gamma_blue(gamma_reply); - - for (i = 0; i < 256; i++) - { - red[i] = (CGGammaValue)(r_int[i]) / UINT16_MAX; - green[i] = (CGGammaValue)(g_int[i]) / UINT16_MAX; - blue[i] = (CGGammaValue)(b_int[i]) / UINT16_MAX; - } - - free(gamma_reply); - return kCGErrorSuccess; -} - - -void CGDisplayRestoreColorSyncSettings(void) -{ - xcb_generic_error_t* error; - xcb_void_cookie_t gamma_cookie; - uint32_t i; - - for (i = 0; i < crtc_count; i++) - { - gamma_cookie = xcb_randr_set_crtc_gamma_checked(conn, crtcs[i], 256, - original_ramps + (0 + 3 * i) * 256, - original_ramps + (1 + 3 * i) * 256, - original_ramps + (2 + 3 * i) * 256); - error = xcb_request_check(conn, gamma_cookie); - if (error) - fprintf(stderr, "Quartz gamma reset emulation with RandR returned %i\n", error->error_code); - } -} - - -uint32_t CGDisplayGammaTableCapacity(CGDirectDisplayID display) -{ - (void) display; - return 256; -} - - -void close_fake_quartz(void) -{ - if (res_reply != NULL) - { - free(res_reply); - res_reply = NULL; - } - if (conn != NULL) - { - xcb_disconnect(conn); - conn = NULL; - } - if (original_ramps != NULL) - { - free(original_ramps); - original_ramps = NULL; - } -} - diff --git a/src/fake_quartz.h b/src/fake_quartz.h deleted file mode 100644 index d6603dc..0000000 --- a/src/fake_quartz.h +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -#ifndef BLUESHIFT_FAKE_QUARTZ_H -#define BLUESHIFT_FAKE_QUARTZ_H - - -/* This header file contains some capabilities of - <CoreGraphics/CGDirectDisplay.h> and <CoreGraphics/CGError.h>, - and can be used modify gamma ramps without Mac OS X and Quartz - but with its API. - - The content of this file is based on the documentation found on: - - https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/Quartz_Services_Ref/Reference/reference.html - - https://developer.apple.com/library/mac/documentation/CoreGraphics/Reference/CoreGraphicsConstantsRef/Reference/reference.html#//apple_ref/c/tdef/CGError -*/ - - -#include <stdint.h> - - -typedef int32_t CGError; -#define kCGErrorSuccess 0 - -typedef float CGGammaValue; -typedef uint32_t CGDirectDisplayID; - - -CGError CGGetOnlineDisplayList(uint32_t max_size, CGDirectDisplayID* displays_out, uint32_t* count_out); - -CGError CGSetDisplayTransferByTable(CGDirectDisplayID display, uint32_t gamma_size, const CGGammaValue* red, - const CGGammaValue* green, const CGGammaValue* blue); - -CGError CGGetDisplayTransferByTable(CGDirectDisplayID display, uint32_t gamma_size, CGGammaValue* red, - CGGammaValue* green, CGGammaValue* blue, uint32_t* gamma_size_out); - -void CGDisplayRestoreColorSyncSettings(void); - -uint32_t CGDisplayGammaTableCapacity(CGDirectDisplayID display) __attribute__((const)); - - -/* The follow part most only be used when this module is used, - it cannot be used when the real CoreGraphics is used. - CoreGraphics does not have this function, it is added so - that there is a way to cleanly close the X connection - and free resources needed by this module. */ -void close_fake_quartz(void); - - -#endif - diff --git a/src/fake_w32gdi.c b/src/fake_w32gdi.c deleted file mode 100644 index c11bd82..0000000 --- a/src/fake_w32gdi.c +++ /dev/null @@ -1,204 +0,0 @@ -/** - * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -#include "fake_w32gdi.h" - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include <xcb/xcb.h> -#include <xcb/randr.h> - - -/* 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. */ - - -#define GAMMA_RAMP_SIZE 256 - - -static xcb_connection_t* conn = NULL; -static size_t dc_count = 0; -static ssize_t crtc_count = -1; -static xcb_randr_crtc_t* crtcs = NULL; -static xcb_randr_get_screen_resources_current_reply_t* res_reply = NULL; - - -/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd144871(v=vs.85).aspx */ -HDC GetDC(HWND hWnd) -{ - (void) hWnd; - return CreateDC(TEXT("DISPLAY"), "0", NULL, NULL); -} - - -/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd162920(v=vs.85).aspx */ -int ReleaseDC(HWND hWnd, HDC hDC) -{ - (void) hWnd; - (void) hDC; - - dc_count--; - if (dc_count == 0) { - if (conn != NULL) - xcb_disconnect(conn); - conn = NULL; - if (res_reply != NULL) - free(res_reply); - res_reply = NULL; - } - - return 1; -} - - -/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877(v=vs.85).aspx */ -int GetDeviceCaps(HDC hDC, int nIndex) -{ - (void) hDC; - - return CM_GAMMA_RAMP + nIndex - COLORMGMTCAPS; -} - - -/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd372194(v=vs.85).aspx */ -BOOL SetDeviceGammaRamp(HDC hDC, LPVOID lpRamp) -{ - xcb_void_cookie_t gamma_cookie = - xcb_randr_set_crtc_gamma_checked(conn, *(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); - return xcb_request_check(conn, gamma_cookie) == NULL ? TRUE : FALSE; -} - - -/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd316946(v=vs.85).aspx */ -BOOL GetDeviceGammaRamp(HDC hDC, LPVOID lpRamp) -{ - xcb_randr_get_crtc_gamma_cookie_t gamma_cookie; - xcb_randr_get_crtc_gamma_reply_t* gamma_reply; - xcb_generic_error_t* error; - - gamma_cookie = xcb_randr_get_crtc_gamma(conn, *(xcb_randr_crtc_t*)hDC); - gamma_reply = xcb_randr_get_crtc_gamma_reply(conn, 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)) - - 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; -} - - -/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd183490(v=vs.85).aspx */ -HDC CreateDC(LPCTSTR lpszDriver, LPCTSTR lpszDevice, void* lpszOutput, void* lpInitData) -{ - int crtc_index; - - (void) lpszOutput; - (void) lpInitData; - - if (strcmp(lpszDriver, "DISPLAY")) - return NULL; - - crtc_index = atoi(lpszDevice); - - if (dc_count == 0) - { - xcb_generic_error_t* error; - xcb_screen_iterator_t iter; - xcb_randr_get_screen_resources_current_cookie_t res_cookie; - - conn = xcb_connect(NULL, NULL); - - iter = xcb_setup_roots_iterator(xcb_get_setup(conn)); - res_cookie = xcb_randr_get_screen_resources_current(conn, iter.data->root); - res_reply = xcb_randr_get_screen_resources_current_reply(conn, res_cookie, &error); - - if (error) - { - xcb_disconnect(conn); - crtc_count = -1; - return NULL; - } - - crtc_count = res_reply->num_crtcs; - crtcs = xcb_randr_get_screen_resources_current_crtcs(res_reply); - } - - if (crtc_index >= crtc_count) - { - if (dc_count == 0) - { - xcb_disconnect(conn); - crtc_count = -1; - } - return NULL; - } - - dc_count++; - return crtcs + crtc_index; -} - - -/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd162609(v=vs.85).aspx */ -BOOL EnumDisplayDevices(LPCTSTR lpDevice, DWORD iDevNum, PDISPLAY_DEVICE lpDisplayDevice, DWORD dwFlags) -{ - size_t count = (size_t)crtc_count; - - (void) dwFlags; - - if (lpDevice != NULL) - { - fprintf(stderr, "lpDevice (argument 1) for EnumDisplayDevices should be NULL\n"); - abort(); - return FALSE; - } - if (crtc_count < 0) - { - if (GetDC(NULL) == NULL) - return FALSE; - dc_count = 0; - count = (size_t)crtc_count; - ReleaseDC(NULL, NULL); - } - if (iDevNum >= count) - return FALSE; - if (lpDisplayDevice->cb != sizeof(DISPLAY_DEVICE)) - { - fprintf(stderr, "lpDisplayDevice->cb for EnumDisplayDevices is not sizeof(DISPLAY_DEVICE)\n"); - abort(); - return FALSE; - } - sprintf(lpDisplayDevice->DeviceName, "%i", iDevNum); - lpDisplayDevice->StateFlags = DISPLAY_DEVICE_ACTIVE; - return TRUE; -} - diff --git a/src/fake_w32gdi.h b/src/fake_w32gdi.h deleted file mode 100644 index e0b9d59..0000000 --- a/src/fake_w32gdi.h +++ /dev/null @@ -1,73 +0,0 @@ -/** - * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -#ifndef BLUESHIFT_FAKE_W32GDI_H -#define BLUESHIFT_FAKE_W32GDI_H - -#include <stdint.h> - - -/* http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx */ -typedef uint16_t WORD; -typedef uint32_t DWORD; -typedef int BOOL; -typedef void* HDC; -typedef void* HWND; -typedef void* LPVOID; -typedef const char* LPCTSTR; -typedef char TCHAR; -#define TRUE 1 -#define FALSE 0 - - -/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd144871(v=vs.85).aspx */ -HDC GetDC(HWND hWnd); - -/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd162920(v=vs.85).aspx */ -int ReleaseDC(HWND hWnd, HDC hDC); - - -/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877(v=vs.85).aspx */ -int GetDeviceCaps(HDC hDC, int nIndex) __attribute__((const)); -#define COLORMGMTCAPS 1 -#define CM_GAMMA_RAMP 1 - -/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd372194(v=vs.85).aspx */ -BOOL SetDeviceGammaRamp(HDC hDC, LPVOID lpRamp); - -/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd316946(v=vs.85).aspx */ -BOOL GetDeviceGammaRamp(HDC hDC, LPVOID lpRamp); - - -/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd183490(v=vs.85).aspx */ -HDC CreateDC(LPCTSTR lpszDriver, LPCTSTR lpszDevice, void* lpszOutput, void* lpInitData); -#define TEXT(X) ((LPCTSTR)(X)) - -/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd183569(v=vs.85).aspx */ -typedef struct { - DWORD cb; - TCHAR DeviceName[32]; - DWORD StateFlags; -} DISPLAY_DEVICE; -typedef DISPLAY_DEVICE* PDISPLAY_DEVICE; -#define DISPLAY_DEVICE_ACTIVE 1 - -/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd162609(v=vs.85).aspx */ -BOOL EnumDisplayDevices(LPCTSTR lpDevice, DWORD iDevNum, PDISPLAY_DEVICE lpDisplayDevice, DWORD dwFlags); - - -#endif - @@ -203,7 +203,7 @@ def parse_icc(content): return f break # XXX should I not jump to (tag_offset + tag_size - ptr) here - # and not break the loops when unkown? + # and not break the loops when unknown? raise Exception('Unsupported ICC profile file') diff --git a/src/monitor.py b/src/monitor.py index 6f28da4..bfed220 100644 --- a/src/monitor.py +++ b/src/monitor.py @@ -23,21 +23,12 @@ from aux import * from curve import * -LIBDIR = 'bin' -''' -:str Path to libraries, '/usr/lib' is standard -''' - LIBEXECDIR = 'bin' ''' :str Path to executable libraries, '/usr/libexec' is standard ''' -## Add the path to libraries to the list of paths to Python modules -sys.path.append(LIBDIR) - - ## Load DRM module try: from blueshift_drm import * |